package com.wmdigit.wmposdemo.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.elvishew.xlog.XLog;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.wmdigit.wmpos.WmAceKG;

import com.wmdigit.wmpos.utils.SPStaticUtils;
import com.wmdigit.wmposdemo.R;
import com.wmdigit.wmposdemo.databinding.ActivityMainBinding;
import com.wmdigit.wmposdemo.utils.Constant;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        checkAllPermissions(this);
        binding.tvRecognition.setOnClickListener(v -> {
            boolean isBind = SPStaticUtils.getBoolean(Constant.BIND, false);
            if (isBind) {
                boolean isScale = WmAceKG.getScaleSetting();
                if (!isScale) {
                    toast("您还未裁剪秤盘,为您跳转至裁剪页");
                    Intent intent = new Intent(MainActivity.this, CropPicActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    toast("获取秤盘信息成功,为您跳转至识别功能页");
                    Intent intent1 = new Intent(MainActivity.this, WeighingActivity.class);
                    startActivity(intent1);
                }
            } else {
                toast("您还未给pos机绑定SN号,为您跳转至注册页");
                Intent intent = new Intent(this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        binding.tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

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

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

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

    @SuppressLint("WrongConstant")
    private void checkAllPermissions(final Activity activity) {
        XXPermissions.with(this)
                // 申请单个权限
                .permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE, Permission.CAMERA)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            toast("获取权限成功");
                        } else {
                            toast("获取部分权限成功，但部分权限未正常授予");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            toast("被永久拒绝授权，请手动授予存储和摄像头权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(MainActivity.this, permissions);
                        } else {
                            toast("获取存储和摄像头权限失败");
                        }
                    }
                });
    }

    private void toast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }
}