package com.apps.pixwall.adapter

import android.app.Activity
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.apps.pixwall.data.Paper
import com.apps.pixwall.databinding.ItemCategoryBinding
import com.apps.pixwall.listener.MainInListener
import com.apps.pixwall.listener.MainItemListener
import com.apps.pixwall.view.MainActivity
import com.apps.pixwall.view.PaperActivity

class MainOutAdapter : RecyclerView.Adapter<MainOutAdapter.CategoryViewHolder>(),MainInListener {
    private var nameList:List<String> = mutableListOf()
    private var coverAllList:List<List<Paper>> = mutableListOf()
    private lateinit var context: Context
    private lateinit var listener:MainItemListener

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

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        holder.binding.txTitle.text = nameList[position]
        val adapter=MainInAdapter()
        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(categoryList: MutableList<String>,list:List<List<Paper>>) {
        this.nameList=categoryList
        this.coverAllList=list
    }
    fun setListener(listener: MainItemListener){
        this.listener=listener
    }

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

    override fun inItemClick(paper: Paper) {
        val intent=Intent(context,PaperActivity::class.java)
        intent.putExtra(PaperActivity.paperKey,paper)
        context.startActivity(intent)
    }

}
