package com.design.mischiefgo.ut

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.design.mischiefgo.entity.Duplicate
import com.design.mischiefgo.ui.UseSoundActivity
import com.bumptech.glide.Glide
import com.design.mischiefgo.databinding.ItemNoRandomBinding
import com.design.mischiefgo.databinding.ItemRandomBinding

class RandomAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private lateinit var context:Context
    private var type:Int=0
    private var infoList :List<Duplicate> = arrayListOf()
    private lateinit var listener: OnMoreClickListener

    companion object {
        private const val VIEW_TYPE_1 = 1
        private const val VIEW_TYPE_2 = 2
    }

    // 第一个 ViewHolder
    class ViewHolder1(val binding: ItemNoRandomBinding): RecyclerView.ViewHolder(binding.root)

    // 第二个 ViewHolder
    class ViewHolder2(val binding: ItemRandomBinding): RecyclerView.ViewHolder(binding.root)

    override fun getItemViewType(position: Int): Int {
        return if (type != 0) VIEW_TYPE_2 else VIEW_TYPE_1
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        context = parent.context
        return when (viewType) {
            VIEW_TYPE_1 -> {
                val binding = ItemNoRandomBinding.inflate(LayoutInflater.from(context), parent, false)
                ViewHolder1(binding)
            }
            VIEW_TYPE_2 -> {
                val binding = ItemRandomBinding.inflate(LayoutInflater.from(context), parent, false)
                ViewHolder2(binding)
            }
            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    override fun getItemCount(): Int {
        return infoList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val duplicate = infoList[position]
        val covert: String = duplicate.convert
        Log.d("covert",covert)

        when (holder) {
            is ViewHolder1 -> {
                // 使用 ItemShowDetail1Binding 的布局
                Glide.with(context).load(covert).into(holder.binding.image)
                holder.binding.title.text = duplicate.name
                holder.binding.imgCard.setOnClickListener {
                    val intent = Intent(context, UseSoundActivity::class.java)
                    intent.putExtra(UseSoundActivity.KEY_LIST_DATA, duplicate)
                    context.startActivity(intent)
                }
            }
            is ViewHolder2 -> {
                // 使用 ItemShowDetail2Binding 的布局
                Glide.with(context).load(covert).into(holder.binding.image)
//                holder.binding.title.text = duplicate.name
                holder.binding.imgCard.setOnClickListener {
                    listener.onMoreClick(duplicate)
                }
            }
        }
    }

    fun setList(list: List<Duplicate>, setType: Int = 0) {
        this.infoList = list
        this.type = setType
        notifyDataSetChanged()
    }

    fun setListener(listener: OnMoreClickListener) {
        this.listener = listener
    }
}