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.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.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.LayoutItemSettingSoundBinding
import com.remax.visualnovel.databinding.LayoutSettingSoundSubViewBinding
import com.remax.visualnovel.entity.response.ChatSound
import com.remax.visualnovel.extension.glide.load
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.view.UITokenLinearLayout

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


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

    private fun initRadioGroup(radioGroup: UITokenLinearLayout) {
        // TODO("Not yet implemented")
    }

    fun playActorSound(bubble : ChatSound) {

    }

    fun setSelectedSound() {

    }

    private fun initRv(itemsRv: RecyclerView) {
        itemsRv.linear(VERTICAL)
            .divider {
                setDivider(16, true)
                orientation = DividerOrientation.VERTICAL
            }.setup {
                addType<ChatSound>(R.layout.layout_item_setting_sound)

                onClick(R.id.left_container) {
                    val bubble = getModel<ChatSound>()
                    playActorSound(bubble)
                }

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

                onBind {
                    val item = getModel<ChatSound>()
                    with(getBinding<LayoutItemSettingSoundBinding>()) {
                        if (!item.imgUrl.isNullOrEmpty()) {
                            userAvatar.load(item.imgUrl)
                        } else {
                            userAvatar.setImageResource(if (item.isMale) R.mipmap.ic_gender_male else R.mipmap.ic_gender_female)
                        }

                        tvSoundName.text = item.name
                        itemRoot.changeBackground(backgroundUIColorToken = if (item.isMale) R.string.male_bg else R.string.female_bg)
                        tvSoundDescrible.setTextColor(context.resources.getColor(if (item.isMale) R.color.male_text_color else R.color.female_text_color))
                        tvSoundDescrible.text = item.description
                    }
                }
            }
    }

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


}