package com.wall.dream.wallpapers.ItemPag;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wall.dream.wallpapers.databinding.PagCategoryCardBinding;
import com.wall.dream.wallpapers.GJ.ObectBoxManagerGJ;
import com.wall.dream.wallpapers.LianXi.OnItemXianClick;
import com.wall.dream.wallpapers.data.DataBase;

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

/**
 * 宽卡片样式的分类适配器
 */
public class ItemCategoryPag extends RecyclerView.Adapter<ItemCategoryPag.CategoryCardHolder> {
    private List<String> categoryList = new ArrayList<>();
    private Context mContext;
    private OnItemXianClick listener;

    public void setData(List<String> data) {
        this.categoryList = data;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemXianClick onItemxianClick) {
        this.listener = onItemxianClick;
    }

    @NonNull
    @Override
    public CategoryCardHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        PagCategoryCardBinding binding = PagCategoryCardBinding.inflate(
                LayoutInflater.from(mContext), parent, false);
        return new CategoryCardHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull CategoryCardHolder holder, int position) {
        String categoryName = categoryList.get(position);
        
        // 设置分类名称（隐藏后面的Good）
        String displayName = removeGoodSuffix(categoryName);
        holder.binding.tvCategoryName.setText(displayName);
        
        // 获取该分类的壁纸数量
        List<DataBase> wallpapers = ObectBoxManagerGJ.queryHome(categoryName);
        int count = wallpapers != null ? wallpapers.size() : 0;

        
        // 根据分类名称设置不同的图标（可以扩展更多图标）
        setCategoryIcon(holder, categoryName);
        
        // 设置点击事件
        holder.binding.categoryCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClickMore(categoryName);
                }
            }
        });
        
        // 添加点击动画效果
        holder.binding.categoryCard.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case android.view.MotionEvent.ACTION_DOWN:
                    v.animate().scaleX(0.95f).scaleY(0.95f).setDuration(100).start();
                    break;
                case android.view.MotionEvent.ACTION_UP:
                case android.view.MotionEvent.ACTION_CANCEL:
                    v.animate().scaleX(1.0f).scaleY(1.0f).setDuration(100).start();
                    break;
            }
            return false;
        });
    }

    /**
     * 移除分类名称后面的"Good"后缀（不区分大小写）
     */
    private String removeGoodSuffix(String categoryName) {
        if (categoryName == null || categoryName.isEmpty()) {
            return categoryName;
        }
        // 使用正则表达式不区分大小写地移除末尾的"Good"
        return categoryName.replaceAll("(?i)good$", "");
    }

    /**
     * 根据分类名称设置对应的图标
     */
    private void setCategoryIcon(CategoryCardHolder holder, String categoryName) {
        // 这里可以根据不同的分类设置不同的图标
        // 目前使用默认的自然图标，后续可以扩展
        // 例如：
        // if (categoryName.toLowerCase().contains("nature")) {
        //     holder.binding.ivCategoryIcon.setImageResource(R.drawable.ic_category_nature);
        // } else if (categoryName.toLowerCase().contains("city")) {
        //     holder.binding.ivCategoryIcon.setImageResource(R.drawable.ic_category_city);
        // }
        
        // 暂时使用默认图标
        // holder.binding.ivCategoryIcon.setImageResource(R.drawable.ic_category_nature);
    }

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

    static class CategoryCardHolder extends RecyclerView.ViewHolder {
        private PagCategoryCardBinding binding;

        public CategoryCardHolder(@NonNull PagCategoryCardBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}


