package com.kaixed.kchat.view.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.View.VISIBLE
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.kaixed.kchat.databinding.ItemSettingFunctionBinding
import com.kaixed.kchat.databinding.ItemSettingNormalBinding
import com.kaixed.kchat.databinding.ItemSettingTitleBinding
import com.kaixed.kchat.model.setting.SettingItem
import com.kaixed.kchat.utils.Constants.MESSAGE_TYPE_FUNCTION
import com.kaixed.kchat.utils.Constants.MESSAGE_TYPE_NORMAL
import com.kaixed.kchat.utils.Constants.MESSAGE_TYPE_TITLE

/**
 * @Author: kaixed
 * @Date: 2024/10/8 13:53
 * @Description: TODO
 */
class SettingListAdapter(private val items: MutableList<SettingItem>) :
    RecyclerView.Adapter<ViewHolder>() {

    class NormalViewHolder(val binding: ItemSettingNormalBinding) : ViewHolder(binding.root)
    class TitleViewHolder(val binding: ItemSettingTitleBinding) : ViewHolder(binding.root)
    class FunctionViewHolder(val binding: ItemSettingFunctionBinding) : ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return when (viewType) {

            MESSAGE_TYPE_TITLE -> {
                val binding = ItemSettingTitleBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                TitleViewHolder(binding)
            }

            MESSAGE_TYPE_NORMAL -> {
                val binding = ItemSettingNormalBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                NormalViewHolder(binding)
            }

            MESSAGE_TYPE_FUNCTION -> {
                val binding = ItemSettingFunctionBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                FunctionViewHolder(binding)
            }

            else -> throw IllegalArgumentException("Invalid view type")
        }
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is TitleViewHolder -> bindTitleViewHolder(holder, position)
            is NormalViewHolder -> bindNormalViewHolder(holder, position)
            is FunctionViewHolder -> bindFunctionViewHolder(holder, position)
        }
    }

    private fun bindTitleViewHolder(holder: TitleViewHolder, position: Int) {
        val item = items[position] as SettingItem.TitleItem
        holder.binding.tvItemName.text = item.title
        holder.binding.tvItemName.visibility = if (item.title.isEmpty()) View.GONE else VISIBLE
        holder.binding.view.visibility = if (item.title.isEmpty()) VISIBLE else View.GONE
    }

    private fun bindNormalViewHolder(holder: NormalViewHolder, position: Int) {
        val item = items[position] as SettingItem.NormalItem
        holder.binding.tvItemName.text = item.name

        holder.binding.viewItemDecoration.visibility =
            if (position != items.size - 1 && items[position + 1] is SettingItem.TitleItem) View.GONE else VISIBLE
    }

    private fun bindFunctionViewHolder(holder: FunctionViewHolder, position: Int) {
    }


    override fun getItemViewType(position: Int): Int {
        return when (items[position]) {
            is SettingItem.TitleItem -> MESSAGE_TYPE_TITLE
            is SettingItem.NormalItem -> MESSAGE_TYPE_NORMAL
            is SettingItem.FunctionItem -> MESSAGE_TYPE_FUNCTION
        }
    }

    override fun getItemCount(): Int = items.size
}