package com.yidian.shenghuoquan.bmanagercenter.ui.auth

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.orhanobut.hawk.Hawk
import com.yidian.bcommon.constant.HawkConfig
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.constant.XRouterPathConstants.Companion.LIFE_ACCOUNT_CREATE
import com.yidian.bcommon.base.BaseActivity
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.http.callback.ICreateLifeAccountCallback
import com.yidian.shenghuoquan.bmanagercenter.constant.Constant
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityLifeAccountCreateBinding
import com.yidian.shenghuoquan.bmanagercenter.http.ApiService
import com.yidian.bcommon.utils.InputFilterUtil
import com.yidian.bcommon.utils.SensitiveInfoUtil
import com.yidian.bcommon.utils.TextWatcherAdapter
import com.yidian.utils.ToastUtil
import com.yidian.xpage.XPageManager

class LifeAccountCreateActivity : BaseActivity<ActivityLifeAccountCreateBinding>(),
  ICreateLifeAccountCallback, InputFilterUtil.OnLengthFilterCallback {
  companion object {
    private const val TAG = "LifeAccountCreate"
    const val LIFE_ACCOUNT_ID = "life_account_id"
    const val LIFE_ACCOUNT_NAME = "life_account_name"
  }

  override fun createViewBinding(): ActivityLifeAccountCreateBinding {
    return ActivityLifeAccountCreateBinding.inflate(layoutInflater)
  }

  override fun getXPageName(): String {
    return LIFE_ACCOUNT_CREATE
  }

  private var lifeAccountId: String? = null
  private var lifeAccountName: String? = null

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initView()
    initListener()
    initData()
  }

  private fun initData() {
    lifeAccountId = intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { params ->
      (params as HashMap<*, *>)[LIFE_ACCOUNT_ID]?.let {
        it as String
      }
    }
    // 设置管理员信息
    setUserName(Hawk.get(HawkConfig.Nickname, ""), Hawk.get(HawkConfig.Mobile, ""))
  }

  private fun initView() {
    viewBind.btnCreateAccount.alpha = 0.32f
    viewBind.btnCreateAccount.isEnabled = false
  }

  private fun initListener() {
    viewBind.ivBack.setOnClickListener { XPageManager.pop(null) }
    viewBind.etAccount.addTextChangedListener(object : TextWatcherAdapter() {
      override fun afterTextChanged(s: Editable?) {
        if (s == null) return
        checkAccount(s.toString())
      }
    })
    // 生活号名称长度限制24个字符 禁止输入Emoji
    viewBind.etAccount.filters =
      arrayOf(InputFilterUtil.getLengthFilter(Constant.MAX_LENGTH_LIFE_ACCOUNT_NAME, this), InputFilterUtil.getEmojiFilter())
    viewBind.btnCreateAccount.setOnClickListener {
      createLifeAccount()
    }
  }

  /**
   * 请求接口创建生活号
   */
  private fun createLifeAccount() {
    lifeAccountName = viewBind.etAccount.text.toString()
    if (lifeAccountName.isNullOrEmpty() || lifeAccountId.isNullOrEmpty()) {
      return
    }
    ApiService.createLifeAccount(
      this,
      hashMapOf(
        Pair(LIFE_ACCOUNT_ID, lifeAccountId), Pair(LIFE_ACCOUNT_NAME, lifeAccountName)
      )
    )

  }

  @SuppressLint("SetTextI18n")
  private fun setUserName(name: String, number: String) {
    val numberFormat = SensitiveInfoUtil.mobileEncrypt(number)
    viewBind.tvUserName.text = "$name $numberFormat"
  }

  @Suppress("DEPRECATION")
  private fun checkAccount(s: String) {
    if (s.isEmpty()) {
      viewBind.btnCreateAccount.alpha = 0.32f
      viewBind.btnCreateAccount.isEnabled = false
      viewBind.tvErrorTips.isVisible = false
      viewBind.vDivider.setBackgroundResource(R.color.divider_white)
    } else {
      viewBind.btnCreateAccount.alpha = 1f
      viewBind.btnCreateAccount.isEnabled = true
      viewBind.tvErrorTips.isVisible = false
      viewBind.etAccount.setTextColor(resources.getColor(R.color.gray_333))
      viewBind.vDivider.setBackgroundResource(R.color.divider_white)
    }
  }

  override fun createLifeAccountCallback(result: Boolean) {
    //创建生活号的回调
    if (result) {
      XPageManager.push(
        XRouterPathConstants.LIFE_ACCOUNT_CREATE_COMPLETE,
        hashMapOf(
          Pair(LifeAccountCreateCompleteActivity.EXTRA_LIFE_ACCOUNT_NAME, lifeAccountName),
          Pair(LifeAccountCreateCompleteActivity.EXTRA_LIFE_ACCOUNT_ID, lifeAccountId)
        )
      )
      XPageManager.pop(null)
    } else {
      ToastUtil.showToast(this, "生活号创建失败")
    }
  }

  override fun onLengthOverrun(maxLength: Int) {
    // 输入长度超限 展示错误提示
    viewBind.tvErrorTips.isVisible = true
    viewBind.etAccount.setTextColor(ContextCompat.getColor(this, R.color.red_account_tips))
    viewBind.vDivider.setBackgroundResource(R.color.red_account_tips)
  }

}
