package com.wall.engine.wallpaper;

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.request.RequestOptions;
import java.util.List;

public class D4ImageAdapter extends RecyclerView.Adapter<D4ImageAdapter.ImageViewHolder> {

    private final Context mContext;
    private final List<WallpaperItem> mWallpaperList;
    private OnItemClickListener mOnItemClickListener;

    private final RequestOptions glideOptions = new RequestOptions()
            .centerCrop()
            .placeholder(R.drawable.load_ing)
            .error(R.drawable.load_ing)
            .override(400, 600);
    public D4ImageAdapter(Context context, List<WallpaperItem> wallpaperList) {
        this.mContext = context;
        this.mWallpaperList = wallpaperList;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, WallpaperItem wallpaperItem);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView ivItem4d;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            ivItem4d = itemView.findViewById(R.id.iv_item_4d);
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        WallpaperItem wallpaperItem = mWallpaperList.get(position);


        Glide.with(mContext)
                .load(wallpaperItem.getUrls().getSmall())
                .apply(glideOptions)
                .into(holder.ivItem4d);

        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(position, wallpaperItem);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mWallpaperList == null ? 0 : mWallpaperList.size();
    }
}