package com.aegis.file.utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import com.aegis.file.enums.FileCategory;

/**
 * 文件分类工具类
 * 
 * @author aegis
 */
public class FileCategoryUtils {
    
    // 文件扩展名常量集合（Java 8兼容，使用HashSet）
    private static final Set<String> DOCUMENT_EXTENSIONS = new HashSet<>(Arrays.asList(
        "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", 
        "rtf", "odt", "ods", "odp", "pages", "numbers", "key"
    ));
    
    private static final Set<String> IMAGE_EXTENSIONS = new HashSet<>(Arrays.asList(
        "jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "ico", 
        "tiff", "tif", "psd", "ai", "eps"
    ));
    
    private static final Set<String> VIDEO_EXTENSIONS = new HashSet<>(Arrays.asList(
        "mp4", "avi", "mov", "wmv", "flv", "mkv", "mpeg", "mpg", 
        "3gp", "webm", "m4v", "rm", "rmvb", "vob"
    ));
    
    private static final Set<String> AUDIO_EXTENSIONS = new HashSet<>(Arrays.asList(
        "mp3", "wav", "flac", "aac", "ogg", "wma", "m4a", "ape", 
        "amr", "opus", "mid", "midi"
    ));
    
    private static final Set<String> ARCHIVE_EXTENSIONS = new HashSet<>(Arrays.asList(
        "zip", "rar", "7z", "tar", "gz", "bz2", "xz", "cab", 
        "iso", "dmg", "pkg"
    ));
    
    /**
     * 根据文件扩展名推断文件分类（返回枚举）
     * 
     * @param fileType 文件扩展名（不含点号，如：pdf、jpg）
     * @return 文件分类枚举
     */
    public static FileCategory inferFileCategory(String fileType) {
        if (fileType == null || fileType.isEmpty()) {
            return FileCategory.OTHER;
        }
        
        String lowerType = fileType.toLowerCase();
        
        // 使用Set提高查找效率（O(1)），代码更清晰
        if (DOCUMENT_EXTENSIONS.contains(lowerType)) {
            return FileCategory.DOCUMENT;
        }
        if (IMAGE_EXTENSIONS.contains(lowerType)) {
            return FileCategory.IMAGE;
        }
        if (VIDEO_EXTENSIONS.contains(lowerType)) {
            return FileCategory.VIDEO;
        }
        if (AUDIO_EXTENSIONS.contains(lowerType)) {
            return FileCategory.AUDIO;
        }
        if (ARCHIVE_EXTENSIONS.contains(lowerType)) {
            return FileCategory.ARCHIVE;
        }
        
        return FileCategory.OTHER;
    }
    
    /**
     * 获取文档类型扩展名集合
     * 
     * @return 文档扩展名集合（只读）
     */
    public static Set<String> getDocumentExtensions() {
        return new HashSet<>(DOCUMENT_EXTENSIONS);
    }
    
    /**
     * 获取图片类型扩展名集合
     * 
     * @return 图片扩展名集合（只读）
     */
    public static Set<String> getImageExtensions() {
        return new HashSet<>(IMAGE_EXTENSIONS);
    }
    
    /**
     * 获取视频类型扩展名集合
     * 
     * @return 视频扩展名集合（只读）
     */
    public static Set<String> getVideoExtensions() {
        return new HashSet<>(VIDEO_EXTENSIONS);
    }
    
    /**
     * 获取音频类型扩展名集合
     * 
     * @return 音频扩展名集合（只读）
     */
    public static Set<String> getAudioExtensions() {
        return new HashSet<>(AUDIO_EXTENSIONS);
    }
    
    /**
     * 获取压缩包类型扩展名集合
     * 
     * @return 压缩包扩展名集合（只读）
     */
    public static Set<String> getArchiveExtensions() {
        return new HashSet<>(ARCHIVE_EXTENSIONS);
    }
}