package cn.yyxx.eyuangame.core.impl.center.fragment

import android.os.Bundle
import android.text.InputType
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import androidx.fragment.app.Fragment
import cn.yyxx.eyuangame.core.entity.ClickType
import cn.yyxx.eyuangame.core.entity.ResultInfo
import cn.yyxx.eyuangame.core.entity.SdkBackLoginInfo
import cn.yyxx.eyuangame.core.impl.center.MemberActivity
import cn.yyxx.eyuangame.core.internal.IRequestCallback
import cn.yyxx.eyuangame.core.network.SdkRequest
import cn.yyxx.eyuangame.core.ui.EventEditText
import cn.yyxx.eyuangame.core.utils.EditTextUtils
import cn.yyxx.support.ResUtils
import cn.yyxx.support.hawkeye.ToastUtils
import org.json.JSONObject

/**
 * @author #Suyghur.
 * Created on 2021/06/30
 */
class ModifyPwdFragment : Fragment(), View.OnClickListener {

    private lateinit var mView: View
    private lateinit var memberImpl: MemberActivity

    private lateinit var ivReturn: ImageView
    private lateinit var eetOldPwd: EventEditText
    private lateinit var eetNewPwd: EventEditText
    private lateinit var btnConfirm: Button

    private var imgShow = 0
    private var imgHide = 0
    private var isShowOldText = false
    private var isShowNewText = false

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        memberImpl = requireActivity() as MemberActivity
        mView = layoutInflater.inflate(ResUtils.getResId(requireActivity(), "yyxx_member_modify_pwd", "layout"), container, false)
        initView(mView)
        return mView
    }

    private fun initView(view: View) {

        imgShow = ResUtils.getResId(requireActivity(), "yyxx_show_img", "drawable")
        imgHide = ResUtils.getResId(requireActivity(), "yyxx_hide_img", "drawable")

        ivReturn = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_iv_return", "id"))
        ivReturn.apply {
            tag = ClickType.ACTION_RETURN
            setOnClickListener(this@ModifyPwdFragment)
        }

        eetOldPwd = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_eet_old_pwd", "id"))
        eetOldPwd.apply {
            editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
            leftImageView.setBackgroundResource(ResUtils.getResId(requireActivity(), "yyxx_pwd_img", "drawable"))
            leftImageView.visibility = View.VISIBLE
            editText.setHint(ResUtils.getResId(requireActivity(), "yyxx_hint_modify_pwd_old", "string"))
            rightImageView.setBackgroundResource(imgShow)
            rightImageView.visibility = View.VISIBLE
            rightImageView.setOnClickListener {
                if (!isShowOldText) {
                    isShowOldText = true
                    rightImageView.setBackgroundResource(imgHide)
                    editText.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                } else {
                    isShowOldText = false
                    rightImageView.setBackgroundResource(imgShow)
                    editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
                }
                editText.setSelection(editText.length())
            }
        }


        eetNewPwd = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_eet_new_pwd", "id"))
        eetNewPwd.apply {
            editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
            leftImageView.setBackgroundResource(ResUtils.getResId(requireActivity(), "yyxx_pwd_img", "drawable"))
            leftImageView.visibility = View.VISIBLE
            editText.setHint(ResUtils.getResId(requireActivity(), "yyxx_hint_modify_pwd_new", "string"))
            rightImageView.setBackgroundResource(imgShow)
            rightImageView.visibility = View.VISIBLE
            rightImageView.setOnClickListener {
                if (!isShowNewText) {
                    isShowNewText = true
                    rightImageView.setBackgroundResource(imgHide)
                    editText.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                } else {
                    isShowNewText = false
                    rightImageView.setBackgroundResource(imgShow)
                    editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
                }
                editText.setSelection(editText.length())
            }
        }

        btnConfirm = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_btn_confirm", "id"))
        btnConfirm.apply {
            tag = ClickType.ACTION_CONFIRM
            setOnClickListener(this@ModifyPwdFragment)
        }

    }

    private fun callModifyPwd() {
        val oldPwd = eetOldPwd.editText.text.trim().toString()
        val newPwd = eetNewPwd.editText.text.trim().toString()

        if (!EditTextUtils.filterPwd(oldPwd)) {
            ToastUtils.toastInfo(requireActivity(), ResUtils.getResString(requireActivity(), "yyxx_tips_old_pwd_format_error"))
            return
        }

        if (!EditTextUtils.filterPwd(newPwd)) {
            ToastUtils.toastInfo(requireActivity(), ResUtils.getResString(requireActivity(), "yyxx_tips_new_pwd_format_error"))
            return
        }

        try {
            with(JSONObject()) {
                put("uid", SdkBackLoginInfo.instance.userId)
                put("pwd", oldPwd)
                put("new_pwd", newPwd)
                SdkRequest.instance.modifyUserPwd(requireActivity(), this, object : IRequestCallback {
                    override fun onResponse(resultInfo: ResultInfo) {
                        if (resultInfo.code == 1) {
                            ToastUtils.toastInfo(requireActivity(), resultInfo.msg)
                            requireActivity().onBackPressed()
                        } else {
                            ToastUtils.toastInfo(requireActivity(), resultInfo.msg)
                        }
                    }

                })
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onClick(v: View?) {
        v?.apply {
            when (tag as Int) {
                ClickType.ACTION_RETURN -> requireActivity().onBackPressed()
                ClickType.ACTION_CONFIRM -> callModifyPwd()
            }
        }
    }
}