package com.haven.wallpapers.adapter;
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.haven.wallpapers.R;
import com.haven.wallpapers.MyApp;
import com.haven.wallpapers.listener.UrlListener;
import com.haven.wallpapers.jsonbean.Data;
import com.haven.wallpapers.myviewholder.DetailsVH;

import java.util.List;


public class DetailsAdapter extends RecyclerView.Adapter<DetailsVH> {
    private List<Data> dataInfo ;
    private Context myContext;
    private UrlListener urlListener;
    @NonNull
    @Override
    public DetailsVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(myContext).inflate(R.layout.details_vh, parent, false);
        return new DetailsVH(itemView);
    }

    public DetailsAdapter(List<Data> dataInfo, Context myContext) {
        this.dataInfo = dataInfo;
        this.myContext = myContext;
    }
    public void setUrlWallpaperListener(UrlListener urlListener) {
        this.urlListener = urlListener;
    }
    @Override
    public void onBindViewHolder(@NonNull DetailsVH holder, @SuppressLint("RecyclerView") int position) {
        Data info = dataInfo.get(position);

        ImageView imageView = holder.getImageView();


        Glide.with(myContext)
                .load(info.getPreviewUrl400())
                .placeholder(MyApp.getPlaceImg())
                .transform(MyApp.getMultiTransformation())
                .into(imageView);
        holder.getItemRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (urlListener != null) {
                    urlListener.onItemClickAction(info);
                }
            }
        });
    }

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