package com.xhly.manageapp.ui.login.activity

import BaseActivity
import android.graphics.Color
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.activity.addCallback
import com.kongzue.dialogx.dialogs.CustomDialog
import com.kongzue.dialogx.interfaces.OnBindView
import com.xhly.corelib.Const
import com.xhly.manageapp.R
import com.xhly.manageapp.databinding.ActivityLoginBinding
import com.xhly.manageapp.ui.login.viewmodel.LoginViewModel
import com.xhly.manageapp.ui.main.activity.MainActivity

class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {
    private var regioncode = ""
    override fun getBinding() = ActivityLoginBinding.inflate(layoutInflater)

    override fun initData() {

    }

    override fun initView() {
        setSoftInput(mBinding.root)
        mBinding.loginBtn.setOnClickListener {
            login()
        }
        mBinding.codeLoginIv.setOnClickListener {
            showEditDialog()
        }
        onBackPressedDispatcher.addCallback {
            //屏蔽返回
        }
    }

    private fun login() {
        val account = mBinding.accountEt.text.toString()
        val code = mBinding.codeEt.text.toString()
        if (account.isNullOrBlank()) {
            Toast(getString(R.string.accountnoempty))
            return
        }
        if (code.isNullOrBlank()) {
            Toast(getString(R.string.codenoempty))
            return
        }
        viewModel.getTest()
        spUtils.setParam("user", "user")
        startIntentActivity(MainActivity().javaClass)
        finish()
    }

    private fun showEditDialog() {
        CustomDialog
            .build()
            .setCustomView(object : OnBindView<CustomDialog>(R.layout.layout_edit_dialog) {
                override fun onBind(dialog: CustomDialog, v: View) {
                    val enterTv = v.findViewById<TextView>(R.id.dialog_enter_tv)
                    val cancelTv = v.findViewById<TextView>(R.id.dialog_cancel_tv)
                    val editText = v.findViewById<EditText>(R.id.dialog_et)
                    enterTv.setOnClickListener {
                        dialog.dismiss()
                        val text = editText.text
                        val list= arrayListOf("com.xhly.manageapp")
                        spUtils.saveJson(Const.WHITELISTAPP,list)
                        regioncode = if (text != null && text.isNotEmpty()) {
                            text.toString()
                        } else {
                            ""
                        }
                    }
                    cancelTv.setOnClickListener {
                        dialog.dismiss()
                    }
                }
            }).setCancelable(false).setMaskColor(Color.parseColor("#66000000"))
            .setAlign(CustomDialog.ALIGN.CENTER).show()
    }
}