package com.remax.visualnovel.ui.chat.ui.expandableSelector

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.drake.brv.annotaion.DividerOrientation
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.divider
import com.drake.brv.utils.grid
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.LayoutItemSettingBubbleBinding
import com.remax.visualnovel.databinding.LayoutSettingBubbleSubViewBinding
import com.remax.visualnovel.entity.response.ChatBubble
import com.remax.visualnovel.extension.glide.load


class ExpandBubbleSubView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private lateinit var items: List<ChatBubble>
    private var mBinding: LayoutSettingBubbleSubViewBinding


    init {
        mBinding = LayoutSettingBubbleSubViewBinding.inflate(LayoutInflater.from(context), this, true)
        with(mBinding) {
            initRv(itemsRv)
        }
    }

    fun setSelectedSound() {

    }

    private fun initRv(itemsRv: RecyclerView) {
        itemsRv.grid(3)
            .divider {
                setDivider(10, true)
                orientation = DividerOrientation.VERTICAL
            }.setup {
                addType<ChatBubble>(R.layout.layout_item_setting_bubble)

                onClick(R.id.iv_select) {
                    val bubble = getModel<ChatBubble>()
                    if (!bubble.select) {
                        itemsRv.bindingAdapter.models?.filterIsInstance<ChatBubble>()?.forEach { item ->
                            item.select = item == bubble
                        }
                        itemsRv.bindingAdapter.notifyDataSetChanged()
                        setSelectedSound()
                    }
                }

                onBind {
                    val item = getModel<ChatBubble>()
                    with(getBinding<LayoutItemSettingBubbleBinding>()) {
                        if (!item.imgUrl.isNullOrEmpty()) {
                            //ivBubble.load(item.imgUrl)
                        }

                        ivBubbleName.text = item.name
                        //selectedBg.isVisible = item.select
                    }
                }
            }
    }

    fun setItems(newItems: List<ChatBubble>) {
        items = newItems
        mBinding.itemsRv.models = items
    }


}