package com.example.prettywallpapers.prettyadapter;

import android.annotation.SuppressLint;
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 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.myvalue.MyValues;
import com.example.prettywallpapers.room.PrettyPaperDatabase;
import com.example.prettywallpapers.room.PrettyPaperEntity;
import com.example.prettywallpapers.room.mytool;

import java.util.List;

public class LIkePaperAdapter extends RecyclerView.Adapter<LikeHolder> {
    private List<PrettyPaperEntity> paperEntities ;
    private Context mycontext;
    private boolean mylike;
    public LIkePaperAdapter(Context context,List<PrettyPaperEntity> entities){
        mycontext = context;
        paperEntities = entities;

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


    @Override
    public void onBindViewHolder(@NonNull LikeHolder holder, @SuppressLint("RecyclerView") int position) {
        PrettyPaperEntity prettyPaperEntities = paperEntities.get(position);
        Log.d("------------entity","---------entity"+paperEntities.size());
        //ImageView imageView = holder.getImageView();
        Glide.with(mycontext).load(prettyPaperEntities.getImage())
                .transform(new RoundedCorners(90))
                .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(holder.getImageView());
       holder.getImageView_like().setBackgroundResource(R.drawable.select_like);
      holder.getImageView_like().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                paperEntities.remove(prettyPaperEntities);
                notifyItemRemoved(position);
                mytool.runIO(new Runnable() {
                    @Override
                    public void run() {
                        PrettyPaperDatabase.getPrettyPaperDatabase().getPrettyPaperDao().delete(prettyPaperEntities);
                        //PrettyPaperDatabase.getPrettyPaperDatabase().getPrettyPaperDao().UpdateLikePaper(prettyPaperEntities);
                        // Log.d("---entities","-----entities"+entities.size());
                        //Toast.makeText(PrettyPaperActivity.this,"succes",Toast.LENGTH_LONG).show();
                    }
                });
                //mytool.updateRoom(prettyPaperEntities);
                }
        });
      holder.getImageView().setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(mycontext, PrettyPaperActivity.class);
              intent.putExtra(MyValues.PAPER_IMAGE,prettyPaperEntities.getImage());
              mycontext.startActivity(intent);
          }
      });


    }

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