package com.yidian.shenghuoquan.newscontent.ui.auth

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import androidx.core.view.isVisible
import com.orhanobut.hawk.Hawk
import com.yidian.common.HawkConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.XRouterPathConstants.Companion.LIFE_ACCOUNT_CREATE
import com.yidian.common.base.BaseActivity
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.bean.ICreateLifeAccountCallback
import com.yidian.shenghuoquan.newscontent.databinding.ActivityLifeAccountCreateBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.utils.TextWatcherAdapter
import com.yidian.utils.ToastUtil
import com.yidian.xpage.XPageManager

class LifeAccountCreateActivity : BaseActivity<ActivityLifeAccountCreateBinding>(),
  ICreateLifeAccountCallback {
  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 val accountMaxLength = 20
  private var lifeAccountId: String? = null
  private var lifeAccountName: String = ""

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

  private fun initData() {
    val params = intent.getSerializableExtra(XRouterPathConstants.ParamsKey)
    if (params != null && params is HashMap<*, *>) {
      lifeAccountId = params[LIFE_ACCOUNT_ID] as String
    }
    // 设置管理员信息
    val name = Hawk.get(HawkConfig.Nickname, "")
    val phone = Hawk.get(HawkConfig.Mobile, "")
    setUserName(name, phone)

  }

  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())
      }
    })
    viewBind.btnCreateAccount.setOnClickListener {
      createLifeAccount()
    }
  }

  /**
   * 请求接口创建生活号
   */
  private fun createLifeAccount() {
    lifeAccountName = viewBind.etAccount.text.toString()
    if (lifeAccountName.isEmpty()) {
      ToastUtil.showToast(this, "生活号名称不能为空")
      return
    }
    ApiService.createLifeAccount(
      this,
      hashMapOf(
        Pair(LIFE_ACCOUNT_ID, lifeAccountId.toString()),
        Pair(LIFE_ACCOUNT_NAME, lifeAccountName)
      )
    )

  }

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

  private fun formatNumber(number: String): String {
    return number.replace("(\\d{3})\\d{4}(\\d{4})".toRegex(), "$1****$2")
  }

  @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)
      return
    }
    if (s.length > accountMaxLength) {
      viewBind.btnCreateAccount.alpha = 0.32f
      viewBind.btnCreateAccount.isEnabled = false
      viewBind.tvErrorTips.isVisible = true
      viewBind.etAccount.setTextColor(resources.getColor(R.color.red_account_tips))
      viewBind.vDivider.setBackgroundResource(R.color.red_account_tips)
    } 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)
        )
      )
    } else {
      ToastUtil.showToast(this, "生活号创建失败")
    }
  }

}
