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

import android.content.DialogInterface
import android.os.Bundle
import android.text.InputType
import android.text.TextUtils
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import cn.yyxx.eyuangame.core.entity.ClickType
import cn.yyxx.eyuangame.core.impl.SdkBridgeImpl
import cn.yyxx.eyuangame.core.impl.login.LoginActivity
import cn.yyxx.eyuangame.core.ui.EventEditText
import cn.yyxx.eyuangame.core.ui.dialog.AgreementDialog
import cn.yyxx.eyuangame.core.utils.EditTextUtils
import cn.yyxx.support.ResUtils
import cn.yyxx.support.hawkeye.ToastUtils
import org.json.JSONException

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

    private lateinit var mView: View
    private lateinit var loginImpl: LoginActivity

    private lateinit var eetAccount: EventEditText
    private lateinit var eetPwd: EventEditText

    private lateinit var btnRegister: Button

    private lateinit var ivCheck: ImageView
    private lateinit var tvAgreement: TextView

    private var imgShow = 0
    private var imgHide = 0
    private var imgCheck = 0
    private var imgUnCheck = 0

    private var check = true
    private var isShowText = false

    private var agreementDialog: AgreementDialog? = null


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        loginImpl = requireActivity() as LoginActivity
        mView = inflater.inflate(ResUtils.getResId(requireActivity(), "yyxx_login_register", "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")
        imgCheck = ResUtils.getResId(requireActivity(), "yyxx_check_img", "drawable")
        imgUnCheck = ResUtils.getResId(requireActivity(), "yyxx_uncheck_img", "drawable")

        eetAccount = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_eet_account", "id"))
        eetAccount.apply {
            leftImageView.setBackgroundResource(ResUtils.getResId(requireActivity(), "yyxx_account_img", "drawable"))
            leftImageView.visibility = View.VISIBLE
            editText.setHint(ResUtils.getResId(requireActivity(), "yyxx_hint_register_account", "string"))
        }

        eetPwd = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_eet_pwd", "id"))
        eetPwd.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_register_pwd", "string"))
            rightImageView.setBackgroundResource(imgShow)
            rightImageView.visibility = View.VISIBLE
            rightImageView.setOnClickListener {
                if (!isShowText) {
                    isShowText = true
                    rightImageView.setBackgroundResource(imgHide)
                    editText.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                } else {
                    isShowText = false
                    rightImageView.setBackgroundResource(imgShow)
                    editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
                }
                editText.setSelection(editText.length())
            }
        }

        btnRegister = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_btn_register", "id"))

        ivCheck = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_iv_check", "id"))
        tvAgreement = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_tv_agreement", "id"))
        if (SdkBridgeImpl.initBean.privacyCfg.switch == 0 || TextUtils.isEmpty(SdkBridgeImpl.initBean.privacyCfg.url)) {
            ivCheck.visibility = View.GONE
            tvAgreement.visibility = View.GONE
        } else {
            ivCheck.visibility = View.VISIBLE
            tvAgreement.visibility = View.VISIBLE
        }

        initViewAction()
        initViewListener()
    }

    private fun initViewAction() {
        btnRegister.tag = ClickType.ACTION_REGISTER
        ivCheck.tag = ClickType.ACTION_CLICK_CHECK
        tvAgreement.tag = ClickType.ACTION_CLICK_AGREEMENT

    }

    private fun initViewListener() {
        btnRegister.setOnClickListener(this)
        ivCheck.setOnClickListener(this)
        tvAgreement.setOnClickListener(this)
    }

    private fun changeCheck() {
        if (check) {
            check = false
            ivCheck.setBackgroundResource(imgUnCheck)
        } else {
            check = true
            ivCheck.setBackgroundResource(imgCheck)
        }
    }

    private fun showAgreementDialog() {
        agreementDialog?.apply {
            dismiss()
            agreementDialog = null
        }
        agreementDialog = AgreementDialog(requireContext(), SdkBridgeImpl.initBean.privacyCfg.url, SdkBridgeImpl.isLand)
        agreementDialog?.apply {
            loginImpl.hideLoginView()
            ivReturn.setOnClickListener {
                dismiss()
                loginImpl.showLoginView()
            }
            setOnKeyListener(object : DialogInterface.OnKeyListener {
                override fun onKey(dialog: DialogInterface?, keyCode: Int, event: KeyEvent?): Boolean {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        dismiss()
                        loginImpl.showLoginView()
                        return true
                    }
                    return false
                }
            })
            show()
        }
    }

    override fun onClick(v: View?) {
        v?.apply {
            when (tag as Int) {
                ClickType.ACTION_CLICK_CHECK -> changeCheck()
                ClickType.ACTION_CLICK_AGREEMENT -> showAgreementDialog()
                ClickType.ACTION_REGISTER -> {
                    try {
                        val userName = eetAccount.editText.text.trim().toString()
                        val pwd = eetPwd.editText.text.trim().toString()
                        if (!EditTextUtils.filterAccount(userName)) {
                            ToastUtils.toastInfo(requireActivity(), ResUtils.getResString(requireActivity(), "yyxx_tips_account_format_error"))
                            return
                        }
                        if (!EditTextUtils.filterPwd(pwd)) {
                            ToastUtils.toastInfo(requireActivity(), ResUtils.getResString(requireActivity(), "yyxx_tips_pwd_format_error"))
                            return
                        }
                        loginImpl.userSignInImpl.userRegister(requireActivity(), userName, pwd)
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                }
            }
        }

    }
}