package com.danding.yang800;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.danding.yang800.Presenter.MentorActivityPresenter;
import com.danding.yang800.Util.ImgUtils;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.View.NetworkProgressDialog;
import com.yanzhenjie.sofia.Sofia;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MentorActivity extends BaseActivity {
    @BindView(R.id.mentor_backTv)
    TextView mentor_backTv;
    @BindView(R.id.mentor_saveTv)
    TextView mentor_saveTv;
    @BindView(R.id.mentor_zxIv)
    ImageView mentor_zxIv;
    private NetworkProgressDialog pd;
    MentorActivityPresenter presenter;
    String storeId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_mentor);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        presenter = new MentorActivityPresenter(this);
        storeId = sp.getString("storeId", "");
        presenter.getInfo(storeId);
    }

    @OnClick({R.id.mentor_backTv, R.id.mentor_saveTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mentor_backTv:
                finish();
                break;
            case R.id.mentor_saveTv:
                Bitmap bmap = createViewBitmap(mentor_zxIv);
                boolean save = ImgUtils.saveImageToGallery(MentorActivity.this, bmap);
                if (save) {
                    ToastUtil.show(MentorActivity.this, "保存成功");
                } else {

                }
                break;

        }
    }

    public Bitmap createViewBitmap(View v) {
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        v.draw(canvas);
        return bitmap;
    }

    public void initDialog() {
        if (pd == null) {
            pd = NetworkProgressDialog.createDialog(this);
            if (!pd.isShowing()) {
                pd.show();
            }
        }
    }

    public void dismissDialog() {
        if (pd != null) {
            pd.dismiss();
            pd = null;
            NetworkProgressDialog.clear();
        }
    }

    public void update(String result) {
        Glide.with(this).load(result).diskCacheStrategy(DiskCacheStrategy.NONE).into(mentor_zxIv);
        dismissDialog();
    }
}
