package com.yidian.shenghuoquan.newscontent.ui.dialog

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.common.base.BaseDialog
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.adapter.BottomSelectAdapter
import com.yidian.shenghuoquan.newscontent.bean.BottomSelectBean
import com.yidian.shenghuoquan.newscontent.databinding.DialogBottomSelectBinding

/**
 * author: yinjiacheng
 * date: 5/29/21 11:44 AM
 * description: 底部选择dialog
 */
class BottomSelectDialog<T : BottomSelectBean> @JvmOverloads constructor(
  context: Context,
  val listener: BottomSelectAdapter.OnItemClickListener<T>? = null,
  val data: ArrayList<T>? = null
) :
  BaseDialog<DialogBottomSelectBinding>(context), View.OnClickListener,
  BottomSelectAdapter.OnItemClickListener<T> {

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

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

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

  override fun onClick(v: View?) {
    if (v?.id == R.id.tv_cancel) {
      dismiss()
    }
  }

  override fun show() {
    super.show()
    window?.setGravity(Gravity.BOTTOM)
    val params = window?.attributes
    params?.width = WindowManager.LayoutParams.MATCH_PARENT
    params?.height = WindowManager.LayoutParams.WRAP_CONTENT
    window?.attributes = params
  }

  override fun onItemClick(view: View, position: Int, data: T) {
    dismiss()
    listener?.onItemClick(view, position, data)
  }
}
