package com.flowing.light.board.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.flowing.light.board.R;
import com.flowing.light.board.bean.BeanDetails;
import com.bumptech.glide.Glide;
import java.util.List;

/**
 * 键盘列表项适配器
 */
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.KeyboardViewHolder> {

    private Context mContext;
    private List<BeanDetails> mKeyboardList;
    private OnItemClickListener mListener;

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void onItemClick(BeanDetails bean);
    }

    /**
     * 设置点击监听器
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    /**
     * 构造方法
     */
    public HomeAdapter(Context context, List<BeanDetails> keyboardList) {
        this.mContext = context;
        this.mKeyboardList = keyboardList;
    }

    @NonNull
    @Override
    public KeyboardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载item布局
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.adapter_home, parent, false);
        return new KeyboardViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull KeyboardViewHolder holder, int position) {
        if (mKeyboardList == null || mKeyboardList.isEmpty()) {
            return;
        }

        BeanDetails bean = mKeyboardList.get(position);
        if (bean == null) {
            return;
        }

        // 设置键盘名称
        holder.keyboardName.setText(bean.getTitleName() != null ? bean.getTitleName() : "");

        // 加载缩略图（使用Glide库）
        String thumbUrl = bean.getThumbUrl();
        if (thumbUrl != null && !thumbUrl.isEmpty()) {
            Glide.with(mContext)
                    .load(thumbUrl)
                    .placeholder(R.mipmap.place_holder) // 占位图
                    .error(R.mipmap.error) // 错误图
                    .into(holder.keyboardThumb);
        } else {
            holder.keyboardThumb.setImageResource(R.mipmap.error);
        }

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onItemClick(bean);
            }
        });
    }

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

    /**
     * 视图持有者
     */
    public static class KeyboardViewHolder extends RecyclerView.ViewHolder {
        ImageView keyboardThumb;
        TextView keyboardName;

        public KeyboardViewHolder(@NonNull View itemView) {
            super(itemView);
            keyboardThumb = itemView.findViewById(R.id.iv_keyboard_thumb);
            keyboardName = itemView.findViewById(R.id.tv_keyboard_name);
        }
    }
}
