package com.example.wallpapermagazine.recylcle;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.wallpapermagazine.R;
import com.example.wallpapermagazine.action.InfoListWallPaperListener;
import com.example.wallpapermagazine.action.InfoWallPaperListener;
import com.example.wallpapermagazine.json.Info;
import com.example.wallpapermagazine.json.Info_data;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Random;

public class CataItemAdapter extends RecyclerView.Adapter<CataItemHolder> {

    private final List<Info_data> datainfo;
    private final Context mycontext;
    private final int op;
    private InfoListWallPaperListener infoWallPaperListener;

    public void setInfoWallPaperListener(InfoListWallPaperListener infoWallPaperListener) {
        this.infoWallPaperListener = infoWallPaperListener;
    }

    public CataItemAdapter(List<Info_data> infoList, int position, Context myCon) {
        this.datainfo = infoList;
        this.op = position;
        this.mycontext = myCon;
    }

    @NonNull
    @Override
    public CataItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemview = LayoutInflater.from(mycontext).inflate(R.layout.item_recly2, parent, false);
        return new CataItemHolder(itemview);
    }

    @Override
    public void onBindViewHolder(@NonNull CataItemHolder holder, @SuppressLint("RecyclerView") int position) {
        ImageView imageView = holder.getImageView();
        String name = datainfo.get(op).getName();
        List<Info> infos;
        for (int i = 0; i < datainfo.size(); i++) {
            if (Objects.equals(name, datainfo.get(i).getName())) {
                infos = datainfo.get(i).getDatas();
                for (int j = 0; j <= i; j++) {
                    Glide.with(mycontext)
                            .load(infos.get(position).getPreviewThumb())
                            .transform(new RoundedCorners(20))
                            .placeholder(R.drawable.fail)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(imageView);
                    holder.getLayoutItem().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (infoWallPaperListener != null) {
                                infoWallPaperListener.onItemClickAction(op, position);
                            }
                        }
                    });
                }
            }

        }
    }

    @Override
    public int getItemCount() {
        return datainfo.get(op).getDatas().size()-6;
    }
}
