package com.abbidot.tracker.ui.activity.account.cn

import android.content.Intent
import android.graphics.Paint
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityLoginV2CnBinding
import com.abbidot.tracker.ui.activity.set.FirstPermissionsActivity
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.cn.LoginCNViewModel

class LoginV2CNActivity :
    BaseActivity<ActivityLoginV2CnBinding>(ActivityLoginV2CnBinding::inflate) {

    private val mLoginCNViewModel: LoginCNViewModel by viewModels()

    private var isShowPassword = false
    private var isCurrentPostEven = false

    override fun getTopBar() = mViewBinding.loginV2CnTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle("")
        mViewBinding.apply {
            root.setBackgroundResource(R.drawable.icon_login_v2_bg)

            ilLoginV2CnPhone.let {
                it.tvInputNameTitle.setText(R.string.login_txt_phone)
                it.etInputContent.setHint(R.string.login_txt_phone)
                it.etInputContent.inputType = InputType.TYPE_CLASS_NUMBER
                val filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
                it.etInputContent.filters = filters
                ViewUtil.instance.setInputViewErrorHideClick(
                    mContext, it.rlEtInputLayout, tvPhoneErrorTipLoginCn, it.etInputContent
                )
            }
            ilLoginV2CnPassword.let {
                it.tvInputNameTitle.setText(R.string.login_txt_password)
                it.etInputContent.setHint(R.string.login_txt_password)
                ViewUtil.instance.setShowAndHidePassword(
                    it.etInputContent, it.ivInputRepresentImage
                )
                val filters = arrayOf<InputFilter>(InputFilter.LengthFilter(60))
                it.etInputContent.filters = filters
                ViewUtil.instance.setInputViewErrorHideClick(
                    mContext, it.rlEtInputLayout, tvPasswordErrorTipLoginCn, it.etInputContent
                )
            }
            //添加下划线
            tvLoginV2CnForgotPassword.paintFlags = Paint.UNDERLINE_TEXT_FLAG

            setOnClickListenerViews(
                tvLoginV2CnForgotPassword, btnLoginV2CnLogin,
                ilLoginV2CnPassword.ivInputRepresentImage,
                btnLoginV2CnLogin, btnLoginV2CnLoginCode
            )
        }
    }

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.FinishActivity) {
            if (!isCurrentPostEven) finish()
        }
        mLoginCNViewModel.mAccountPwdLoginResultLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.apply {
                        isCurrentPostEven = true
                        Util.dealLoginSuccessData(mContext, this)

                        showToast(R.string.txt_login_successful, gravity = Gravity.CENTER)
                        loginSuccessGo()
                    }
                }

                override fun onRequestError(exceptionCode: String?) {
                    setButtonEnabled(mViewBinding.btnLoginV2CnLogin, ConstantInt.Type1)
                }

                override fun onInterceptCode() {
                    setButtonEnabled(mViewBinding.btnLoginV2CnLogin, ConstantInt.Type1)
                }
            })
        }
    }

    private fun loginSuccessGo() {
        val hasPermissions = Util.checkNeedPermissionsEnabled(mContext)
        mViewBinding.btnLoginV2CnLogin.postDelayed({
            if (hasPermissions) {
                startActivityFinish(Util.checkBindDeviceAndPetGoActivity(mContext))
            } else {
                startActivityFinish(Intent(mContext, FirstPermissionsActivity::class.java))
            }
        }, 1000)
    }

    private fun goForgotPassword() {
        Intent(mContext, ForgotPasswordCNActivity::class.java).apply {
//            val email = mViewBinding.ilLoginV2EmailAddress.etInputContent.text.toString()
//            putExtra(MMKVKey.Email, email)
            startActivity(this)
        }
    }

    private fun loginClick() {
        mViewBinding.apply {
            var phone = ilLoginV2CnPhone.etInputContent.text.toString()
            var password = ilLoginV2CnPassword.etInputContent.text.toString()
            var isSure = true

            if (AppUtils.isDebug()) {
                if (TextUtils.equals("12", phone)) {
                    phone = "13049437653"
                    password = "123456"
                }
            }
            if (TextUtils.isEmpty(phone) || phone.length < 11) {
                isSure = false
                ViewUtil.instance.setInputViewErrorShow(
                    mContext, ilLoginV2CnPhone.rlEtInputLayout, tvPhoneErrorTipLoginCn
                )
            }
            if (TextUtils.isEmpty(password)) {
                isSure = false
                ViewUtil.instance.setInputViewErrorShow(
                    mContext, ilLoginV2CnPassword.rlEtInputLayout, tvPasswordErrorTipLoginCn
                )
            }
            if (!isSure) return

            setButtonEnabled(btnLoginV2CnLogin, ConstantInt.Type0)
            password = Utils.get32Md5Value(password)

            mLoginCNViewModel.accountPasswordLogin(phone, password)
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnLoginV2CnLoginCode -> startActivity(
                    Intent(
                        mContext,
                        VerifyCodeLoginCNActivity::class.java
                    )
                )

                ilLoginV2CnPassword.ivInputRepresentImage -> {
                    isShowPassword = !isShowPassword
                    ViewUtil.instance.showAndHidePasswordClick(
                        ilLoginV2CnPassword.etInputContent,
                        ilLoginV2CnPassword.ivInputRepresentImage,
                        isShowPassword
                    )
                }

                tvLoginV2CnForgotPassword -> goForgotPassword()
                btnLoginV2CnLogin -> loginClick()
            }
        }
    }
}