package com.app.input.inputaura.onjfsgvfsf;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.app.input.inputaura.dasda.Shikamaru;
import com.bumptech.glide.Glide;
import com.app.input.inputaura.R;
import com.app.input.inputaura.kfsdfbsd.IutFhfg;
import com.app.input.inputaura.bgtng.OnuIg;
import com.app.input.inputaura.lmyfvfs.ViyVyg;

import java.util.ArrayList;
import java.util.List;

public class OvfYfd extends RecyclerView.Adapter<OvfYfd.ForYouViewHolder> {

    private Context mContext;
    private List<Shikamaru> mList = new ArrayList<>();

    private OnuIg mCallBack;

    public OvfYfd(Context context) {
        mContext = context;
    }

    public void setForYouList(List<Shikamaru> list) {
        this.mList = list;
        notifyDataSetChanged();
    }


    public void setRemoveLike(OnuIg callback) {
        mCallBack = callback;
    }

    @NonNull
    @Override
    public ForYouViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.ynsygd, parent, false);
        return new ForYouViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ForYouViewHolder holder, int position) {
        Shikamaru shikamaru = mList.get(position);
        String thumbGif = shikamaru.getThumbGif();
        String thumb = shikamaru.getThumbUrl();
        if (!thumbGif.isEmpty()) {
            IutFhfg.INSTANCE.loadWepJif(mContext, thumbGif, holder.itemImg);
        } else {
            Glide.with(mContext)
                    .load(thumb).error(R.drawable.hffghfgh)
                    .placeholder(R.drawable.hffghfgh).into(holder.itemImg);
        }
        holder.itemFavorite.setSelected(true);
        holder.layoutFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.itemFavorite.setSelected(false);
                int adapterPosition = holder.getAdapterPosition();
                notifyItemRemoved(adapterPosition);
                if (mCallBack != null) {
                    mCallBack.OnRemoveLike(shikamaru);
                }
            }
        });
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ViyVyg.class);
                intentApply.putExtra(ViyVyg.SOURCE_KEY, shikamaru);
                intentApply.putExtra(ViyVyg.DISPLAY_URL_KEY, shikamaru.getImgPath());
                intentApply.putExtra(ViyVyg.ZIP_URL_KEY, shikamaru.getZipPath());
                intentApply.putExtra(ViyVyg.NAME_KEY, shikamaru.getTitleName());
                intentApply.putExtra(ViyVyg.GIF_KEY, shikamaru.getImgGif());
                String intent_thumb;
                if (!thumbGif.isEmpty()) {
                    intent_thumb = thumbGif;
                } else {
                    intent_thumb = thumb;
                }
                intentApply.putExtra(ViyVyg.THUMB_KEY, intent_thumb);
                mContext.startActivity(intentApply);


            }
        });


    }

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

    public static class ForYouViewHolder extends RecyclerView.ViewHolder {

        private CardView cardView;
        private FrameLayout layoutFavorite;
        private ImageView itemImg, itemFavorite;

        public ForYouViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            layoutFavorite = itemView.findViewById(R.id.layout_favorite);
            itemImg = itemView.findViewById(R.id.im);
            itemFavorite = itemView.findViewById(R.id.im_favorite);

        }

    }

}
