package com.zxbw.modulemain.box.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.zxbw.modulemain.R
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import kotlin.random.Random

class SelectBoxAdapter : BaseRecyclerAdapter<Any, SelectBoxAdapter.ViewHolder> {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<Boolean>
    private val checkStatus: MutableMap<Int, Boolean> = HashMap()
    private var hideBoxNum = 0;
    private var maxSelectBoxNum = -1;
    private var selectBoxNum = 0;
    private var hideBoxIndex = ArrayList<Int>()

    constructor(
        content: Context,
        listener: OnRecycleItemClickListener<Boolean>
    ) : super() {
        mContext = content
        this.listener = listener
        initCheck(false)
    }

    fun setMaxSelect(maxNum: Int) {
        maxSelectBoxNum = maxNum
    }

    private fun initCheck(flag: Boolean) {
        for (i in 0 until itemCount) {
            //更改指定位置的数据
            checkStatus[i] = flag
        }
        selectBoxNum = 0;
        hideBoxNum = Random.nextInt(6)
        var indexList = ArrayList<Int>()
        for (num in 0..16) {
            indexList.add(num)
        }
        hideBoxIndex.clear()
        for (j in 0 until hideBoxNum) {
            val nextInt = Random.nextInt(indexList.size)
            hideBoxIndex.add(indexList[nextInt])
            indexList.removeAt(nextInt)
        }
    }

    fun resetBoxList() {
        initCheck(false)
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.item_tv_box_open_number.text = "${position + 1}"
        holder.item_tv_box_un_open_number.text = "${position + 1}"
        holder.item_select_icon.visibility = View.GONE
        //已被选中的  无法选中
        if (hideBoxIndex.contains(position)) {
            holder.item_select_bg.setBackgroundResource(R.drawable.shape_f2f2f2_r10)
            holder.item_tv_box_open_number.visibility = View.VISIBLE
            holder.item_tv_box_un_open_number.visibility = View.GONE

            holder.item_img_box_un_open_bg.setImageResource(R.drawable.box_select_open_box)
        } else {
            holder.item_tv_box_open_number.visibility = View.GONE
            holder.item_tv_box_un_open_number.visibility = View.VISIBLE
            holder.item_img_box_un_open_bg.setImageResource(R.drawable.box_select_unopen_box)

            holder.item_select_bg.setBackgroundResource(R.drawable.shape_ece9ff_r10)




            if (checkStatus[position] == true) {
                holder.item_select_bg.setBackgroundResource(R.drawable.box_select_item_bg)
                holder.item_select_icon.visibility = View.VISIBLE
            } else {
                holder.item_select_bg.setBackgroundResource(R.drawable.shape_ece9ff_r10)
                holder.item_select_icon.visibility = View.GONE
            }
        }

        holder.item_rl_view.setOnClickListener {
            if (hideBoxIndex.contains(position)){
                return@setOnClickListener
            }
            var b = checkStatus[position] ?: false
            if (!b) {
                if (maxSelectBoxNum in 1..selectBoxNum) {
                    Utils.showToast(mContext, "优惠盲盒只能选择一个~")
                    return@setOnClickListener
                }
            }
            b = !b
            if (b) {
                selectBoxNum++
                holder.item_select_bg.setBackgroundResource(R.drawable.box_select_item_bg)
                holder.item_select_icon.visibility = View.VISIBLE
            } else {
                selectBoxNum--
                holder.item_select_bg.setBackgroundResource(R.drawable.shape_ece9ff_r10)
                holder.item_select_icon.visibility = View.GONE
            }
            checkStatus[position] = b
            listener.onItemClick(it, position, b)

        }
    }

    override fun getItemCount(): Int {
        return 16
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelectBoxAdapter.ViewHolder {
        return ViewHolder(
            LayoutInflater.from(AppContext.get())
                .inflate(R.layout.item_layout_select_box, parent, false)
        )
    }

    class ViewHolder : RecyclerView.ViewHolder {
        var item_rl_view: RelativeLayout
        var item_select_bg: ImageView
        var item_select_icon: ImageView
        var item_img_box_un_open_bg: ImageView
        var item_tv_box_un_open_number: TextView
        var item_tv_box_open_number: TextView

        constructor(
            itemView: View
        ) : super(itemView) {
            item_rl_view = itemView.findViewById<RelativeLayout>(R.id.id_rl_item_select_box)
            item_select_bg = itemView.findViewById<ImageView>(R.id.id_img_item_select_bg)
            item_select_icon = itemView.findViewById<ImageView>(R.id.id_img_select_icon)
            item_img_box_un_open_bg = itemView.findViewById<ImageView>(R.id.id_img_box_un_open_bg)
            item_tv_box_un_open_number =
                itemView.findViewById<TextView>(R.id.id_tv_box_un_open_number)
            item_tv_box_open_number = itemView.findViewById<TextView>(R.id.id_tv_box_open_number)

        }
    }


}