package com.flowing.light.board.adapter;


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

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

import com.flowing.light.board.R;
import com.flowing.light.board.bean.BeanDetails;
import com.flowing.light.board.callback.OnItemClickCallback;
import com.flowing.light.board.activity.SetActivity;
import com.bumptech.glide.Glide;
import com.flowing.light.board.tools.Common;

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

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

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

    private OnItemClickCallback mCallBack;

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

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

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

        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_set, 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.isEmpty()) {
            Common.INSTANCE.loadWepJif(mContext, thumbGif, holder.itemImg);
        } else {
            Glide.with(mContext).load(thumb).into(holder.itemImg);
        }

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, SetActivity.class);
                intentApply.putExtra(SetActivity.SOURCE_KEY, beanDetails);
                intentApply.putExtra(SetActivity.DISPLAY_URL_KEY, beanDetails.getImgPath());
                intentApply.putExtra(SetActivity.ZIP_URL_KEY, beanDetails.getZipPath());
                intentApply.putExtra(SetActivity.NAME_KEY, beanDetails.getTitleName());
                intentApply.putExtra(SetActivity.GIF_KEY, beanDetails.getImgGif());
                String intent_thumb;
                if (!thumbGif.isEmpty()) {
                    intent_thumb = thumbGif;
                } else {
                    intent_thumb = thumb;
                }
                intentApply.putExtra(SetActivity.THUMB_KEY, intent_thumb);
                mContext.startActivity(intentApply);
                if (mCallBack != null) {
                    mCallBack.OnItemClickListener();
                }

            }
        });


    }

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

    public static class ForYouViewHolder extends RecyclerView.ViewHolder {

        private CardView cardView;
        private ImageView itemImg;

        public ForYouViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            itemImg = itemView.findViewById(R.id.imPreview);

        }

    }
}
