package com.yidian.shenghuoquan.bmanagercenter.personnel.ui

import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.constant.XRouterPathConstants.Companion.PERSONAL_MANAGER_INFO
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.extensions.clickAntiShake
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityManagerInfoBinding
import com.yidian.shenghuoquan.bmanagercenter.personnel.bean.AccountAdmin
import com.yidian.bcommon.utils.SensitiveInfoUtil
import com.yidian.xpage.XPageManager

/**
 * 人员管理 —— 管理员信息
 */
class ManagerInfoActivity : BaseActivity<ActivityManagerInfoBinding>() {

  private var managerMobile: String = ""

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

  override fun getXPageName(): String {
    return PERSONAL_MANAGER_INFO
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initTitle()
    getIntentData(intent)
  }

  private fun initTitle() {
    viewBind.include.tvTitle.text = "管理员信息"
    viewBind.include.ivBack.setOnClickListener {
      XPageManager.pop(null)
    }
    viewBind.include.tvMenu.text = "更换"
    viewBind.include.tvMenu.clickAntiShake {
      val hashMap = HashMap<String, String?>()
      hashMap["mobile"] = managerMobile
      XPageManager.push(XRouterPathConstants.PERSONAL_MANAGER_CHANGE, hashMap)
    }
    viewBind.include.tvMenu.setTextColor(ContextCompat.getColor(this, R.color.color_1852F1))
  }

  private fun getIntentData(intent: Intent?) {
    val paramsMap = intent?.getSerializableExtra(XRouterPathConstants.ParamsKey)
    if (paramsMap != null) {
      paramsMap as HashMap<*, *>
      val managerInfo = paramsMap["managerInfo"] as AccountAdmin
      viewBind.tvUserName.text = managerInfo.nickName
      viewBind.tvLoginAccount.text = managerInfo.mobile?.let {
        managerMobile = it
        SensitiveInfoUtil.mobileEncrypt(it)
      }
      val sb = StringBuilder()
      managerInfo.roleList?.forEachIndexed { index, s ->
        sb.append(s)
        if (index < managerInfo.roleList.size - 1) {
          sb.append("｜")
        }
      }
      viewBind.tvCurrentRole.text = sb
    }
  }

}
