package com.draw.painting.line.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

import com.draw.painting.line.DrawingApp;
import com.draw.painting.line.adapter.ImageListAdapter;
import com.draw.painting.line.databinding.ActivityGalleryBinding;
import com.draw.painting.line.manager.AppHelper;

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

public class GalleryActivity extends AppCompatActivity {

    private ActivityGalleryBinding preViewBinding;

    private String dirStr;
    
    // 分类名称映射（儿童友好）
    private static final Map<String, String> categoryNames = new HashMap<String, String>() {{
        put("Animals", "Cute Animals");
        put("Birds", "Birds");
        put("Butterfly", "Butterfly");
        put("Cafe", "Cafe");
        put("Cartoons", "Cartoons");
        put("Christmas", "Christmas");
        put("Festivals", "Festivals");
        put("Flowers", "Flowers");
        put("Fruits", "Fruits");
        put("General", "General");
        put("Mehndi", "Mehndi");
        put("Nature", "Nature");
        put("Properties", "Properties");
        put("Rangoli", "Rangoli");
        put("Sports", "Sports");
        put("Vehicles", "Vehicles");
    }};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppHelper.enableEdgeToEdge(this);
        preViewBinding = ActivityGalleryBinding.inflate(getLayoutInflater());
        setContentView(preViewBinding.getRoot());
//        AppHelper.setupWindowInsetsListener(preViewBinding.getRoot());

        AppHelper.setupBottomNavPadding(preViewBinding.getRoot());

        dirStr = getIntent().getStringExtra(DrawingApp.Key_ViewDir);
        
        // 设置儿童友好的标题
        String categoryName = categoryNames.get(dirStr);
        if (categoryName == null) {
            categoryName = dirStr.substring(dirStr.lastIndexOf("_")+1);
        }
        preViewBinding.categoryName.setText(categoryName);
        
        onInitRecommended();
        onInitList();
        onInitClick();
    }

    private void onInitRecommended() {
        List<String> allList = AppHelper.fileExistsInAssets(this, dirStr);
        if (!allList.isEmpty()) {
            // 显示第一张作为推荐
            String recommendedPath = allList.get(0);
            Bitmap bitmap = AppHelper.loadImageFromAssets(this, recommendedPath);
            if (bitmap != null) {
                preViewBinding.imRecommended.setImageBitmap(bitmap);
            }
            
            // 推荐卡片点击
            preViewBinding.cardRecommended.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(GalleryActivity.this, DrawActivity.class);
                    intent.putExtra(DrawingApp.Key_Camera, recommendedPath);
                    startActivity(intent);
                }
            });
        }
    }

    private void onInitList() {
        List<String> allList = AppHelper.fileExistsInAssets(this, dirStr);
        
        if (allList.isEmpty()) {
            return;
        }
        
        // 将列表分为Easy和Fun两组
        int midPoint = allList.size() / 2;
        List<String> easyList = new ArrayList<>(allList.subList(0, midPoint));
        List<String> funList = new ArrayList<>(allList.subList(midPoint, allList.size()));
        
        // Easy组
        ImageListAdapter easyAdapter = new ImageListAdapter(this, easyList);
        preViewBinding.recyclerviewEasy.setAdapter(easyAdapter);
        preViewBinding.recyclerviewEasy.setLayoutManager(new GridLayoutManager(this, 3));
        
        // Fun组
        ImageListAdapter funAdapter = new ImageListAdapter(this, funList);
        preViewBinding.recyclerview.setAdapter(funAdapter);
        preViewBinding.recyclerview.setLayoutManager(new GridLayoutManager(this, 3));
    }

    private void onInitClick() {
        preViewBinding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

