package com.design.wallpapergallery.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.design.wallpapergallery.data.Photo
import com.design.wallpapergallery.databinding.ItemClassificationBinding
import com.design.wallpapergallery.listener.MainSecListener
import com.design.wallpapergallery.listener.MainFirListener
import com.design.wallpapergallery.view.StartDownApplyActivity

class ClassifyAdapter : RecyclerView.Adapter<ClassifyAdapter.ClassificationViewHolder>(),MainSecListener {
    private var nameList:List<String> = mutableListOf()
    private var coverAllList:List<List<Photo>> = mutableListOf()
    private lateinit var context: Context
    private lateinit var listener:MainFirListener

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

    override fun onBindViewHolder(holder: ClassificationViewHolder, position: Int) {
        holder.binding.txTitle.text = nameList[position]
        val adapter=ClassifyOpenAdapter()
        adapter.setCoverList(coverAllList[position])
        adapter.setListener(this)
        holder.binding.rvView.adapter=adapter
        val linearLayoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        holder.binding.rvView.layoutManager=linearLayoutManager
        holder.binding.rvView.setOnClickListener{
            listener.more(nameList[position])
        }
        holder.binding.llView.setOnClickListener {
            listener.more(nameList[position])
        }
    }

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

    fun setList(classificationList: MutableList<String>,list:List<List<Photo>>) {
        this.nameList=classificationList
        this.coverAllList=list
    }
    fun setListener(listener: MainFirListener){
        this.listener=listener
    }

    class ClassificationViewHolder(val binding: ItemClassificationBinding) : RecyclerView.ViewHolder(binding.root)

    override fun inItemClick(photo: Photo) {
        val intent=Intent(context,StartDownApplyActivity::class.java)
        intent.putExtra(StartDownApplyActivity.paperKey,photo)
        context.startActivity(intent)
    }

}
