package com.android.launcher3.function.setting

import android.content.Context
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.widget.EditText
import com.android.launcher3.BaseActivity
import com.android.launcher3.R
import com.android.launcher3.function.pwd.PwdPinSetPwdActivity
import com.android.launcher3.settings.ErrorTimeHelper
import com.secspace.lib.common.utils.Prefs
import com.secspace.lib.common.utils.ToastUtil
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

    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 {
            ToastUtil.showShort(this@ChangePwdActivity, R.string.edu_toast_wait)
        }
    }

    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 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()) {
            ToastUtil.showShort(this, String.format(getString(R.string.edu_item_input_lock), ErrorTimeHelper.getInstance(this).countDownTime))
            return
        }
        if (newPwd.isEmpty()|| newPwd.length < PwdPinSetPwdActivity.PWD_TINY_LENGTH) {
            ToastUtil.showShort(this, R.string.edu_item_input_newpwd_null)
            return
        }
        if (confirmNewPwd.isEmpty() || confirmNewPwd.length < PwdPinSetPwdActivity.PWD_TINY_LENGTH) {
            ToastUtil.showShort(this, R.string.edu_item_input_newpwd_again_null)
            return
        }
        if (originalPwd.isEmpty()) {
            ToastUtil.showShort(this, R.string.edu_item_input_oldpwd_null)
            return
        }
        if (!TextUtils.equals(originalPwd, Prefs.getLoginPwd(this))) {
            ToastUtil.showShort(this, R.string.edu_item_input_pwd_error)
            ErrorTimeHelper.getInstance(this).addErrorCount()
            return
        }
        if (!TextUtils.equals(newPwd, confirmNewPwd)) {
            ToastUtil.showShort(this, R.string.edu_item_input_pwd_different)
            return
        }
        ToastUtil.showShort(this, R.string.edu_item_input_pwd_change_success)
        Prefs.setLoginPwd(this, newPwd)
        //如果存在输错密码多次的情况下重置密码
        ErrorTimeHelper.getInstance(this).resetLockTimeAndCount()
        finish()
    }

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