package com.free.life.wallpaper.recycler;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.free.life.wallpaper.MyWallpaper;
import com.free.life.wallpaper.databinding.ItemHomeParentBinding;
import com.free.life.wallpaper.bean.Category;
import com.free.life.wallpaper.bean.Data;
import com.free.life.wallpaper.mylistener.ItemClickListener;
import com.free.life.wallpaper.utils.CommonUtils;
import com.free.life.wallpaper.utils.RecyclerSpace;

import java.util.ArrayList;
import java.util.List;

public class HomeParentAdapter extends RecyclerView.Adapter<HomeParentAdapter.HomeParentHolder> {
    private List<Category> data = new ArrayList<>();
    private Context mContext;
    private ItemClickListener listener;

    public void setData(List<Category> data) {
        this.data = data;
    }
    public void setInfoWallpaperListener(ItemClickListener itemClickListener) {
        this.listener = itemClickListener;
    }
    @NonNull
    @Override
    public HomeParentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        ItemHomeParentBinding inflate = ItemHomeParentBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new HomeParentHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeParentHolder holder, int position) {
        Category category = data.get(position);
        holder.itemBing.tvClassName.setText(category.getClassName());
        initChild(holder,category.getDataList().subList(0,4));
        holder.itemBing.imMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickMore(holder.getAdapterPosition());
            }
        });

    }

    private void initChild(HomeParentHolder holder, List<Data> data) {
        int height = CommonUtils.dp2Px(120);
        AdapterThumb adapterThumb = new AdapterThumb(mContext,height);
        adapterThumb.setDataInfo(data);
        adapterThumb.setInfoWallpaperListener(listener);
        RecyclerSpace recyclerSpace = new RecyclerSpace(10, 10, 0);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(MyWallpaper.mAppContext, 2);
        holder.itemBing.childRecycler.addItemDecoration(recyclerSpace);
        holder.itemBing.childRecycler.setLayoutManager(gridLayoutManager);

        holder.itemBing.childRecycler.setAdapter(adapterThumb);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class HomeParentHolder extends RecyclerView.ViewHolder {

        private ItemHomeParentBinding itemBing;

        public HomeParentHolder(@NonNull ItemHomeParentBinding itemView) {
            super(itemView.getRoot());
            itemBing = itemView;
        }
    }
}
