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

import android.content.Context
import android.content.Intent
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.function.pwd.EncryptedActivity
import com.secspace.lib.common.dialog.other.DialogUIUtils
import com.secspace.lib.common.utils.JsonUtil
import com.secspace.lib.common.utils.Prefs
import com.secspace.lib.common.utils.encrypt

class EncryptedVerifyFragment : Fragment() {
    private lateinit var questionTv1: TextView
    private lateinit var questionTv2: TextView
    private lateinit var answerEt1: EditText
    private lateinit var answerEt2: EditText
    private lateinit var sureBtn: Button
    private lateinit var titleTv: TextView
    private lateinit var encrypted: Encrypted
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view: View = inflater.inflate(R.layout.fragment_encrypted_ver, container, false)
        val encryptedStr = Prefs.getEncrypted(activity)
        if (encryptedStr.isEmpty()) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_encryted_error)
        }
        encrypted = JsonUtil.fromJson(encryptedStr, Encrypted::class.java)
        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)
        questionTv1 = view.findViewById(R.id.tv_question1)
        questionTv2 = view.findViewById(R.id.tv_question2)
        answerEt1 = view.findViewById(R.id.et_answer1)
        answerEt2 = view.findViewById(R.id.et_answer2)
        titleTv.setText(R.string.edu_item_input_encryted_verfiy)

        setHintSize(answerEt1, getString(R.string.edu_item_input_encryted_answer1))
        setHintSize(answerEt2, getString(R.string.edu_item_input_encryted_answer2))
        questionTv1.text = encrypted.question1
        questionTv2.text = encrypted.question2

        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)
        var answer1 = answerEt1.text.toString()
        var answer2 = answerEt2.text.toString()
        if (answer1.isEmpty() || answer2.isEmpty()) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_answer_null)
            return
        }

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

        if (answer1 != encrypted.answer1) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_answer1_error)
            return
        }
        if (answer2 != encrypted.answer2) {
            DialogUIUtils.showToastCenter(R.string.edu_item_input_answer2_error)
            return
        }

        DialogUIUtils.showToastCenter(R.string.edu_item_input_encryted_ver_success)

        val intent = Intent(activity, EncryptedActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        intent.putExtra("from", EncryptedVerifyFragment::class.java.simpleName)
        startActivity(intent)
        activity?.finish()
    }
}