package com.android.launcher3.function.pwd.fragment

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.SpannableString
import android.text.Spanned
import android.text.SpannedString
import android.text.style.AbsoluteSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.android.launcher3.R
import com.secspace.lib.common.function.model.Encrypted
import com.android.launcher3.settings.ErrorTimeHelper
import com.google.gson.Gson
import com.secspace.lib.common.dialog.other.DialogUIUtils
import com.secspace.lib.common.utils.Prefs
import com.secspace.lib.common.utils.encrypt


class EncryptedSetFragment : Fragment() {
    private lateinit var questionEt1: EditText
    private lateinit var questionEt2: EditText
    private lateinit var answerEt1: EditText
    private lateinit var answerEt2: EditText
    private lateinit var sureBtn: Button
    private lateinit var titleTv: TextView
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view: View = inflater.inflate(R.layout.fragment_encrypted_set, container, false)
        initView(view)
        return view
    }

    private fun initView(view: View) {
        titleTv = view.findViewById(R.id.tv_setting_title)
        sureBtn = view.findViewById(R.id.btn_encrypted_set)
        questionEt1 = view.findViewById(R.id.et_question1)
        questionEt2 = view.findViewById(R.id.et_question2)
        answerEt1 = view.findViewById(R.id.et_answer1)
        answerEt2 = view.findViewById(R.id.et_answer2)
        titleTv.setText(R.string.edu_item_input_encryted_set)

        setHintSize(questionEt1, getString(R.string.edu_item_input_encryted_question1))
        setHintSize(answerEt1, getString(R.string.edu_item_input_encryted_answer1))
        setHintSize(questionEt2, getString(R.string.edu_item_input_encryted_question2))
        setHintSize(answerEt2, getString(R.string.edu_item_input_encryted_answer2))
        sureBtn.setOnClickListener {
            btnClick()
        }
        titleTv.setOnClickListener {
            activity?.finish()
        }
    }

    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)
    }

    private fun btnClick() {
        val imm: InputMethodManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(activity?.window?.decorView?.windowToken, 0)
        val question1 = questionEt1.text.toString()
        var answer1 = answerEt1.text.toString()
        val question2 = questionEt2.text.toString()
        var answer2 = answerEt2.text.toString()
        if (question1.isEmpty() || question2.isEmpty()) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_question_null)
            return
        }
        if (answer1.isEmpty() || answer2.isEmpty()) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_answer_null)
            return
        }

        DialogUIUtils.showToastCenter(R.string.edu_item_input_encryted_set_success)

        answer1 = encrypt(answer1)
        answer2 = encrypt(answer2)

        val encrypted = Encrypted(question1, answer1, question2, answer2)
        Prefs.setEncrypted(activity, Gson().toJson(encrypted))
        //如果存在输错密码多次的情况下重置密码
        ErrorTimeHelper.getInstance(activity).resetLockTimeAndCount()
        activity?.finish()
    }
}