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

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.yidian.bcommon.base.BaseDialog
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.adapter.MerchantSwitchAdapter
import com.yidian.shenghuoquan.bmanagercenter.databinding.DialogMerchantSwitchBinding
import com.yidian.bcommon.bean.LifeAccountItemBean
import com.yidian.shenghuoquan.bmanagercenter.manager.LifeAccountManager
import com.yidian.utils.DensityUtil

/**
 * author: yinjiacheng
 * date: 6/15/21 11:33 PM
 * description: 商户管理中心-切换主体
 */
class MerchantSwitchDialog(
    context: Context,
    private val curLifeAccountId: String,
    val data: List<LifeAccountItemBean.Response>,
    val callback: OnMerchantSwitchCallback? = null
) : BaseDialog<DialogMerchantSwitchBinding>(context, R.style.merchant_switch_dialog),
  MerchantSwitchAdapter.OnItemClickListener, View.OnClickListener {

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

  override fun initView() {
    viewBinding.rvLifeAccount.layoutManager = LinearLayoutManager(context)
    viewBinding.rvLifeAccount.adapter = MerchantSwitchAdapter(curLifeAccountId, data, this)
    // 分割线
    val decoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
    ContextCompat.getDrawable(context, R.drawable.shape_recycler_view_item_divider)?.let { decoration.setDrawable(it) }
    viewBinding.rvLifeAccount.addItemDecoration(decoration)
  }

  override fun initListener() {
    viewBinding.root.setOnClickListener(this)
  }

  override fun onItemClick(view: View, position: Int, data: LifeAccountItemBean.Response) {
    // 选择某个生活号 弹窗消失
    callback?.onLifeAccountSelect(data)
    // 更新当前生活号id
    LifeAccountManager.saveLifeAccountId(data.life_account_id)
    dismiss()
  }

  override fun show() {
    super.show()
    window?.setGravity(Gravity.TOP)
    val params = window?.attributes
    params?.width = WindowManager.LayoutParams.MATCH_PARENT
    params?.height = DensityUtil.getScreenHeight()
    params?.y = DensityUtil.dp2px(44f)
    window?.attributes = params
    // 隐藏导航栏
    /*window?.decorView?.systemUiVisibility =
      View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION*/
  }

  override fun onClick(v: View?) {
    dismiss()
  }

  interface OnMerchantSwitchCallback {
    /**
     * 选择某个生活号
     * @param data 生活号信息
     */
    fun onLifeAccountSelect(data: LifeAccountItemBean.Response)
  }
}
