package com.app.input.personalised.board.listhelpers;


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.bumptech.glide.Glide;
import com.app.input.personalised.board.R;
import com.app.input.personalised.board.databean.KeyboardTheme;
import com.app.input.personalised.board.helpers.AppUtils;
import com.app.input.personalised.board.itemback.FavoriteRemovedListener;
import com.app.input.personalised.board.boartactivity.ThemeDetailActivity;

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

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

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

    private FavoriteRemovedListener mCallBack;

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

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


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

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

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

    @Override
    public void onBindViewHolder(@NonNull ForYouViewHolder holder, int position) {
        KeyboardTheme beanDetails = mList.get(position);
        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();
        if (!thumbGif.isEmpty()) {
            AppUtils.INSTANCE.loadWepJif(mContext, thumbGif, holder.itemImg);
        } else {
            Glide.with(mContext)
                    .load(thumb).error(R.drawable.placeholder)
                    .placeholder(R.drawable.placeholder).into(holder.itemImg);
        }
        holder.itemFavorite.setSelected(true);
        holder.layoutFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 先调用回调删除数据库中的收藏
                if (mCallBack != null) {
                    mCallBack.OnRemoveLike(beanDetails);
                }
                // 注意：不需要手动调用 notifyItemRemoved
                // 因为 LiveData 会自动更新列表，适配器会通过 setForYouList 重新设置数据
            }
        });
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ThemeDetailActivity.class);
                intentApply.putExtra(ThemeDetailActivity.SOURCE_KEY, beanDetails);
                intentApply.putExtra(ThemeDetailActivity.DISPLAY_URL_KEY, beanDetails.getImgPath());
                intentApply.putExtra(ThemeDetailActivity.ZIP_URL_KEY, beanDetails.getZipPath());
                intentApply.putExtra(ThemeDetailActivity.NAME_KEY, beanDetails.getTitleName());
                intentApply.putExtra(ThemeDetailActivity.GIF_KEY, beanDetails.getImgGif());
                String intent_thumb;
                if (!thumbGif.isEmpty()) {
                    intent_thumb = thumbGif;
                } else {
                    intent_thumb = thumb;
                }
                intentApply.putExtra(ThemeDetailActivity.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);

        }

    }

}
