package com.draw.painting.canvas.adapters;

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.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.draw.painting.canvas.R;
import com.draw.painting.canvas.core.SystemHelper;
import com.draw.painting.canvas.onClickListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SketchCategoryAdapter extends RecyclerView.Adapter<SketchCategoryAdapter.CategoryVH> {

    private Context mContext;
    private List<String> categoryList;
    private onClickListener listener;
    private Map<String, Integer> iconMap;
    private Map<String, Integer> colorMap;

    public SketchCategoryAdapter(Context context, List<String> categoryList) {
        this.mContext = context;
        this.categoryList = categoryList;
        initIconMap();
        initColorMap();
    }

    public void setListener(onClickListener listener) {
        this.listener = listener;
    }

    private void initIconMap() {
        iconMap = new HashMap<>();
        // 为各分类设置简洁线条图标
        iconMap.put("Animals", R.drawable.ic_category_animals);
        iconMap.put("Birds", R.drawable.ic_category_birds);
        iconMap.put("Butterfly", R.drawable.ic_category_butterfly);
        iconMap.put("Cafe", R.drawable.ic_category_cafe);
        iconMap.put("Cartoons", R.drawable.ic_category_cartoons);
        iconMap.put("Christmas", R.drawable.ic_category_christmas);
        iconMap.put("Festivals", R.drawable.ic_category_festivals);
        iconMap.put("Flowers", R.drawable.ic_category_flowers);
        iconMap.put("Fruits", R.drawable.ic_category_fruits);
        iconMap.put("General", R.drawable.ic_category_general);
        iconMap.put("Mehndi", R.drawable.ic_category_mehndi);
        iconMap.put("Nature", R.drawable.ic_category_nature);
        iconMap.put("Properties", R.drawable.ic_category_properties);
        iconMap.put("Rangoli", R.drawable.ic_category_rangoli);
        iconMap.put("Sports", R.drawable.ic_category_sports);
        iconMap.put("Vehicles", R.drawable.ic_category_vehicles);
    }
    
    private void initColorMap() {
        colorMap = new HashMap<>();
        // 🎨 为不同分类设置差异性更强的颜色（增强识别度）
        colorMap.put("Animals", R.color.accent_deep_blue);        // 深蓝 - 动物（更具差异性）
        colorMap.put("Birds", R.color.accent_green);              // 绿色 - 鸟类
        colorMap.put("Butterfly", R.color.accent_purple);         // 紫色 - 蝴蝶（更具差异性）
        colorMap.put("Flowers", R.color.accent_coral);            // 珊瑚粉 - 花卉
        colorMap.put("Fruits", R.color.primary_warm_yellow);      // 温暖黄 - 水果
        colorMap.put("Nature", R.color.accent_green);             // 绿色 - 自然
        colorMap.put("Cartoons", R.color.accent_yellow);          // 黄色 - 卡通
        colorMap.put("Christmas", R.color.accent_coral);          // 珊瑚粉 - 圣诞
        colorMap.put("Festivals", R.color.accent_purple);         // 紫色 - 节日
        colorMap.put("Cafe", R.color.accent_dark);                // 深蓝 - 咖啡厅
        colorMap.put("Sports", R.color.accent_deep_blue);         // 深蓝 - 运动
        colorMap.put("Vehicles", R.color.accent_dark);            // 深蓝 - 车辆
        colorMap.put("Mehndi", R.color.accent_coral);             // 珊瑚粉 - 曼海蒂
        colorMap.put("Rangoli", R.color.primary_warm_yellow);     // 黄色 - 彩绘
        colorMap.put("Properties", R.color.accent_dark);          // 深蓝 - 属性
        colorMap.put("General", R.color.accent_deep_blue);        // 深蓝 - 通用
    }

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

    @Override
    public void onBindViewHolder(@NonNull CategoryVH holder, int position) {
        String dir = categoryList.get(position);
        String categoryName = dir.substring(dir.lastIndexOf("_") + 1);
        holder.tvCategoryName.setText(categoryName);
        
        // 设置线条图标
        Integer iconRes = iconMap.get(categoryName);
        if (iconRes != null) {
            holder.ivCategoryIcon.setImageResource(iconRes);
        } else {
            holder.ivCategoryIcon.setImageResource(R.drawable.ic_category_general);
        }
        
        // 🎨 设置图标颜色（Logo风格多彩配色）
        Integer colorRes = colorMap.get(categoryName);
        if (colorRes != null) {
            holder.ivCategoryIcon.setColorFilter(ContextCompat.getColor(mContext, colorRes));
        } else {
            holder.ivCategoryIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.primary_soft_blue));
        }
        
        // 显示 sketch 数量
        List<String> files = SystemHelper.fileExistsInAssets(mContext, dir);
        int count = files.size();
        String countText = count + (count == 1 ? " sketch" : " sketches");
        holder.tvSketchCount.setText(countText);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClickItem(true, dir);
                }
            }
        });
    }

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

    public static class CategoryVH extends RecyclerView.ViewHolder {
        private TextView tvCategoryName;
        private TextView tvSketchCount;
        private ImageView ivCategoryIcon;

        public CategoryVH(@NonNull View itemView) {
            super(itemView);
            tvCategoryName = itemView.findViewById(R.id.tv_category_name);
            tvSketchCount = itemView.findViewById(R.id.tv_sketch_count);
            ivCategoryIcon = itemView.findViewById(R.id.iv_category_icon);
        }
    }
}

