package com.casic.detector.view

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.amap.api.navi.NaviSetting
import com.casic.detector.databinding.ActivityLoginBinding
import com.casic.detector.utils.LocaleConstant
import com.casic.detector.vm.UserViewModel
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.utils.LoadState
import com.pengxh.kt.lite.utils.LoadingDialogHub
import com.pengxh.kt.lite.utils.SaveKeyValues
import com.pengxh.kt.lite.widget.dialog.AlertMessageDialog

class LoginActivity : KotlinBaseActivity<ActivityLoginBinding>() {

    private val kTag = "LoginActivity"
    private lateinit var userViewModel: UserViewModel

    override fun initOnCreate(savedInstanceState: Bundle?) {
        //先把导航隐私政策声明，后面导航会用到
        NaviSetting.updatePrivacyShow(this, true, true)
        NaviSetting.updatePrivacyAgree(this, true)

        binding.autoSavePasswordView.isChecked =
            SaveKeyValues.getValue(LocaleConstant.AUTO_SAVE, false) as Boolean

        userViewModel = ViewModelProvider(this)[UserViewModel::class.java]
        userViewModel.loginResult.observe(this) {
            if (it.isSuccess) {
                val data = it.data
                //保存基本信息
                SaveKeyValues.putValue(LocaleConstant.USER_NAME, data.userName)
                SaveKeyValues.putValue(LocaleConstant.USER_ID, data.id)
                SaveKeyValues.putValue(LocaleConstant.USER_COMPANY_ID, data.companyId)

                navigatePageTo<MainActivity>()
                finish()
            }
        }
    }

    override fun initEvent() {
        binding.loginButton.setOnClickListener {
            val account = binding.accountView.text.toString()
            val password = binding.passwordView.text.toString()
            val serversIp = binding.serversIpView.text.toString()
            val serversPort = binding.serversPortView.text.toString()
            if (account.isBlank() || password.isBlank() || serversIp.isBlank() || serversPort.isBlank()) {
                AlertMessageDialog.Builder()
                    .setContext(this)
                    .setTitle("账号登录")
                    .setMessage("信息不能为空哦！")
                    .setPositiveButton("知道了")
                    .setOnDialogButtonClickListener(object :
                        AlertMessageDialog.OnDialogButtonClickListener {
                        override fun onConfirmClick() {

                        }
                    }).build().show()
            } else {
                SaveKeyValues.putValue(LocaleConstant.USER_ACCOUNT, account)
                SaveKeyValues.putValue(LocaleConstant.USER_PASSWORD, password)
                SaveKeyValues.putValue(LocaleConstant.SERVER_IP, serversIp)
                SaveKeyValues.putValue(LocaleConstant.SERVER_PORT, serversPort)

                //登陆
                userViewModel.login(account, password)
            }
        }

        binding.autoSavePasswordView.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                SaveKeyValues.putValue(LocaleConstant.AUTO_SAVE, true)
            } else {
                SaveKeyValues.putValue(LocaleConstant.AUTO_SAVE, false)
            }
        }
    }

    override fun initViewBinding(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(layoutInflater)
    }

    override fun observeRequestState() {
        userViewModel.loadState.observe(this) {
            when (it) {
                LoadState.Loading -> LoadingDialogHub.show(this, "登录中，请稍后")
                else -> LoadingDialogHub.dismiss()
            }
        }
    }

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
    }

    override fun onResume() {
        super.onResume()
        if (binding.autoSavePasswordView.isChecked) {
            binding.accountView.setText(
                SaveKeyValues.getValue(LocaleConstant.USER_ACCOUNT, "") as String
            )
            binding.passwordView.setText(
                SaveKeyValues.getValue(LocaleConstant.USER_PASSWORD, "") as String
            )
            binding.serversIpView.setText(
                SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String
            )
            binding.serversPortView.setText(
                SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String
            )
        }
    }
}