package com.hh.arome.activity.setting;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hh.arome.MyApp;
import com.hh.arome.R;
import com.hh.arome.services.keeplive.MyJobService;
import com.hh.arome.utils.UIUtils;
import com.hh.arome.view.MessageRoundBtnDialogClose;
import com.hh.lib_base.ConfigData;
import com.hh.lib_base.Constants;
import com.hh.lib_base.activity.BaseActivity;
import com.hh.lib_base.bean.device.DeviceConfigResultBean;
import com.hh.lib_base.bean.event.ExitAppEvent;
import com.hh.lib_base.utils.DoubClickUtils;
import com.hh.lib_base.utils.StringUtils;
import com.hh.lib_base.utils.ToastUtils;
import com.hh.lib_base.utils.device.DeviceUtils;
import com.hh.lib_device.deviceui.BaseDeviceUI;
import com.hh.lib_device.deviceui.IDeviceUI;

import org.greenrobot.eventbus.EventBus;

public class PasswordActivity extends BaseActivity implements View.OnClickListener {

    private String deviceModel ;

    private IDeviceUI iDeviceUI;
    private EditText et_pwd;
    private TextView tv_comfirm, tv_exit_app, tv_device_restart;
    private Toolbar tb_center;
    private TextView tv_title_center;
    private ImageButton iv_title_left;
    Handler mHandler =new Handler(Looper.getMainLooper());
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        deviceModel = DeviceUtils.getDeviceModel();
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        if(Constants.DeviceModel.D2.equals(deviceModel)){
            return R.layout.activity_password_hp;
        }else{
            return R.layout.activity_password;
        }
    }

    @Override
    protected void 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);
    }

    @Override
    protected void initData() {
        tv_title_center.setText("请输入密码");
        iDeviceUI = BaseDeviceUI.getInstance().getDeviceUI();
    }

    @Override
    protected void initEvent() {
        iv_title_left.setOnClickListener(this);
        tv_comfirm.setOnClickListener(this);
        tv_exit_app.setOnClickListener(this);
        tv_device_restart.setOnClickListener(this);
    }


    @Override
    public void onClick(View var1) {
        if(DoubClickUtils.onDoubClick()){
            return;
        }
        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(PasswordActivity.this);
        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(PasswordActivity.this);
        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() {
        MyJobService.Companion.stopJob(MyApp.getContext());
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                EventBus.getDefault().post(new ExitAppEvent()); //发送事件给MainActivity,关闭App
                finish();
            }
        },200);

    }

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

        iDeviceUI.reboot();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mHandler!=null){
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}