package com.draw.painting.line.adapter;

import android.content.Context;
import android.util.Log;
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.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.drawable.Drawable;

import com.draw.painting.line.R;
import com.draw.painting.line.manager.CategoryConfig;
import com.draw.painting.line.onClickListener;

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

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

    private List<String> nameList = CategoryConfig.getAllDir();
    private onClickListener listener;
    private Context mCon;

    // 分类图标和描述映射
    private static final Map<String, String[]> categoryInfo = new HashMap<String, String[]>() {{
        put("Animals", new String[]{"🐶", "Cute Animals", "Cute animals to draw!"});
        put("Birds", new String[]{"🐦", "Birds", "Let them fly!"});
        put("Butterfly", new String[]{"🦋", "Butterfly", "So pretty!"});
        put("Cafe", new String[]{"☕", "Cafe", "Yummy drawings!"});
        put("Cartoons", new String[]{"🎭", "Cartoons", "Fun characters!"});
        put("Christmas", new String[]{"🎄", "Christmas", "Holiday fun!"});
        put("Festivals", new String[]{"🎉", "Festivals", "Celebrate!"});
        put("Flowers", new String[]{"🌸", "Flowers", "Beautiful blooms!"});
        put("Fruits", new String[]{"🍎", "Fruits", "Sweet and juicy!"});
        put("General", new String[]{"🎨", "General", "All kinds of fun!"});
        put("Mehndi", new String[]{"✋", "Mehndi", "Beautiful patterns!"});
        put("Nature", new String[]{"🌳", "Nature", "Explore outdoors!"});
        put("Properties", new String[]{"🏠", "Properties", "Build and create!"});
        put("Rangoli", new String[]{"🪷", "Rangoli", "Colorful designs!"});
        put("Sports", new String[]{"⚽", "Sports", "Let's play!"});
        put("Vehicles", new String[]{"🚗", "Vehicles", "Zoom zoom!"});
    }};

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

    public CategoryListAdapter(Context mCon) {
        this.mCon = mCon;
    }

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

    @Override
    public void onBindViewHolder(@NonNull CategoryVH holder, int position) {
        String dir = nameList.get(position);
        Log.d("-------------tt", "------------dir=" + dir);
        
        // 获取分类信息
        String[] info = categoryInfo.get(dir);
        if (info != null) {
            holder.tvIcon.setText(info[0]);
            holder.tvName.setText(info[1]);
            holder.tvDescription.setText(info[2]);
        } else {
            // 默认值
            String categoryName = dir.substring(dir.lastIndexOf("_") + 1);
            holder.tvIcon.setText("🎨");
            holder.tvName.setText(categoryName);
            holder.tvDescription.setText(R.string.let_s_draw);
        }

        // 第一个分类卡片更突出（降低饱和度，作为推荐分类而非主行动）
        boolean isFirst = position == 0;
        CardView cardView = (CardView) holder.itemView;
        if (isFirst) {
            // 标题字号更大
            holder.tvName.setTextSize(22);
            // 卡片背景：使用带描边的highlight背景
            Drawable highlightBg = ContextCompat.getDrawable(mCon, R.drawable.card_bg_highlight);
            cardView.setBackground(highlightBg);
            // 增加最小高度
            View constraintLayout = cardView.findViewById(R.id.constraint_layout);
            if (constraintLayout != null) {
                constraintLayout.setMinimumHeight((int) (180 * 1.1)); // 增加10%高度
            }
        } else {
            // 普通卡片样式：使用带描边的普通背景
            holder.tvName.setTextSize(20);
            Drawable normalBg = ContextCompat.getDrawable(mCon, R.drawable.card_bg);
            cardView.setBackground(normalBg);
        }

        // 装饰线稿（半透明）
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mCon);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        holder.preRecycler.setLayoutManager(linearLayoutManager);
        CategoryThumbAdapter categoryThumbAdapter = new CategoryThumbAdapter(mCon, dir);
        categoryThumbAdapter.setListener(listener);
        holder.preRecycler.setAdapter(categoryThumbAdapter);

        // 整个卡片可点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClickItem(true, dir);
                }
            }
        });
    }

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

    public static class CategoryVH extends RecyclerView.ViewHolder {
        private TextView tvIcon;
        private TextView tvName;
        private TextView tvDescription;
        private ImageView ivArrow;
        private RecyclerView preRecycler;

        public CategoryVH(@NonNull View itemView) {
            super(itemView);
            tvIcon = itemView.findViewById(R.id.tv_icon);
            tvName = itemView.findViewById(R.id.tv_name);
            tvDescription = itemView.findViewById(R.id.tv_description);
            ivArrow = itemView.findViewById(R.id.iv_arrow);
            preRecycler = itemView.findViewById(R.id.pre_recycler);
        }
    }
}

