package com.simple.plugin.signDemo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.supcon.module_ext_plugin.R;
import com.supconit.hcmobile.permissions.BasePermissionsAppCompatActivityActivity;
import com.supconit.hcmobile.util.Util;
import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;

public class MainTwoActivity extends BasePermissionsAppCompatActivityActivity {
    private SignatureView mSignatureView;
    private Button btnClear;
    private Button btnSave;
    private Button btnQh;
    private ImageView btn_close;
    private LinearLayout qmqy;
    //权限获取标识
    private String qxBs="0";
    public static void start(Activity activity,String screenNum,String imgPath){
        Intent intent=new Intent(activity,MainTwoActivity.class);
        intent.putExtra("screenNum",screenNum);
        intent.putExtra("imgPath",imgPath);
        activity.startActivity(intent);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //0;竖屏  1; 横屏
        if (getIntent().getStringExtra("screenNum").equals("0")){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        setContentView(R.layout.activity_main_sign_demo);
        //先获取存储权限
        get();
        initView();
    }
    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        qmqy=findViewById(R.id.qmqy);
        mSignatureView = findViewById(R.id.view_signature);
        btnClear = findViewById(R.id.btn_clear);
        btnSave = findViewById(R.id.btn_save);
        btnQh=findViewById(R.id.btn_qh);
        btn_close=findViewById(R.id.btn_close);
        qmqy.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    qmqy.setVisibility(View.GONE);
                    mSignatureView.setVisibility(View.VISIBLE);
                }
                return false;
            }
        });
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSignatureView.clear();
            }
        });
        btnQh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 *系统中定义： int ORIENTATION_PORTRAIT = 1;  竖屏
                 *系统中定义： int ORIENTATION_LANDSCAPE = 2; 横屏
                 */
                int screenNum = getResources().getConfiguration().orientation;
                if (screenNum==1){
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }

            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    if (qxBs.equals("0")){
                        Toast.makeText(MainTwoActivity.this, "请先授予权限", Toast.LENGTH_SHORT).show();
                        get();
                        return;
                    }
                if (mSignatureView.getTouched()) {
                    try {

                        mSignatureView.save("/sdcard/sign.png", true, 10, MainTwoActivity.this,getIntent().getStringExtra("imgPath"));
                       EventImgPath imgPath=new EventImgPath();
                       imgPath.setImagePath(mSignatureView.getSavePath());
                        EventBus.getDefault().post(imgPath);
                        finish();
//                        Toast.makeText(MainTwoActivity.this, "图片保存在："+mSignatureView.getSavePath(), Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(MainTwoActivity.this, "请先签名", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    public void get(){
        Util.askPermission(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},"").subscribe(new SingleObserver<Boolean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onSuccess(Boolean aBoolean) {
                if (aBoolean){
                    qxBs="1";
                }
            }

            @Override
            public void onError(Throwable e) {

            }
        });
    }
}
