package com.example.wallpapermagazine.recylcle;

import android.annotation.SuppressLint;
import android.content.Context;
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.ReadFile;
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.util.List;
import java.util.Objects;

public class RecommonedAdapter extends RecyclerView.Adapter<RecommonedHolder> {

    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 RecommonedAdapter(List<Info_data> infoList, Context myCon, int op) {
        datainfo = infoList;
        this.op = op;
        mycontext = myCon;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecommonedHolder 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 6;
    }
}