package com.joyful.euphoria.board.recyclerview;


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.joyful.euphoria.board.xmlactivity.DownloadXML;
import com.bumptech.glide.Glide;
import com.joyful.euphoria.board.R;
import com.joyful.euphoria.board.beaned.BeanDetails;
import com.joyful.euphoria.board.utils.Normal;
import com.joyful.euphoria.board.remember.DeleteCollectRemember;

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

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

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

    private DeleteCollectRemember mCallBack;

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

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


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

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

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

    @Override
    public void onBindViewHolder(@NonNull ForYouViewHolder holder, int position) {
        BeanDetails beanDetails = mList.get(position);
        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();
        if (thumbGif != null && !thumbGif.isEmpty()) {
            Normal.INSTANCE.loadWepJif(mContext, thumbGif, holder.itemImg);
        } else {
            // 加载图片时也对thumb做非空处理
            Glide.with(mContext)
                    .load(thumb != null ? thumb : R.drawable.euphoria_place_holder) // 增加null判断
                    .error(R.drawable.euphoria_place_holder)
                    .placeholder(R.drawable.euphoria_place_holder)
                    .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(beanDetails);
                }
            }
        });
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, DownloadXML.class);
                intentApply.putExtra(DownloadXML.SOURCE_KEY, beanDetails);
                intentApply.putExtra(DownloadXML.DISPLAY_URL_KEY, beanDetails.getImgPath());
                intentApply.putExtra(DownloadXML.ZIP_URL_KEY, beanDetails.getZipPath());
                intentApply.putExtra(DownloadXML.NAME_KEY, beanDetails.getTitleName());
                intentApply.putExtra(DownloadXML.GIF_KEY, beanDetails.getImgGif());
                String intent_thumb;
                if (thumbGif != null && !thumbGif.isEmpty()) {  // 同样增加非空判断
                    intent_thumb = thumbGif;
                } else {
                    intent_thumb = thumb != null ? thumb : "";  // 避免thumb为null的情况
                }
                intentApply.putExtra(DownloadXML.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);

        }

    }

}
