package com.adpro.pixelshow.event

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adpro.pixelshow.data.Movie
import com.adpro.pixelshow.databinding.ItemClassEveryBinding
import com.adpro.pixelshow.look.DownloadRequestActivity

class ClassFrameAdapter : RecyclerView.Adapter<ClassFrameAdapter.CategoryViewHolder>(),SecFavListener {
    private var categoryNameList:List<String> = mutableListOf()
    private var coverAllList:List<List<Movie>> = mutableListOf()
    private lateinit var context: Context
    private lateinit var adapterListener:FirClassListener

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
        val binding= ItemClassEveryBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        context=parent.context
        return CategoryViewHolder(binding)
    }

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        holder.binding.txTitle.text = categoryNameList[position]
        val adapter=ClassFillingAdapter()
        adapter.setCoverList(coverAllList[position])
        adapter.setListener(this)
        holder.binding.rvView.adapter=adapter
        val girdLayoutManager = GridLayoutManager(context,3)
        holder.binding.rvView.layoutManager=girdLayoutManager
        holder.binding.rvView.setOnClickListener{
            adapterListener.more(categoryNameList[position])
        }
        holder.binding.llView.setOnClickListener {
            adapterListener.more(categoryNameList[position])
        }
    }

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

    fun setList(classificationList: MutableList<String>,list:List<List<Movie>>) {
        this.categoryNameList=classificationList
        this.coverAllList=list
    }
    fun setListener(listener: FirClassListener){
        this.adapterListener=listener
    }

    class CategoryViewHolder(val binding: ItemClassEveryBinding) : RecyclerView.ViewHolder(binding.root)

    override fun inItemClick(movie: Movie) {
        val intent=Intent(context,DownloadRequestActivity::class.java)
        intent
        intent.putExtra(DownloadRequestActivity.paperKey,movie)
        context.startActivity(intent)
    }

}
