package com.hh.arome.ui;

import android.app.Dialog;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.common.lib_base.utils.StringUtils;
import com.common.lib_base.utils.ui.ToastUtils;
import com.common.lib_base.utils.ui.UIUtils;
import com.common.pos.api.util.ShellUtils;
import com.hh.arome.R;
import com.hh.arome.bean.DeviceConfigResultBean;
import com.hh.arome.common.ConfigData;
import com.hh.arome.common.Constants;
import com.hh.arome.ui.base.BaseHHActivity;
import com.hh.arome.ui.setting.SettingActivity;
import com.hh.arome.view.MessageRoundBtnDialogClose;

public class PasswordActivity extends BaseHHActivity implements View.OnClickListener {

    private Toolbar tb_center;

    @Override
    public int setLayout() {
        return R.layout.activity_password;
//        return R.layout.activity_zolo;
    }

    EditText et_pwd;
    TextView tv_comfirm, tv_exit_app, tv_device_restart;

    TextView tv_title_center;
    ImageButton iv_title_left;

    @Override
    protected void initView() {
        super.initView();

        tv_title_center = findViewById(R.id.tv_title_center);
        iv_title_left = findViewById(R.id.iv_title_left);

        et_pwd = findViewById(R.id.et_pwd);
        tv_comfirm = findViewById(R.id.tv_comfirm);
        tv_exit_app = findViewById(R.id.tv_exit_app);
        tv_device_restart = findViewById(R.id.tv_device_restart);
        tb_center = findViewById(R.id.tb_center);
        iv_title_left.setOnClickListener(this);
        tv_comfirm.setOnClickListener(this);
        tv_exit_app.setOnClickListener(this);
        tv_device_restart.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tv_title_center.setText("请输入密码");
    }

    @Override
    protected void initListener() {


    }


    @Override
    public void onClick(View var1) {
        switch (var1.getId()) {
            case R.id.iv_title_left:
                startMainActivity();
                break;
            case R.id.tv_comfirm:
                confirmPwd();
                break;
            case R.id.tv_exit_app:
                showDialogExit();
                break;
            case R.id.tv_device_restart:
                showDialogDeviceRestart();
                break;
        }
    }


    //返回键监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            startMainActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void startMainActivity() {
        ConfigData.mSessionId = "";
        startActivity(MainActivity.class);
        finish();
    }


    private void confirmPwd() {

        String inputPwd = UIUtils.getEditText(et_pwd);

        if (StringUtils.isNull(inputPwd)) {
            ToastUtils.showLongToast("请输入密码");
            return;
        }

        DeviceConfigResultBean deviceConfigResultBean = ConfigData.getDeviceConfigResultBean();

        if (inputPwd.equals(Constants.Value.PWD)) {
            startActivity(SettingActivity.class);
            finish();
        } else if (deviceConfigResultBean != null) {
            String mpassword = deviceConfigResultBean.getMpassword();
            if (StringUtils.isNotNull(mpassword) && inputPwd.equals(mpassword)) {
                startActivity(SettingActivity.class);
                finish();
            } else {
                ToastUtils.showLongToast("密码错误，清重新输入");
            }
        } else {
            ToastUtils.showLongToast("密码错误，清重新输入");
        }


    }

    private void showDialogDeviceRestart() {
        MessageRoundBtnDialogClose.Builder builder = new MessageRoundBtnDialogClose.Builder(mContext);
        builder.setHaveTitle(false)   //是否要标题
                .setIsOneBtn(false)   //是否只有一个按钮  只有一个按钮的情况下面只有设置左边的按钮
                .setMessage("是否重启设备")                      //中间信息部分
                .setOnClickLisener(new MessageRoundBtnDialogClose.onDialogClickListener() {    //监听事件
                    @Override
                    public void onRightClick(Dialog dialog) {
                        super.onRightClick(dialog);
                        restartDevice();
                    }
                }).build().show();
    }


    public void showDialogExit() {
        MessageRoundBtnDialogClose.Builder builder = new MessageRoundBtnDialogClose.Builder(mContext);
        builder.setHaveTitle(false)   //是否要标题
                .setIsOneBtn(false)   //是否只有一个按钮  只有一个按钮的情况下面只有设置左边的按钮
                .setMessage("是否退出应用")                      //中间信息部分
                .setOnClickLisener(new MessageRoundBtnDialogClose.onDialogClickListener() {    //监听事件
                    @Override
                    public void onRightClick(Dialog dialog) {
                        super.onRightClick(dialog);
                        closeApp();
                    }
                }).build().show();
    }


    // 关闭app
    private void closeApp() {

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("IsExist", true);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
        finish();

    }

    // 重启设备
    private void restartDevice() {

        try {
            // posutil
            ShellUtils.execCommand("reboot", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
