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 android.widget.Toast;

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.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 islike;
    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(100))
                .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());
       islike = prettyPaperEntities.getPaperlike();
       if (islike){
           holder.getImageView_like().setSelected(true);
       }else {
           holder.getImageView_like().setSelected(false);
       }
      holder.getImageView_like().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.getImageView_like().setSelected(!holder.getImageView_like().isSelected());
                if (holder.getImageView_like().isSelected()){
                    prettyPaperEntities.setPaperlike(true);
                    Toast.makeText(mycontext,mycontext.getText(R.string.save_success),Toast.LENGTH_SHORT).show();
                }else {
                    prettyPaperEntities.setPaperlike(false);
                    Toast.makeText(mycontext,mycontext.getText(R.string.like_cancle),Toast.LENGTH_SHORT).show();
                }
                MyTool.runIO(new Runnable() {
                    @Override
                    public void run() {
                        PrettyPaperDatabase.getPrettyPaperDatabase().getPrettyPaperDao().UpdateLikePaper(prettyPaperEntities);
                    }
                });
                }
        });
      holder.getImageView().setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(mycontext, PreviewActivity.class);
              intent.putExtra(MyValues.PAPER_IMAGE,prettyPaperEntities.getImage());
              mycontext.startActivity(intent);
          }
      });


    }

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