package com.joyful.euphoria.board.recyclerview;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.joyful.euphoria.board.R;
import com.joyful.euphoria.board.beaned.BeanDetails;
import com.joyful.euphoria.board.beaned.CateFullData;
import java.util.List;

public class RecyclerviewHomePage extends RecyclerView.Adapter<RecyclerviewHomePage.ViewHolder> {
    private Context mContext;
    private List<CateFullData> mCateFullList; // 存储分类完整数据（含所有键盘）
    private OnCateTitleClickListener mTitleListener; // 分类标题/箭头点击事件
    private OnKeyboardImgClickListener mImgListener; // 键盘图片点击事件
    // 移除Handler声明，因为不再需要自动滑动

    // 点击事件接口
    public interface OnCateTitleClickListener {
        void onCateTitleClick(CateFullData cateFullData); // 传递完整分类数据
    }

    public interface OnKeyboardImgClickListener {
        void onKeyboardImgClick(BeanDetails bean); // 传递单个键盘数据
    }

    public RecyclerviewHomePage(Context context, List<CateFullData> cateFullList) {
        mContext = context;
        mCateFullList = cateFullList;
    }

    // Setter方法
    public void setOnCateTitleClickListener(OnCateTitleClickListener listener) {
        mTitleListener = listener;
    }

    public void setOnKeyboardImgClickListener(OnKeyboardImgClickListener listener) {
        mImgListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CateFullData cateFullData = mCateFullList.get(position);
        List<BeanDetails> keyboardList = cateFullData.getKeyboardList();
        if (keyboardList == null || keyboardList.isEmpty()) return;

        // 1. 绑定分类名称
        holder.tvCateName.setText(cateFullData.getCategoryName());

        // 2. 设置ViewPager2适配器
        RecyclerviewBoardPager pagerAdapter = new RecyclerviewBoardPager(keyboardList);
        holder.vpKeyboard.setAdapter(pagerAdapter);

        // 3. 初始显示第一个键盘名称
        holder.tvKeyboardName.setText(keyboardList.get(0).getTitleName() != null ?
                keyboardList.get(0).getTitleName() : "unknown keyboard");

        // 4. 监听滑动事件更新标题
        holder.vpKeyboard.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                // 增加索引校验：确保position在有效范围内
                if (position >= 0 && position < keyboardList.size()) {
                    BeanDetails currentBean = keyboardList.get(position);
                    holder.tvKeyboardName.setText(currentBean.getTitleName() != null ?
                            currentBean.getTitleName() : "unknown keyboard");
                }
            }
        });


        // 5. 分类标题点击事件
        holder.llCateTitle.setOnClickListener(v -> {
            if (mTitleListener != null) {
                mTitleListener.onCateTitleClick(cateFullData);
            }
        });

        // 6. 图片点击事件
        pagerAdapter.setOnItemClickListener(bean -> {
            if (mImgListener != null) {
                mImgListener.onKeyboardImgClick(bean);
            }
        });
    }


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

    // ViewHolder绑定新布局控件
    public static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout llCateTitle;
        TextView tvCateName;
        ImageView ivArrowRight;
        ViewPager2 vpKeyboard;
        TextView tvKeyboardName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            llCateTitle = itemView.findViewById(R.id.ll_cate_title);
            tvCateName = itemView.findViewById(R.id.tv_home_cate_name);
            ivArrowRight = itemView.findViewById(R.id.iv_arrow_right);
            vpKeyboard = itemView.findViewById(R.id.vp_keyboard);  // 初始化ViewPager2
            tvKeyboardName = itemView.findViewById(R.id.tv_keyboard_name);
        }
    }

    // 销毁
    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);

    }
}