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.LayoutItemSettingHistoryBinding
import com.remax.visualnovel.databinding.LayoutSettingBgSubViewBinding
import com.remax.visualnovel.entity.response.ChatHistory
import com.remax.visualnovel.extension.glide.load
import java.text.SimpleDateFormat


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


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

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

                onClick(R.id.root) {
                    itemsRv.bindingAdapter.models?.filterIsInstance<ChatHistory>()?.forEach { item ->
                        openChatHistory(item)
                    }
                }

                onBind {
                    val item = getModel<ChatHistory>()
                    with(getBinding<LayoutItemSettingHistoryBinding>()) {
                        if (!item.imgUrl.isNullOrEmpty()) {
                            ivActorAvatar.load(item.imgUrl)
                        }

                        tvChatTime.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.time)
                        tvChatDes.text = item.describle
                    }
                }
            }
    }

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

    fun openChatHistory(history: ChatHistory) {

    }


}