package com.yidian.shenghuoquan.newscontent.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.HawkConfig
import com.yidian.bcommon.XRouterPathConstants
import com.yidian.bcommon.XRouterPathConstants.Companion.LIFE_ACCOUNT_CREATE
import com.yidian.bcommon.base.BaseActivity
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.bean.ICreateLifeAccountCallback
import com.yidian.shenghuoquan.newscontent.constant.Constant
import com.yidian.shenghuoquan.newscontent.databinding.ActivityLifeAccountCreateBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.utils.InputFilterUtil
import com.yidian.shenghuoquan.newscontent.utils.SensitiveInfoUtil
import com.yidian.shenghuoquan.newscontent.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)
  }

}
