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.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.utils.ResUtil
import com.remax.visualnovel.widget.uitoken.setBgColorDirectly

private const val FILTER_ALL:Int = 0
private const val FILTER_MALE:Int = 1
private const val FILTER_FEMALE:Int = 2

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

    private lateinit var mEventListener: IEventListener


    interface IEventListener {
        fun onSoundSelected(sound: ChatSound)
        fun onFilterChanged(filterType: Int)
    }


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

    private fun initRadioGroup() {
        with (mBinding) {
            radioAll.isChecked = true
            radioGroup.setOnCheckedChangeListener { _, checkedId ->
                when (checkedId) {
                    R.id.radio_all -> {
                        mEventListener.onFilterChanged(FILTER_ALL)
                    }

                    R.id.radio_male -> {
                        mEventListener.onFilterChanged(FILTER_MALE)
                    }

                    R.id.radio_female -> {
                        mEventListener.onFilterChanged(FILTER_FEMALE)
                    }
                }
            }
        }
    }

    fun playActorSound(bubble : ChatSound) {

    }


    fun setEventListener(eventListener: IEventListener) {
        mEventListener = eventListener
    }

    private fun initRv(itemsRv: RecyclerView) {
        itemsRv.linear(VERTICAL)
            .divider {
                setDivider(5, 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.item_root) {
                    val sound = getModel<ChatSound>()
                    if (!sound.select) {
                        itemsRv.bindingAdapter.models?.filterIsInstance<ChatSound>()?.forEach { item ->
                            item.select = item == sound
                        }
                        itemsRv.bindingAdapter.notifyDataSetChanged()
                        mEventListener.onSoundSelected(sound)
                    }
                }

                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.setBgColorDirectly(bgColor = ResUtil.getColor(if (item.isMale) R.color.male_bg else R.color.female_bg), radius = ResUtil.getPixelSize(R.dimen.dp_10).toFloat(), bgDrawable = null)
                        tvSoundDescrible.setTextColor(ResUtil.getColor(if (item.isMale) R.color.male_text_color else R.color.female_text_color))
                        tvSoundDescrible.text = item.description
                        ivSelect.setImageResource(if (item.select) R.drawable.sound_item_selected else R.drawable.sound_item_unselected)
                    }
                }
            }
    }

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


}