package com.yidian.shenghuoquan.newscontent.ui.store

import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.yidian.common.XEventConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.base.BaseActivity
import com.yidian.common.extensions.clickAntiShake
import com.yidian.common.extensions.hide
import com.yidian.common.extensions.show
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.adapter.ChooseStoreApter
import com.yidian.shenghuoquan.newscontent.databinding.ActivityChooseStoreBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.utils.StorageUtil
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.xarc.xevent.XEventManager
import com.yidian.xpage.XPageManager


/**
 * 选择门店
 */
class ChooseStoreActivity : BaseActivity<ActivityChooseStoreBinding>() {

  private var lastSelectStoreList: List<String>? = null
  private val chooseStoreAdapter: ChooseStoreApter by lazy { ChooseStoreApter() }

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

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

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    val serializableExtra = intent.getSerializableExtra(XRouterPathConstants.ParamsKey)
    serializableExtra?.let {
      val paramsMap = serializableExtra as HashMap<*, *>
      lastSelectStoreList = paramsMap[lastSelectStoreKey] as List<String>?
    }
    initTitle()
    initClick()
    initList()
  }

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

  private fun requestStoreList() {
    val requestParams = HashMap<String, String?>()
    requestParams["life_account_id"] = StorageUtil.getLifeAccountId()
    ApiService.getShopList(requestParams) { shopList ->
      if (shopList.isNullOrEmpty()) {
        viewBind.rvStore.hide()
        viewBind.pvEmptyPage.show()
        return@getShopList
      } else {
        viewBind.rvStore.show()
        viewBind.pvEmptyPage.hide()
      }
      // 勾选上次选中的门店的
      if (!lastSelectStoreList.isNullOrEmpty()) {
        shopList.forEach { shopListBean ->
          shopListBean.isSelected = lastSelectStoreList!!.contains(shopListBean.sub_shop_id)
        }
      }
      chooseStoreAdapter.setList(shopList)
    }
  }

  private fun initList() {
    viewBind.rvStore.adapter = chooseStoreAdapter
  }

  private fun initClick() {
    // 确认选中的门店
    viewBind.tvSave.clickAntiShake {
      val selectStoreIdList = mutableListOf<String>()
      // 过滤门店列表，筛选选中的门店
      val storeList = chooseStoreAdapter.data
      val filterList = storeList.filter { it.isSelected }
      filterList.forEach {
        selectStoreIdList.add(it.sub_shop_id)
      }
      // 关闭选择类目页面
      XEventManager.post(XBaseEvent(XEventConfig.CHOOSE_STORE_SUCCESS, selectStoreIdList))
      finish()
    }
  }

  private fun initTitle() {
    viewBind.include.tvTitle.text = "选择门店"
    viewBind.include.ivBack.clickAntiShake {
      XPageManager.pop(null)
    }
    viewBind.include.tvMenu.text = "新增门店"
    viewBind.include.tvMenu.setTextColor(ContextCompat.getColor(this, R.color.color_1852F1))
    viewBind.include.tvMenu.clickAntiShake {
      val intent = Intent(this, EditStoreActivity::class.java)
      intent.putExtra("from", EditStoreActivity.fromAddStore)
      startActivity(intent)
    }
  }

  companion object {
    const val lastSelectStoreKey = "lastSelectStore"
  }

}
