package com.example.prettywallpapers.prettyadapter;

import static com.example.prettywallpapers.myvalue.MyValues.*;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
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.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.PrettyPaperActivity;
import com.example.prettywallpapers.list.PrettyPaper;
import com.example.prettywallpapers.listener.SwitchType;
import com.example.prettywallpapers.myvalue.MyValues;

import java.util.List;

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

    @Override
    public void onBindViewHolder(@NonNull InfoViewHolder holder, int position) {
        PrettyPaper prettyPaper = prettyPapers.get(position);
        ImageView imageView = holder.getImageView();
        //Log.e("----------paper","-----------paper"+prettyPaper.getSourceUrl());
        Glide.with(context)
                .load(prettyPaper.getSourceUrl())
                .error(R.mipmap.sit)
                .transform(new RoundedCorners(100))
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                       //Log.e("-----paper", "onLoadFailed-----------------categorylist.getCategoryUrl()=---"+prettyPaper.getSourceUrl()+"---------e="+e.getMessage());
                        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, PrettyPaperActivity.class);
                //intent.putExtra(MyValues.TYPE,"2");
                //Log.d("-----MyValues.TYPE","------MyValues.TYPE"+MyValues.TYPE);
                intent.putExtra(MyValues.PAPER_IMAGE,prettyPaper.getSourceUrl());
                Log.d("-----MyValues.paper","------MyValues.TYPE"+MyValues.PAPER_IMAGE);
                context.startActivity(intent);
            }
        });

    }

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