package com.swago.room.gift

import android.graphics.Rect
import android.util.Log
import android.view.View
import androidx.appcompat.widget.ViewUtils
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.swago.baseswago.model.live.gift.GiftModel
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.LogUtil
import com.swago.room.R
import com.tencent.qcloud.tim.uikit.utils.ScreenUtil
import java.util.ArrayList

/**
 *@date 2021/12/13 20:20
 *description:
 */
class GiftPagerAdapter(private val type: Int, layoutResId: Int, data: List<ArrayList<GiftModel>>) :
    BaseQuickAdapter<ArrayList<GiftModel>, BaseViewHolder>(layoutResId, data) {

    private var selected = -1


    override fun convert(helper: BaseViewHolder, item: ArrayList<GiftModel>) {
        val recyclerView: RecyclerView =
            helper.itemView.findViewById(R.id.recyclerView)
        val mAdapter = GiftAdapter(type, R.layout.item_gift, item)
        if (recyclerView.itemDecorationCount == 0) {
            recyclerView.addItemDecoration(GiftItemDecoration())
        }
        recyclerView.layoutManager = GridLayoutManager(
            helper.itemView.context,
            4,
            RecyclerView.VERTICAL,
            false
        )
        recyclerView.adapter = mAdapter
        mAdapter.setNewData(item)

        mAdapter.setOnItemClickListener { _, _, position ->
            this@GiftPagerAdapter.data.forEach {
                it.forEach {
                    it.isSelected = false
                }
            }
            item[position].isSelected = true

            if (selected == position && type == 3) {
                //幸运礼物
                GiftConfig.clickCount++
                if (GiftConfig.clickCount == GiftConfig.list.size) {
                    GiftConfig.clickCount = 0
                }
            } else {
                GiftConfig.clickCount = 0
            }

            listener?.invoke()

            selected = position

            GiftConfig.giftSelectedWhichType = type
            GiftConfig.giftSelectedPageSelectedPosition = position
            GiftConfig.giftSelectedPageIndex = helper.layoutPosition

            this@GiftPagerAdapter.notifyDataSetChanged()
        }
    }

    //连击时切换幸运礼物的时候combo按钮要消失
    var listener: (() -> Unit)? = null


    inner class GiftItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            super.getItemOffsets(outRect, view, parent, state)
            val itemPosition = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
            val totalMargin = DpPxUtil.getScreenWidth() - 4 * DpPxUtil.dip2px(80f)
            val margin = totalMargin / 5
            when {
                itemPosition % 4 == 0 -> {
                    outRect.set(margin, 0, margin / 2, 0)
                }
                itemPosition % 4 == 3 -> {
                    outRect.set(margin / 2, 0, margin, 0)
                }
                else -> {
                    outRect.set(margin / 2, 0, margin / 2, 0)
                }
            }

        }
    }
}
