package com.yidian.shenghuoquan.bmanagercenter.ui.center

import android.os.Bundle
import com.yidian.bcommon.constant.XEventConfig
import com.yidian.bcommon.constant.XRouterPathConstants
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.ActivityLifeAccountModifyNameBinding
import com.yidian.shenghuoquan.bmanagercenter.http.ApiService
import com.yidian.bcommon.utils.InputFilterUtil
import com.yidian.shenghuoquan.bmanagercenter.widget.CommonTopBarView
import com.yidian.shenghuoquan.bmanagercenter.widget.LifeAccountAuthIdentityInfoEditView
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.xarc.xevent.XEventManager
import com.yidian.xpage.XPageManager

/**
 * author: yinjiacheng
 * date: 6/14/21 12:55 AM
 * description: 生活号名称修改
 */
class LifeAccountModifyNameActivity : BaseActivity<ActivityLifeAccountModifyNameBinding>(),
  CommonTopBarView.OnCommonTopBarActionCallback,
  LifeAccountAuthIdentityInfoEditView.OnLifeAccountAuthIdentityInfoEditViewCallback,
  ICreateLifeAccountCallback, InputFilterUtil.OnLengthFilterCallback {

  companion object {
    const val EXTRA_LIFE_ACCOUNT_ID = "life_account_id"
    const val EXTRA_LIFE_ACCOUNT_NAME = "life_account_name"
  }

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

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

  override fun getXPageName(): String {
    return XRouterPathConstants.LIFE_ACCOUNT_MODIFY_NAME
  }

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

  private fun initView() {
    // 默认保存按钮不可用
    viewBind.bvTopBar.setActionEnable(false)
    // 生活号名称长度限制24个字符 禁止输入Emoji
    viewBind.evLifeAccountName.setInputFilters(
      InputFilterUtil.getLengthFilter(Constant.MAX_LENGTH_LIFE_ACCOUNT_NAME, this),
      InputFilterUtil.getEmojiFilter()
    )
  }

  private fun initListener() {
    viewBind.bvTopBar.setOnCommonTopBarCallback(null, this)
    viewBind.evLifeAccountName.setOnLifeAccountAuthIdentityInfoEditViewCallback(this)
  }

  private fun initData() {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { params ->
      (params as HashMap<*, *>).let {
        lifeAccountId = it[EXTRA_LIFE_ACCOUNT_ID] as String
        lifeAccountName = it[EXTRA_LIFE_ACCOUNT_NAME] as String
      }
    }
    viewBind.evLifeAccountName.fillEditContent(lifeAccountName)
    // 获取焦点弹出软键盘
    viewBind.evLifeAccountName.requestEditTextFocus()
  }

  override fun onDoAction() {
    // 请求接口检查并修改生活号名称
    ApiService.createLifeAccount(
      this,
      hashMapOf(
        Pair(EXTRA_LIFE_ACCOUNT_ID, lifeAccountId.toString()),
        Pair(EXTRA_LIFE_ACCOUNT_NAME, viewBind.evLifeAccountName.getEditContent())
      )
    )
  }

  override fun onTextClear(from: LifeAccountAuthIdentityInfoEditView) {
    viewBind.bvTopBar.setActionEnable(false)
  }

  override fun onTextChange(from: LifeAccountAuthIdentityInfoEditView, text: String) {
    viewBind.bvTopBar.setActionEnable(true)
  }

  override fun createLifeAccountCallback(result: Boolean) {
    if (result) {
      // 生活号名称修改成功 发送事件通知商户基本信息页面和商户管理中心页面
      XEventManager.post(XBaseEvent(XEventConfig.LIFE_ACCOUNT_NAME_MODIFY, viewBind.evLifeAccountName.getEditContent()))
      XPageManager.pop(null)
//    } else {
//      // 生活号名称修改失败 提示
//      viewBind.evLifeAccountName.showErrorTips(resources.getString(R.string.life_account_name_already_exist))
    }
  }

  override fun onLengthOverrun(maxLength: Int) {
    // 输入长度超限 展示错误提示
    viewBind.evLifeAccountName.showErrorTips(resources.getString(R.string.life_account_name_overrun_error_tips))
  }
}
