package com.lh.wallpaper2.adapter;

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.resource.bitmap.RoundedCorners;
import com.lh.wallpaper2.R;
import com.lh.wallpaper2.action.FavoriteWallpaperListener;
import com.lh.wallpaper2.room.Favorite;
import com.lh.wallpaper2.file.ReadFile;
import com.lh.wallpaper2.recycleView.DetailsWallpaperVH;

import java.util.List;

public class FavoriteWallpaperAdapter extends RecyclerView.Adapter<DetailsWallpaperVH>  {
    private List<Favorite> dataInfo ;
    private Context myContext;
    private FavoriteWallpaperListener infoWallpaperListener;

    public void setInfoWallpaperListener(FavoriteWallpaperListener infoWallpaperListener) {
        this.infoWallpaperListener = infoWallpaperListener;
    }
    public FavoriteWallpaperAdapter(List<Favorite> infoList, Context myCon) {
        dataInfo = infoList;
        myContext = myCon;
    }
    @NonNull
    @Override
    public DetailsWallpaperVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(myContext).inflate(R.layout.details_vh, parent, false);
        return new DetailsWallpaperVH(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull DetailsWallpaperVH holder, int position) {
        Favorite info = dataInfo.get(position);
        ImageView imageView = holder.getImageView();
        Glide.with(myContext)
                .load(info.getPicture())
                .transform(new RoundedCorners(ReadFile.dp2Px(7)))
                .placeholder(R.drawable.preview)
                .into(imageView);
        holder.getItemRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (infoWallpaperListener != null) {
                    infoWallpaperListener.onItemClickAction(info);
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return dataInfo.size();
    }
}
