package com.example.wallpapermagazine.recylcle;

import android.annotation.SuppressLint;
import android.content.Context;
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.core.content.ContextCompat;
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.LoveDAO.FavoriteImage;
import com.example.wallpapermagazine.R;
import com.example.wallpapermagazine.ReadFile;
import com.example.wallpapermagazine.action.FavoriteLimage;
import com.example.wallpapermagazine.action.InfoListWallPaperListener;
import com.example.wallpapermagazine.action.InfoWallPaperListener;
import com.example.wallpapermagazine.action.Like;
import com.example.wallpapermagazine.json.Info;

import java.util.List;

public class FavoriteImageAdapter extends RecyclerView.Adapter<ImageViewHolder> {

    private final Context context;
    private List<Info> datainfo;
    private final List<FavoriteImage> favoriteImages;
    private FavoriteLimage infoListWallPaperListener;

    public void setInfoWallPaperListener(FavoriteLimage infoListWallPaperListener){
         this.infoListWallPaperListener= infoListWallPaperListener;
    }
    public FavoriteImageAdapter(Context context,  List<FavoriteImage> favoriteImages) {
        this.context = context;
        this.favoriteImages = favoriteImages;

    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.cata_item_recyle, parent, false);
        return new ImageViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, @SuppressLint("RecyclerView") int position) {
        FavoriteImage favoriteImage = favoriteImages.get(position);
        ImageView imageView=holder.getImageView();
        Glide.with(context)
                .load(favoriteImage.imageUrl)
                .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(infoListWallPaperListener != null) {
                    infoListWallPaperListener.onItemClickAction(favoriteImage,position);
                }

            }
        });
    }

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

}
