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

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.yidian.common.base.BaseDialog
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.adapter.MerchantSwitchAdapter
import com.yidian.shenghuoquan.newscontent.databinding.DialogMerchantSwitchBinding
import com.yidian.shenghuoquan.newscontent.http.httpbean.LifeAccountItemBean
import com.yidian.shenghuoquan.newscontent.utils.StorageUtil
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)
  }

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

  override fun onItemClick(view: View, position: Int, data: LifeAccountItemBean.Response) {
    // 选择某个生活号 弹窗消失
    callback?.onLifeAccountSelect(data)
    // 更新当前生活号id
    StorageUtil.putLifeAccountId(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() - DensityUtil.dp2px(42f)
    params?.y = DensityUtil.dp2px(42f)
    window?.attributes = params
  }

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

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