package com.swago.room.gift

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.model.live.gift.GiftModel
import com.swago.room.R
import com.swago.room.databinding.FragmentGiftBinding


/**
 *@date 2021/12/13 16:06
 *description:
 */
class GiftFragment : BaseXFragment<FragmentGiftBinding>() {
    /**
     * type礼物类型
     */
    private var type = -1
    private var data: ArrayList<GiftModel>? = null

    private val arrayList by lazy {
        ArrayList<ArrayList<GiftModel>>()
    }

    private var adapter: GiftPagerAdapter? = null

    companion object {
        fun newInstance(type: Int, data: ArrayList<GiftModel>): GiftFragment {
            val giftFragment = GiftFragment()
            val bundle = Bundle()
            bundle.putInt("type", type)
            bundle.putParcelableArrayList("data", data)
            giftFragment.arguments = bundle
            return giftFragment
        }
    }

    override fun loadData() {

    }

    override fun initOther() {
        arguments?.let { bundle ->
            type = bundle.getInt("type")
            data = bundle.getParcelableArrayList("data")
            adapter = GiftPagerAdapter(type, R.layout.fragment_gift, arrayList)
            binding.recyclerView.layoutManager = LinearLayoutManager(
                context,
                RecyclerView.HORIZONTAL, false
            )
            val horizontalPagerSnapHelper = HorizontalPagerSnapHelper()
            horizontalPagerSnapHelper.attachToRecyclerView(binding.recyclerView)
            binding.recyclerView.adapter = adapter


            data?.let {
                if (it.isEmpty()) return@let
                if (GiftConfig.giftSelectedWhichType == type){
                    it[GiftConfig.giftSelectedPageIndex * 8 + GiftConfig.giftSelectedPageSelectedPosition].isSelected =
                        true
                    binding.recyclerView.scrollToPosition(GiftConfig.giftSelectedPageIndex)
                }

                val isEight = it.size % 8 == 0
                val sum = if (isEight) {
                    it.size / 8
                } else {
                    it.size / 8 + 1
                }
                for (i in 0 until sum) {
                    val list = ArrayList<GiftModel>()
                    if ((i + 1) * 8 > it.size) {
                        list.addAll(it.subList(i * 8, it.size))
                    } else {
                        list.addAll(it.subList(i * 8, (i + 1) * 8))
                    }
                    arrayList.addAll(listOf(list))
                }
                adapter?.notifyDataSetChanged()
            }

            adapter?.listener = {
                listener?.invoke()
            }
        }
    }


    fun reset() {
        adapter?.let {
            it.data.forEach {
                it.forEach {
                    it.isSelected = false
                }
            }
            it.notifyDataSetChanged()
        }
    }

    var listener: (() -> Unit)? = null

    override fun initLiveData() {

    }
}