package com.android.launcher3;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.android.launcher3.function.pwd.PwdPinSetPwdActivity;
import com.android.launcher3.function.setting.SettingAdminActivity;
import com.android.launcher3.settings.ErrorTimeHelper;
import com.secspace.lib.common.dialog.other.DialogUIUtils;
import com.secspace.lib.common.function.DialogManager;
import com.secspace.lib.common.function.model.Encrypted;
import com.secspace.lib.common.utils.InputUtil;
import com.secspace.lib.common.utils.JsonUtil;
import com.secspace.lib.common.utils.Prefs;
import com.secspace.lib.common.utils.ToastUtil;
import com.secspace.lib.common.utils.UtilsKt;


public class ManagerAuthActivity extends BaseActivity {
    protected Context mAppContext;
    private String question;
    private String answer1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_auth);
        mAppContext = this.getApplicationContext();
        showPwdDialog();
    }

    private void showPwdDialog() {
        DialogManager.INSTANCE.showPwdDialog(this, "请输入密码", new DialogManager.PwdCanVerListener() {

            @Override
            public void onNeutral() {
                showEncryptedVerDialog();
            }

            @Override
            public void onPositive(View view) {
                EditText editText = (EditText) view;
                InputUtil.closeSoftInput(getApplicationContext(), editText);
                boolean canInputPwd = ErrorTimeHelper.getInstance(mAppContext).canInputPwd();
                if (canInputPwd) {
                    if (TextUtils.equals(editText.getText().toString(), Prefs.getLoginPwd(mAppContext))) {
                        finish();
                        ErrorTimeHelper.getInstance(mAppContext).resetLockTimeAndCount();
                        Intent intent = new Intent();
                        intent.setClass(mAppContext, SettingAdminActivity.class);
                        startActivity(intent);
                    } else {
                        DialogUIUtils.showToastCenter("密码错误");
                        ErrorTimeHelper.getInstance(mAppContext).addErrorCount();
                        finish();
                    }
                } else {
                    ToastUtil.showShort(mAppContext, "请在" + ErrorTimeHelper.getInstance(mAppContext).getCountDownTime() + "秒后重试");
                    finish();
                }
            }

            @Override
            public void onNegative() {
                finish();
            }
        });
    }

    private void showEncryptedVerDialog() {
        String encryptedStr = Prefs.getEncrypted(mAppContext);
        if (TextUtils.isEmpty(encryptedStr)) {
            DialogUIUtils.showToastCenter("请先设置密保");
            finish();
            return;
        }

        Encrypted encrypted = JsonUtil.fromJson(encryptedStr, Encrypted.class);
        if (TextUtils.equals(encrypted.getQuestion1(), question)) {
            question = encrypted.getQuestion2();
            answer1 = encrypted.getAnswer2();
        } else {
            question = encrypted.getQuestion1();
            answer1 = encrypted.getAnswer1();
        }

        DialogManager.INSTANCE.showEncryptedVerDialog(this, getString(R.string.edu_item_input_encryted_title), question, new DialogManager.PwdListener() {
            @Override
            public void onPositive(View view) {
                EditText editText = (EditText) view;
                InputUtil.closeSoftInput(getApplicationContext(), editText);
                Editable editable = editText.getText();
                if (editable == null || TextUtils.isEmpty(editable.toString())) {
                    DialogUIUtils.showToastCenter("密保答案为空");
                    finish();
                    return;
                }
                String answer = editable.toString();
                answer = UtilsKt.encrypt(answer);
                if (TextUtils.equals(answer1, answer)) {
                    Intent intent = new Intent(mAppContext, PwdPinSetPwdActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                    return;
                }
                DialogUIUtils.showToastCenter("密保答案错误");
                finish();
            }

            @Override
            public void onNegative() {
                showEncryptedVerDialog();
            }
        });
    }
}
