package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.ui.view.LinePathView;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;
import com.tofly.ghrq_route.common.util.file.FileUtil;
import com.tofly.ghrq_route.common.util.file.MimeUtil;

import java.util.Date;
import java.util.UUID;

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


/**
 * Created by Amu on 2017/6/19.
 */

public class SignatureBottomDialog extends BottomSheetDialog{
    @BindView(R.id.frameLayout_view)
    FrameLayout frameLayoutView;

    private Context mContext;

    private LinePathView mSignatureView;

    private TroublePictureBean mPictureSignature;

    public void setGetSignature(GetSignature getSignature) {
        this.getSignature = getSignature;
    }

    private GetSignature getSignature;

    public interface GetSignature {
        void onGetSignature(TroublePictureBean signature);
    }

    public SignatureBottomDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_signature_bottom);
        ButterKnife.bind(this);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels); // 高度设置为屏幕的0.6
        lp.height = (int) (d.heightPixels);
        dialogWindow.setAttributes(lp);

        initView();
        initData();
    }


    @Override
    public void show() {
        super.show();
        //设置高度
        FrameLayout bottomSheet = findViewById(R.id.design_bottom_sheet);
        BottomSheetBehavior mDialogBehavior = BottomSheetBehavior.from(bottomSheet);
        mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

    private void initView() {
        mSignatureView = new LinePathView(mContext);
        mSignatureView.setBackColor(Color.WHITE);
        frameLayoutView.addView(mSignatureView);
        mSignatureView.requestFocus();
    }

    private void initData() {

    }

    @OnClick({R.id.btn_clear_draw, R.id.btn_finish_signature, R.id.btn_header_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_clear_draw: {
                mSignatureView.clear();
                removeCacheFile();
            }
            break;
            case R.id.btn_finish_signature: {
                saveImage();
            }
            break;
            case R.id.btn_header_close:
                dismiss();
                break;
        }
    }

    private void saveImage() {
        boolean success = false;
        TroublePictureBean picture = null;
        Bitmap bitmap = mSignatureView.getCachebBitmap();
        if (bitmap != null) {
            String pictureid = UUID.randomUUID().toString();
            String filename = pictureid + "." + "png";
            String time = DateUtils.getDate(new Date().getTime());
            String[] outPath = new String[1];
            boolean bSaveOk = FileUtil.saveImage(bitmap, filename, false, outPath);//保存图片到sd卡
            if (bSaveOk) {
                String filepath = outPath[0];
                if (filepath != null && FileUtil.fileExists(filepath)) {
                    success = true;
                    picture = new TroublePictureBean();
                    picture.setId(pictureid);
                    String fileType = MimeUtil.getMimeType(filepath);
                    picture.setFiletype(fileType);
                    picture.setFilepath(filepath);
                    picture.setStatus(TroubleBean.SAVED_STATE);
                    picture.setCreateTime(time);
                }
            }
        }

        if (success && picture != null) {
            mPictureSignature = picture;
            ToastUtil.showToast(mContext, mContext.getString(R.string.save_signature_ok));

            if (getSignature != null) {
                getSignature.onGetSignature(mPictureSignature);
            }
            dismiss();
        }else {
            mPictureSignature = null;
            ToastUtil.showToast(mContext, mContext.getString(R.string.save_signature_error));
        }
    }

    private void removeCacheFile(){
        //删除临时文件
        if (mPictureSignature != null){
            String file = mPictureSignature.getFilepath();
            FileUtil.deleteFile(file);
            mPictureSignature = null;
        }
    }

}
