package com.soundapp.soundgags.setting

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.soundapp.soundgags.datame.Reflection
import com.soundapp.soundgags.ui.NowPlayingActivity
import com.bumptech.glide.Glide
import com.soundapp.soundgags.databinding.ItemShowStyle1Binding
import com.soundapp.soundgags.databinding.ItemShowStyle2Binding

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

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

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

    // 第二个 ViewHolder
    class ViewHolder2(val binding: ItemShowStyle2Binding): 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 = ItemShowStyle1Binding.inflate(LayoutInflater.from(context), parent, false)
                ViewHolder1(binding)
            }
            VIEW_TYPE_2 -> {
                val binding = ItemShowStyle2Binding.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 reflection = infoList[position]
        val covert: String = reflection.convert
        Log.d("covert",covert)

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

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

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