package com.lingyue.parse.enums;

import lombok.Getter;

/**
 * 文件类型枚举
 * 
 * @author lingyue
 * @since 2026-01-14
 */
@Getter
public enum FileType {
    
    PDF("application/pdf", "pdf", "PDF文档"),
    WORD("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx", "Word文档"),
    WORD_OLD("application/msword", "doc", "Word文档(旧版)"),
    EXCEL("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx", "Excel表格"),
    EXCEL_OLD("application/vnd.ms-excel", "xls", "Excel表格(旧版)"),
    IMAGE_JPG("image/jpeg", "jpg", "JPEG图片"),
    IMAGE_PNG("image/png", "png", "PNG图片"),
    IMAGE_GIF("image/gif", "gif", "GIF图片"),
    UNKNOWN("application/octet-stream", "", "未知类型");
    
    private final String mimeType;
    private final String extension;
    private final String description;
    
    FileType(String mimeType, String extension, String description) {
        this.mimeType = mimeType;
        this.extension = extension;
        this.description = description;
    }
    
    /**
     * 根据文件扩展名获取文件类型
     */
    public static FileType fromExtension(String extension) {
        if (extension == null || extension.isEmpty()) {
            return UNKNOWN;
        }
        
        String ext = extension.toLowerCase().replace(".", "");
        for (FileType type : values()) {
            if (type.extension.equals(ext)) {
                return type;
            }
        }
        return UNKNOWN;
    }
    
    /**
     * 根据MIME类型获取文件类型
     */
    public static FileType fromMimeType(String mimeType) {
        if (mimeType == null || mimeType.isEmpty()) {
            return UNKNOWN;
        }
        
        for (FileType type : values()) {
            if (type.mimeType.equals(mimeType)) {
                return type;
            }
        }
        return UNKNOWN;
    }
    
    /**
     * 判断是否为图片类型
     */
    public boolean isImage() {
        return this == IMAGE_JPG || this == IMAGE_PNG || this == IMAGE_GIF;
    }
    
    /**
     * 判断是否为文档类型
     */
    public boolean isDocument() {
        return this == PDF || this == WORD || this == WORD_OLD;
    }
    
    /**
     * 判断是否为表格类型
     */
    public boolean isSpreadsheet() {
        return this == EXCEL || this == EXCEL_OLD;
    }
}
