package com.app.intelligent.board.uiadapter;

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.intelligent.board.uimodel.KeyboardItemInfo;
import com.bumptech.glide.Glide;
import com.app.intelligent.board.R;
import com.app.intelligent.board.utils.AppCommonUtils;
import com.app.intelligent.board.uilistener.FavoriteItemDeleteListener;
import com.app.intelligent.board.uiactivity.KeyboardSettingActivity;

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

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

    private Context mContext;
    private List<KeyboardItemInfo> mList = new ArrayList<>();
    private FavoriteItemDeleteListener mCallBack;

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

    public void setForYouList(List<KeyboardItemInfo> list) {
        this.mList = (list == null) ? new ArrayList<>() : list;
        notifyDataSetChanged();
    }

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

    @NonNull
    @Override
    public ForYouViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_favorite_item, parent, false);
        return new ForYouViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ForYouViewHolder holder, int position) {
        KeyboardItemInfo beanDetails = mList.get(position);
        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();

        // 加载图片
        if (thumbGif != null && !thumbGif.isEmpty()) {
            AppCommonUtils.INSTANCE.loadWepJif(mContext, thumbGif, holder.itemImg);
        } else {
            Glide.with(mContext)
                    .load(thumb)
                    .error(R.drawable.place_holder)
                    .placeholder(R.drawable.place_holder)
                    .into(holder.itemImg);
        }

        // 收藏按钮：仅设置选中状态（删除原点击监听）
        holder.itemFavorite.setSelected(true);

        // 清除按钮逻辑：仅保留清除按钮的删除逻辑
        holder.layoutClear.setOnClickListener(v -> {
            handleRemoveLike(holder, beanDetails);
        });

        // 条目点击跳转
        holder.cardView.setOnClickListener(v -> {
            Intent intentApply = new Intent(mContext, KeyboardSettingActivity.class);
            intentApply.putExtra(KeyboardSettingActivity.SOURCE_KEY, beanDetails);
            intentApply.putExtra(KeyboardSettingActivity.DISPLAY_URL_KEY, beanDetails.getImgPath());
            intentApply.putExtra(KeyboardSettingActivity.ZIP_URL_KEY, beanDetails.getZipPath());
            intentApply.putExtra(KeyboardSettingActivity.NAME_KEY, beanDetails.getTitleName());
            intentApply.putExtra(KeyboardSettingActivity.GIF_KEY, beanDetails.getImgGif());
            String intent_thumb = (thumbGif != null && !thumbGif.isEmpty()) ? thumbGif : thumb;
            intentApply.putExtra(KeyboardSettingActivity.THUMB_KEY, intent_thumb);
            mContext.startActivity(intentApply);
        });
    }

    // 封装删除逻辑
    private void handleRemoveLike(ForYouViewHolder holder, KeyboardItemInfo beanDetails) {
        holder.itemFavorite.setSelected(false);
        int adapterPosition = holder.getAdapterPosition();
        if (adapterPosition != RecyclerView.NO_POSITION) {
            mList.remove(adapterPosition);
            notifyItemRemoved(adapterPosition);
            notifyItemRangeChanged(adapterPosition, mList.size());
            if (mCallBack != null) {
                mCallBack.OnRemoveLike(beanDetails);
            }
        }
    }

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

    public static class ForYouViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        FrameLayout layoutFavorite, layoutClear;
        ImageView itemImg, itemFavorite, imClear;

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