package com.example.prettywallpapers.prettyadapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.prettywallpapers.R;
import com.example.prettywallpapers.activity.PreviewActivity;
import com.example.prettywallpapers.list.PrettyPaper;
import com.example.prettywallpapers.myvalue.MyValues;

import java.util.List;

public class SeeAllInFoAdapter extends RecyclerView.Adapter<SeeAllInFoViewHolder> {
    private List<PrettyPaper> prettyPapers;
    private Context context;
    public SeeAllInFoAdapter(List<PrettyPaper> paperList, Context mycontext){
        prettyPapers = paperList;
        context = mycontext;
    }
    @NonNull
    @Override
    public SeeAllInFoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.name_list_recycle,parent,false);
        return new SeeAllInFoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SeeAllInFoViewHolder holder, int position) {
        PrettyPaper prettyPaper = prettyPapers.get(position);
        ImageView imageView = holder.getImageView();
        Glide.with(context).load(prettyPaper.getSourceUrl())
                .error(R.mipmap.sit)
                .transform(new RoundedCorners(70))
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                }).into(imageView);
        holder.getImage_layout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PreviewActivity.class);
                intent.putExtra(MyValues.PAPER_IMAGE,prettyPaper.getSourceUrl());
                context.startActivity(intent);
            }
        });
    }

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