package com.android.launcher3.function.pwd

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.SpannedString
import android.text.TextUtils
import android.text.style.AbsoluteSizeSpan
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.android.launcher3.BaseActivity
import com.android.launcher3.R
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.encrypt
import kotlinx.android.synthetic.main.activity_change_pwd.*
import kotlinx.android.synthetic.main.activity_setting.tv_setting_title


class ChangePwdActivity : BaseActivity() {
    private var mContext: Context? = null
    private var question: String? = null
    private var answer1: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setAndroidNativeLightStatusBar(true)
        setContentView(R.layout.activity_change_pwd)
        mContext = this
        initView()
    }

    private fun initView() {
        tv_setting_title.setText(R.string.edu_item_change_pwd)
        setHintSize(et_old_pwd, getString(R.string.edu_item_input_oldpwd))
        setHintSize(et_new_pwd, getString(R.string.edu_item_input_newpwd))
        setHintSize(et_new_pwd_again, getString(R.string.edu_item_input_newpwd_again))

        tv_confir_pwd_protection.setOnClickListener {
            showEncryptedVerDialog()
        }
    }

    private fun setHintSize(editText: EditText, text: String) {
        val ss = SpannableString(text)
        val ass = AbsoluteSizeSpan(15, true)
        ss.setSpan(ass, 0, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        editText.hint = SpannedString(ss)
    }

    fun btnClick(v: View) {
        val imm: InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(window.decorView.windowToken, 0)
        val originalPwd = et_old_pwd.text.toString()
        val newPwd = et_new_pwd.text.toString()
        val confirmNewPwd = et_new_pwd_again.text.toString()
        if (!ErrorTimeHelper.getInstance(this).canShowDialog()) {
            DialogUIUtils.showToastCenter(String.format(getString(R.string.edu_item_input_lock), ErrorTimeHelper.getInstance(this).countDownTime))
            return
        }
        if (newPwd.isEmpty()|| newPwd.length < PwdPinSetPwdActivity.PWD_TINY_LENGTH) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_newpwd_null)
            return
        }
        if (confirmNewPwd.isEmpty() || confirmNewPwd.length < PwdPinSetPwdActivity.PWD_TINY_LENGTH) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_newpwd_again_null)
            return
        }
        if (originalPwd.isEmpty()) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_oldpwd_null)
            return
        }
        if (!TextUtils.equals(originalPwd, Prefs.getLoginPwd(this))) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_pwd_error)
            ErrorTimeHelper.getInstance(this).addErrorCount()
            return
        }
        if (!TextUtils.equals(newPwd, confirmNewPwd)) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_pwd_different)
            return
        }
        DialogUIUtils.showToastCenter(R.string.edu_item_input_pwd_change_success)
        Prefs.setLoginPwd(this, newPwd)
        //如果存在输错密码多次的情况下重置密码
        ErrorTimeHelper.getInstance(this).resetLockTimeAndCount()
        finish()
    }

    private fun showEncryptedVerDialog() {
        val encryptedStr = Prefs.getEncrypted(mAppContext)
        if (TextUtils.isEmpty(encryptedStr)) {
            DialogUIUtils.showToastCenter("请先设置密保")
            return
        }
        val (question1, answer11, question2, answer2) = JsonUtil.fromJson(encryptedStr, Encrypted::class.java)
        if (TextUtils.equals(question1, question)) {
            question = question2
            answer1 = answer2
        } else {
            question = question1
            answer1 = answer11
        }
        DialogManager.showEncryptedVerDialog(this, getString(R.string.edu_item_input_encryted_title), question, object : DialogManager.PwdListener {
            override fun onPositive(view: View?) {
                val editText = view as EditText?
                InputUtil.closeSoftInput(applicationContext, editText)
                val editable = editText!!.text
                if (editable == null || TextUtils.isEmpty(editable.toString())) {
                    DialogUIUtils.showToastCenter(R.string.edu_item_input_encryted_title)
                    return
                }
                var answer = editable.toString()
                answer = encrypt(answer)
                if (TextUtils.equals(answer1, answer)) {
                    val intent = Intent(mAppContext, PwdPinSetPwdActivity::class.java)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    startActivity(intent)
                    return
                }
                DialogUIUtils.showToastCenter("密保答案错误")
            }

            override fun onNegative() {
                showEncryptedVerDialog()
            }
        })
    }

    fun close(view: View?) {
        finish()
    }
}