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

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.*
import android.widget.Button
import android.widget.TextView
import cn.yyxx.support.ResUtils

/**
 * @author #Suyghur.
 * Created on 2021/06/18
 */
class AutoLoginDialog(context: Context) : Dialog(context), DialogInterface.OnKeyListener {

    lateinit var tvAccount: TextView
        private set
    lateinit var tvNum: TextView

    lateinit var btnSwitch: Button
        private set

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

    private fun initView(context: Context) {

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

        val attr = window?.attributes as WindowManager.LayoutParams
        //设置dialog 在布局中的位置
        attr.height = ViewGroup.LayoutParams.WRAP_CONTENT
        attr.width = ViewGroup.LayoutParams.WRAP_CONTENT
        attr.gravity = Gravity.CENTER

        tvAccount = view.findViewById(ResUtils.getResId(context, "yyxx_iv_account", "id"))
        tvNum = view.findViewById(ResUtils.getResId(context, "yyxx_iv_num", "id"))
        btnSwitch = view.findViewById(ResUtils.getResId(context, "yyxx_btn_switch", "id"))

        setOnKeyListener(this)
    }

    override fun onKey(dialog: DialogInterface?, keyCode: Int, event: KeyEvent): Boolean {
        return keyCode == KeyEvent.KEYCODE_BACK && event.repeatCount == 0
    }
}