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 BindAccountFragment : Fragment(), View.OnClickListener {

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

    private lateinit var ivReturn: ImageView
    private lateinit var eetAccount: EventEditText
    private lateinit var eetPwd: EventEditText
    private lateinit var btnConfirm: Button

    private var imgShow = 0
    private var imgHide = 0
    private var isShowText = 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_bind_account", "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@BindAccountFragment)
        }

        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
            rightImageView.visibility = View.GONE
            editText.setHint(ResUtils.getResId(requireActivity(), "yyxx_hint_bind_account_uname", "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_bind_account_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())
            }
        }

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

    private fun callBindAccount() {
        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
        }

        try {
            with(JSONObject()) {
                put("uid", SdkBackLoginInfo.instance.userId)
                put("login_type", SdkBackLoginInfo.instance.loginType)
                put("uname", userName)
                put("pwd", pwd)
                SdkRequest.instance.bindAccount(requireActivity(), this, object : IRequestCallback {
                    override fun onResponse(resultInfo: ResultInfo) {
                        if (resultInfo.code == 1) {
                            SdkBackLoginInfo.instance.hasBindAccount = true
                            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 -> callBindAccount()
            }
        }
    }
}