package com.yidian.shenghuoquan.newscontent.ui.center

import android.os.Bundle
import android.text.InputFilter
import com.yidian.common.XEventConfig
import com.yidian.common.XRouterPathConstants
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.ActivityLifeAccountModifyNameBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.utils.InputFilterUtil
import com.yidian.shenghuoquan.newscontent.widget.CommonTopBarView
import com.yidian.shenghuoquan.newscontent.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 {

  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() {
    // 只允许输入中文并且最长20汉字
    viewBind.evLifeAccountName.setInputFilters(InputFilterUtil.getChineseFilter(), InputFilter.LengthFilter(20))
  }

  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) {

  }

  override fun onTextChange(from: LifeAccountAuthIdentityInfoEditView, text: String) {

  }

  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))
    }
  }
}
