package com.yidian.shenghuoquan.newscontent.personnel.ui

import android.os.Bundle
import androidx.core.content.ContextCompat
import com.orhanobut.hawk.Hawk
import com.yidian.common.HawkConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.XRouterPathConstants.Companion.PERSONAL_MY_STAFF
import com.yidian.common.base.BaseActivity
import com.yidian.common.extensions.clickAntiShake
import com.yidian.common.extensions.initTitleBar
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.databinding.ActivityMyStaffBinding
import com.yidian.shenghuoquan.newscontent.personnel.adapter.MyStaffListAdapter
import com.yidian.shenghuoquan.newscontent.personnel.bean.AccountAdmin
import com.yidian.shenghuoquan.newscontent.personnel.bean.AccountListDto
import com.yidian.shenghuoquan.newscontent.personnel.bean.AccountUser
import com.yidian.shenghuoquan.newscontent.personnel.http.callback.IPersonalAccountListCallback
import com.yidian.shenghuoquan.newscontent.personnel.http.PersonalRemoteDataSource
import com.yidian.shenghuoquan.newscontent.utils.StorageUtil
import com.yidian.xpage.XPageManager

/**
 * 人员管理 —— 我的员工页面
 */
class MyStaffActivity : BaseActivity<ActivityMyStaffBinding>(), IPersonalAccountListCallback {

  private var managerInfo: AccountAdmin? = null

  private val myStaffAdapter: MyStaffListAdapter by lazy { MyStaffListAdapter() }

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

  override fun getXPageName(): String {
    return PERSONAL_MY_STAFF
  }

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

  override fun onResume() {
    super.onResume()
    requestAccountList()
  }

  private fun requestAccountList() {
    val lifeAccountId = StorageUtil.getLifeAccountId()
    if (!lifeAccountId.isNullOrBlank()) {
      val paramsMap = HashMap<String, String?>()
      paramsMap["life_account_id"] = lifeAccountId
      PersonalRemoteDataSource.getAccountList(this, paramsMap)
    }
  }

  private fun initView() {
    initTitleBar(viewBind.include.toolbar, viewBind.include.tvTitle, resources.getString(R.string.my_staff))
    viewBind.include.toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.color_FFF5F6F7))
    viewBind.rvStaffList.adapter = myStaffAdapter
  }

  private fun initClick() {
    // 管理员信息
//    viewBind.clManager.clickAntiShake {
//      managerInfo?.let {
//        val hashMap = HashMap<String, Any?>()
//        hashMap["managerInfo"] = it
//        XPageManager.push(XRouterPathConstants.PERSONAL_MANAGER_INFO, hashMap)
//      }
//    }
    // 更新管理员
//    viewBind.tvChange.clickAntiShake {
//      managerInfo?.let {
//        val hashMap = HashMap<String, String?>()
//        hashMap["mobile"] = it.mobile
//        XPageManager.push(XRouterPathConstants.PERSONAL_MANAGER_CHANGE, hashMap)
//      }
//    }
    // 点击添加
    viewBind.tvAddStaff.clickAntiShake {
      XPageManager.push(XRouterPathConstants.PERSONAL_ADD_STAFF, null)
    }
    // 点击员工信息
    myStaffAdapter.setOnItemClickListener { adapter, _, position ->
      val user = adapter.data[position] as AccountUser
      val hashMap = HashMap<String, Any?>()
      hashMap["staffInfo"] = user
      XPageManager.push(XRouterPathConstants.PERSONAL_STAFF_INFO, hashMap)
    }
  }

  override fun getAccountListSuccess(response: AccountListDto?) {
    response?.let {
      it.admin?.get(0)?.let { admin ->
        managerInfo = admin
        initManager(admin)
      }
      myStaffAdapter.setList(it.users)
    }
  }

  private fun initManager(admin: AccountAdmin) {
    viewBind.tvName.text = admin.nickName
    viewBind.tvMobile.text = admin.mobile
    val sb = StringBuilder()
    admin.roleList?.forEachIndexed { index, s ->
      sb.append(s)
      if (index < admin.roleList.size - 1) {
        sb.append("｜")
      }
    }
    viewBind.tvRole.text = sb
  }

  override fun getAccountListFailure(message: String?) {

  }

}
