package com.ytpm.advertise.enums;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public enum AppCategoryEnum {
    ANDROID(
        "Android", 
        Arrays.asList(
            new PrimaryCategory("App", Arrays.asList(
                "Daydream", "Android Wear", "Art & Design", "Auto & Vehicles", "Beauty", 
                "Books & Reference", "Business", "Comics", "Communication", "Dating", 
                "Education", "Entertainment", "Events", "Finance", "Food & Drink", 
                "Health & Fitness", "House & Home", "Libraries & Demo", "Lifestyle", 
                "Maps & Navigation", "Medical", "Music & Audio", "News & Magazines", 
                "Parenting", "Personalisation", "Photography", "Productivity", "Shopping", 
                "Social", "Sports", "Tools", "Travel & Local", "Video Players & Editors", "Weather"
            )),
            new PrimaryCategory("Game", Arrays.asList(
                "Action", "Adventure", "Arcade", "Board", "Card", "Casino", "Casual", 
                "Educational", "Music", "Puzzle", "Racing", "Role Playing", "Simulation", 
                "Sports", "Strategy", "Trivia", "Word"
            )),
            new PrimaryCategory("Family", Arrays.asList(
                "Ages 5 & Under", "Ages 6-8", "Ages 9 & Over", "Action & Adventure", 
                "Brain Games", "Creativity", "Education", "Music and video", "Pretend play"
            ))
        )
    ),
    
    IOS(
        "iOS", 
        Arrays.asList(
            new PrimaryCategory("App", Arrays.asList(
                "Books", "Business", "Catalogs", "Education", "Entertainment", "Finance", 
                "Food & Drink", "Health & Fitness", "Lifestyle", "Magazines & Newspapers", 
                "Medical", "Music", "Navigation", "News", "Photo & Video", "Productivity", 
                "Reference", "Shopping", "Social Networking", "Sports", "Stickers", "Travel", 
                "Utilities", "Weather"
            )),
            new PrimaryCategory("Game", Arrays.asList(
                "Action", "Adventure", "Arcade", "Board", "Card", "Casino", "Dice", 
                "Educational", "Family", "Music", "Puzzle", "Racing", "Role Playing", 
                "Simulation", "Sports", "Strategy", "Trivia", "Word"
            ))
        )
    );

    private final String platform;
    private final List<PrimaryCategory> primaryCategories;

    AppCategoryEnum(String platform, List<PrimaryCategory> primaryCategories) {
        this.platform = platform;
        this.primaryCategories = primaryCategories;
    }

    public String getPlatform() {
        return platform;
    }

    public List<PrimaryCategory> getPrimaryCategories() {
        return primaryCategories;
    }

    /**
     * 静态内部类：一级分类（包含二级分类列表）
     */
    public static class PrimaryCategory {
        private final String name;
        private final List<String> secondaryCategories;

        public PrimaryCategory(String name, List<String> secondaryCategories) {
            this.name = name;
            this.secondaryCategories = secondaryCategories;
        }

        public String getName() {
            return name;
        }

        public List<String> getSecondaryCategories() {
            return secondaryCategories;
        }
    }

    /**
     * 接口1：根据平台获取所有一级分类名称
     * 
     * @param platform 平台名称 ("Android" 或 "iOS")
     * @return 一级分类名称列表（如果没有匹配平台则返回空列表）
     */
    public static List<String> getPrimaryCategories(String platform) {
        return findPlatform(platform)
                .map(appCategoryEnum ->
                    appCategoryEnum.getPrimaryCategories().stream()
                        .map(PrimaryCategory::getName)
                        .collect(Collectors.toList()))
                .orElse(Collections.emptyList());
    }

    /**
     * 接口2：根据平台和一级分类获取所有二级分类
     * 
     * @param platform 平台名称 ("Android" 或 "iOS")
     * @param primaryCategory 一级分类名称
     * @return 二级分类名称列表（如果没有匹配则返回空列表）
     */
    public static List<String> getSecondaryCategories(String platform, String primaryCategory) {
        return findPlatform(platform)
                .flatMap(appCategoryEnum ->
                    appCategoryEnum.getPrimaryCategories().stream()
                        .filter(pc -> pc.getName().equalsIgnoreCase(primaryCategory))
                        .findFirst()
                        .map(PrimaryCategory::getSecondaryCategories))
                .orElse(Collections.emptyList());
    }

    /**
     * 辅助方法：根据平台名称查找枚举
     */
    private static Optional<AppCategoryEnum> findPlatform(String platform) {
        return Arrays.stream(values())
                .filter(app -> app.getPlatform().equalsIgnoreCase(platform))
                .findFirst();
    }

    // 使用示例
    public static void main(String[] args) {
        // 测试接口1：获取Android平台的一级分类
        System.out.println("Android平台一级分类:");
        List<String> androidPrimary = getPrimaryCategories("Android");
        androidPrimary.forEach(System.out::println);

        // 测试接口1：获取iOS平台的一级分类
        System.out.println("\niOS平台一级分类:");
        List<String> iosPrimary = getPrimaryCategories("iOS");
        iosPrimary.forEach(System.out::println);

        // 测试接口2：获取Android平台的Game分类下的二级分类
        System.out.println("\nAndroid平台Game分类的二级分类:");
        List<String> androidGameSecondary = getSecondaryCategories("Android", "Game");
        androidGameSecondary.forEach(System.out::println);

        // 测试接口2：获取iOS平台的App分类下的二级分类
        System.out.println("\niOS平台App分类的二级分类:");
        List<String> iosAppSecondary = getSecondaryCategories("iOS", "App");
        iosAppSecondary.forEach(System.out::println);

        // 测试无效输入
        System.out.println("\n测试无效平台:");
        List<String> invalidPlatform = getPrimaryCategories("Windows");
        System.out.println("结果: " + (invalidPlatform.isEmpty() ? "空列表" : invalidPlatform));

        System.out.println("\n测试无效一级分类:");
        List<String> invalidCategory = getSecondaryCategories("Android", "Invalid");
        System.out.println("结果: " + (invalidCategory.isEmpty() ? "空列表" : invalidCategory));
    }
}