package com.app.intelligent.board.uiadapter;

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

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

public class HomeItemSubAdapter extends RecyclerView.Adapter<HomeItemSubAdapter.ChildViewHolder> {

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

    public HomeItemSubAdapter(Context context, List<KeyboardItemInfo> list) {
        mContext = context;
        this.mList = list;
    }

    @NonNull
    @Override
    public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 核心修复：添加 parent 和 attachToRoot=false，让布局受父容器约束
        AdapterHomeItemSubBinding inflate = AdapterHomeItemSubBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false // 关键：禁止直接附加到父容器，由RecyclerView管理
        );
        return new ChildViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) {
        KeyboardItemInfo beanDetails = mList.get(position);

        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();
        if (!thumbGif.isEmpty()) {
            AppCommonUtils.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.imageView);
        } else {
            Glide.with(mContext).load(thumb)
                    .error(R.drawable.place_holder)
                    .placeholder(R.drawable.place_holder)
                    .into(holder.binding.imageView);
        }

        holder.binding.fragme.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.isEmpty() ? thumbGif : thumb;
            intentApply.putExtra(KeyboardSettingActivity.THUMB_KEY, intent_thumb);
            mContext.startActivity(intentApply);
        });
    }

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

    public static class ChildViewHolder extends RecyclerView.ViewHolder {
        private AdapterHomeItemSubBinding binding;

        public ChildViewHolder(@NonNull AdapterHomeItemSubBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }
}