package cn.yyxx.eyuangame.core.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.*
import android.widget.*
import cn.yyxx.eyuangame.core.impl.SdkBridgeImpl
import cn.yyxx.eyuangame.core.ui.EventEditText
import cn.yyxx.support.DensityUtils
import cn.yyxx.support.ResUtils

/**
 * @author #Suyghur.
 * Created on 2021/06/23
 */
class ForgetPwdDialog(context: Context, private val isLandscape: Boolean, private val areaCodeList: MutableList<String>) :
    Dialog(context, ResUtils.getResId(context, "EYuanGameTheme", "style")) {

    lateinit var ivReturn: ImageView
    lateinit var eetAccount: EventEditText
        private set
    lateinit var tvArea: TextView
    lateinit var ivArrow: ImageView
    lateinit var eetPhone: EventEditText
        private set
    lateinit var btnConfirm: Button

    private lateinit var rlAreaList: RelativeLayout
    private lateinit var llAreaList: LinearLayout
    private lateinit var svAreaList: ScrollView

    private var imgUp = 0
    private var imgDown = 0

    var code = ""
        private set

    init {
        setCanceledOnTouchOutside(false)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView(context)
    }

    private fun initView(context: Context) {

//        if (SdkBridgeImpl.isLand) {
//            window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
//        }

        val view: View = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "yyxx_forget_pwd_dialog", "layout"), null)

        setContentView(view)


        val attr = window?.attributes as WindowManager.LayoutParams
        //设置dialog 在布局中的位置
        attr.height = ViewGroup.LayoutParams.WRAP_CONTENT
//        val w=DensityUtils.getHeightAndWidth(context)
        if (isLandscape) {
            attr.width = DensityUtils.getHeightAndWidth(context)[0] / 2
        } else {
            attr.width = DensityUtils.getHeightAndWidth(context)[0] - 80
        }
        attr.gravity = Gravity.CENTER

//        attr.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN or WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE

        imgUp = ResUtils.getResId(context, "yyxx_pack_up_white_img", "drawable")
        imgDown = ResUtils.getResId(context, "yyxx_pack_down_white_img", "drawable")

        ivReturn = view.findViewById(ResUtils.getResId(context, "yyxx_iv_return", "id"))
        ivReturn.setOnClickListener {
            dismiss()
        }
        eetAccount = view.findViewById(ResUtils.getResId(context, "yyxx_eet_account", "id"))
        eetAccount.leftImageView.setBackgroundResource(ResUtils.getResId(context, "yyxx_account_img", "drawable"))
        eetAccount.leftImageView.visibility = View.VISIBLE
        eetAccount.editText.setHint(ResUtils.getResId(context, "yyxx_hint_account", "string"))

        tvArea = view.findViewById(ResUtils.getResId(context, "yyxx_tv_area", "id"))
        ivArrow = view.findViewById(ResUtils.getResId(context, "yyxx_iv_arrow", "id"))
        ivArrow.setOnClickListener {
            changeAreaList()
        }

        eetPhone = view.findViewById(ResUtils.getResId(context, "yyxx_eet_phone", "id"))
        eetPhone.editText.setHint(ResUtils.getResId(context, "yyxx_hint_forget_phone", "string"))


        rlAreaList = view.findViewById(ResUtils.getResId(context, "yyxx_rl_area_list", "id"))
        rlAreaList.visibility = View.GONE
        llAreaList = view.findViewById(ResUtils.getResId(context, "yyxx_ll_area_list", "id"))
        svAreaList = view.findViewById(ResUtils.getResId(context, "yyxx_sv_area_list", "id"))

        btnConfirm = view.findViewById(ResUtils.getResId(context, "yyxx_btn_confirm", "id"))
        autoFillAreaList(context)
    }

    private fun autoFillAreaList(context: Context) {
        if (areaCodeList.size == 0) {
            //默认4个地区
            areaCodeList.add("86")
            //香港
            areaCodeList.add("852")
            //澳门
            areaCodeList.add("853")
            //台湾
            areaCodeList.add("886")
        }
        initAreaList(context)
    }

    private fun initAreaList(context: Context) {
        llAreaList.removeAllViews()
        //单行高
        var height = 100
        tvArea.apply {
            height = if (layoutParams != null) {
                layoutParams!!.height
            } else {
                100
            }
        }
        //ScrollView高度
        var maxHeight = 0
        areaCodeList.apply {
            maxHeight = if (size < 2) {
                size * height
            } else {
                2 * height
            }
        }
        //循环添加列
        areaCodeList.apply {
            for (i in indices) {
                val areaCode = this[i]
                //添加线
                val line = View(context)
                line.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2)
                line.setBackgroundColor(Color.parseColor("#E6FFFFFF"))
                llAreaList.apply {
                    if (childCount > 0) {
                        addView(line)
                    }
                }
                //添加区号
                val item = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "yyxx_area_list_item", "layout"), null)
                item?.apply {
                    val tvAreaCode = findViewById<TextView>(ResUtils.getResId(context, "yyxx_tv_area", "id"))
                    tvAreaCode.gravity = Gravity.CENTER
                    tvAreaCode.text = areaCode
                    setOnClickListener(OnItemClick(areaCode))
                    layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height)
                    llAreaList.addView(this, llAreaList.childCount)
                }
            }
        }
        svAreaList.layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, maxHeight)
    }

    private fun changeAreaList() {
        if (rlAreaList.visibility == View.GONE) {
            ivArrow.setBackgroundResource(imgUp)
            rlAreaList.visibility = View.VISIBLE
        } else {
            ivArrow.setBackgroundResource(imgDown)
            rlAreaList.visibility = View.GONE
        }
    }


    override fun dismiss() {
        super.dismiss()
        areaCodeList.clear()
        rlAreaList.visibility = View.GONE
    }

    /**
     * 账号列表item点击事件
     */
    private inner class OnItemClick(val areaCode: String) : View.OnClickListener {
        override fun onClick(v: View?) {
            tvArea.apply {
                changeAreaList()
                tvArea.text = areaCode
                code = areaCode
            }
        }
    }
}