package com.tofly.ghrq_route.module.emergencyHandle.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;

import androidx.appcompat.widget.Toolbar;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.common.ui.view.LinePathView;
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/8.
 */

public class UiEmergencySignature extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.frameLayout_view)
    FrameLayout frameLayoutView;

    private LinePathView mSignatureView;

    private TroublePictureBean mPictureSignature;

    public static final String SIGNATURE_PICTURE_BEAN_KEY = "sign_picture_bean";

    public static void startActivityForResult(Activity activity) {
        Intent intent = new Intent(activity, UiEmergencySignature.class);
        activity.startActivityForResult(intent, C.REQUEST_CODE.SIGNATURE_REQUEST_CODE);
        activity.overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_emerge_signature);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();
    }

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

    private void initData() {

    }

    private void initListener() {
        setOnNavigationClickListener(v -> {
            removeCacheFile();
            finish();
        });
    }

    @Override
    public void onBackPressed() {
        removeCacheFile();
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @OnClick({R.id.btn_clear_draw, R.id.btn_finish_signature})
    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;
        }
    }

    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;
            toast(getString(R.string.save_signature_ok));

            Intent intent = new Intent();
            intent.putExtra(SIGNATURE_PICTURE_BEAN_KEY, mPictureSignature);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }else {
            mPictureSignature = null;
            toast(getString(R.string.save_signature_error));
        }
    }


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

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
