package com.keyborad.theme.trendyborad.trendylistutils;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.keyborad.theme.trendyborad.R;
import com.keyborad.theme.trendyborad.bean.BeanDetails;
import com.keyborad.theme.trendyborad.databinding.AdapterNwCyImTrendyBinding;
import com.keyborad.theme.trendyborad.trendyuiactivity.TrendySetKeyboardActivity;
import com.keyborad.theme.trendyborad.trendyutils.TrendyCommon;
import com.bumptech.glide.Glide;
import java.util.List;

// 新的分类列表适配器（对齐HomeChildAdapter的图片/点击逻辑）
public class TrendyNewCategoryAdapter extends RecyclerView.Adapter<TrendyNewCategoryAdapter.ViewHolder> {
    private final Context mContext;
    private final List<BeanDetails> mDataList;
    private OnItemClickListener mOnItemClickListener;

    // 构造方法
    public TrendyNewCategoryAdapter(Context context, List<BeanDetails> dataList) {
        this.mContext = context;
        this.mDataList = dataList;
    }

    // ViewHolder类
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final AdapterNwCyImTrendyBinding binding;

        public ViewHolder(@NonNull AdapterNwCyImTrendyBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载新的Item布局
        AdapterNwCyImTrendyBinding binding = AdapterNwCyImTrendyBinding.inflate(
                LayoutInflater.from(mContext),
                parent,
                false
        );
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 空数据保护
        if (mDataList == null || mDataList.isEmpty() || position >= mDataList.size()) {
            // 空数据时显示占位图
            holder.binding.ivKeyboard.setImageResource(R.drawable.err_load);
            return;
        }

        BeanDetails item = mDataList.get(position);

        // ========== 1. 完全对齐HomeChildAdapter的图片加载逻辑 ==========
        String thumbGif = item.getThumbGif(); // GIF缩略图地址
        String thumbUrl = item.getThumbUrl(); // 静态图缩略图地址

        // 优先级：GIF图 > 静态图 > 占位图
        if (thumbGif != null && !thumbGif.isEmpty()) {
            // 加载GIF图（用项目自定义的Common工具类）
            TrendyCommon.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.ivKeyboard);
        } else if (thumbUrl != null && !thumbUrl.isEmpty()) {
            // 加载静态图（用Glide，和HomeChildAdapter一致）
            Glide.with(mContext)
                    .load(thumbUrl)
                    .placeholder(R.drawable.err_load) // 加载中占位图
                    .error(R.drawable.err_load)       // 加载失败占位图
                    .into(holder.binding.ivKeyboard);
        } else {
            // 无图片地址时显示占位图
            holder.binding.ivKeyboard.setImageResource(R.drawable.err_load);
        }

        // ========== 2. 文字描述：用BeanDetails的titleName字段（和HomeChildAdapter一致） ==========
        String title = item.getTitleName();
        if (title != null && !title.isEmpty()) {
            holder.binding.tvName.setText(title);
        } else {
            holder.binding.tvName.setText(""); // 无标题时置空
        }

        // ========== 3. 点击事件：对齐HomeChildAdapter，跳转到SetKeyboardActivity ==========
        holder.itemView.setOnClickListener(v -> {
            // 先触发自定义点击回调（如果设置了）
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(position, item);
            }

            // 再执行和HomeChildAdapter一致的跳转逻辑
            Intent intent = new Intent(mContext, TrendySetKeyboardActivity.class);
            intent.putExtra(TrendySetKeyboardActivity.SOURCE_KEY, item);
            intent.putExtra(TrendySetKeyboardActivity.DISPLAY_URL_KEY, item.getImgPath());
            intent.putExtra(TrendySetKeyboardActivity.ZIP_URL_KEY, item.getZipPath());
            intent.putExtra(TrendySetKeyboardActivity.NAME_KEY, item.getTitleName());
            intent.putExtra(TrendySetKeyboardActivity.GIF_KEY, item.getImgGif());

            String intentThumb = (thumbGif != null && !thumbGif.isEmpty()) ? thumbGif : thumbUrl;
            intent.putExtra(TrendySetKeyboardActivity.THUMB_KEY, intentThumb);

            // 非Activity上下文需要添加FLAG
            if (!(mContext instanceof android.app.Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    // 点击事件接口（保留自定义扩展能力）
    public interface OnItemClickListener {
        void onItemClick(int position, BeanDetails item);
    }

    // 设置点击事件的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}