//
//  FileInfoUtils.m
//  AIIM
//
//  Created by qitewei on 2025/5/26.
//

#import "FileInfoUtils.h"

@implementation FileInfoUtils
+ (NSString *)getFileNameWithURL:(NSURL *)fileURL {
    // 检查URL是否有效
    if (![fileURL isFileURL]) {
        NSLog(@"Error: The provided URL is not a file URL.");
        return nil;
    }
    
    // 从URL中获取最后一个路径组件（即文件名）
    return [fileURL lastPathComponent];
}

+ (NSString *)getFileSizeStringWithURL:(NSURL *)fileURL error:(NSError **)error {
    // 检查URL是否有效
    if (![fileURL isFileURL]) {
        if (error) {
            *error = [NSError errorWithDomain:@"FileInfoErrorDomain"
                                         code:1001
                                     userInfo:@{NSLocalizedDescriptionKey: @"The provided URL is not a file URL."}];
        }
        return nil;
    }
    
    // 获取文件属性
    NSDictionary<NSFileAttributeKey, id> *attributes;
    NSError *attrError = nil;
    
    attributes = [fileURL resourceValuesForKeys:@[NSURLFileSizeKey] error:&attrError];
    
    if (attrError) {
        if (error) {
            *error = attrError;
        }
        NSLog(@"Error getting file attributes: %@", attrError.localizedDescription);
        return nil;
    }
    
    // 从属性中获取文件大小
    NSNumber *fileSizeNumber = attributes[NSURLFileSizeKey];
    unsigned long long fileSize = [fileSizeNumber unsignedLongLongValue];
    
    // 格式化文件大小为易读的字符串
    return [self formatFileSize:fileSize];
}

/**
 * 根据本地文件URL获取文件字节大小
 * @param fileURL 本地文件的URL
 * @param error 错误信息输出
 * @return 文件字节大小
 */
+ (NSInteger )getFileSizeWithURL:(NSURL *)fileURL error:(NSError **)error{
    if (![fileURL isFileURL]) {
        if (error) {
            *error = [NSError errorWithDomain:@"FileInfoErrorDomain"
                                         code:1001
                                     userInfo:@{NSLocalizedDescriptionKey: @"The provided URL is not a file URL."}];
        }
        return 0;
    }
    
    // 获取文件属性
    NSDictionary<NSFileAttributeKey, id> *attributes;
    NSError *attrError = nil;
    
    attributes = [fileURL resourceValuesForKeys:@[NSURLFileSizeKey] error:&attrError];
    
    if (attrError) {
        if (error) {
            *error = attrError;
        }
        NSLog(@"Error getting file attributes: %@", attrError.localizedDescription);
        return 0;
    }
    
    // 从属性中获取文件大小
    NSNumber *fileSizeNumber = attributes[NSURLFileSizeKey];
    unsigned long long fileSize = [fileSizeNumber unsignedLongLongValue];
    
    // 格式化文件大小为易读的字符串
    return fileSize;
}

+ (BOOL)isFileGreaterThan100MB:(NSURL *)fileURL error:(NSError **)error {
    // 定义200MB的字节数
    const unsigned long long oneHundredMB = 2024 * 1024 * 1024; // 100MB = 100*1024*1024 bytes
    
    // 检查URL是否有效
    if (![fileURL isFileURL]) {
        if (error) {
            *error = [NSError errorWithDomain:@"FileInfoErrorDomain"
                                         code:1001
                                     userInfo:@{NSLocalizedDescriptionKey: @"The provided URL is not a file URL."}];
        }
        return NO;
    }
    
    // 获取文件属性
    NSDictionary<NSFileAttributeKey, id> *attributes;
    NSError *attrError = nil;
    
    attributes = [fileURL resourceValuesForKeys:@[NSURLFileSizeKey] error:&attrError];
    
    if (attrError) {
        if (error) {
            *error = attrError;
        }
        NSLog(@"Error getting file attributes: %@", attrError.localizedDescription);
        return NO;
    }
    
    // 从属性中获取文件大小
    NSNumber *fileSizeNumber = attributes[NSURLFileSizeKey];
    unsigned long long fileSize = [fileSizeNumber unsignedLongLongValue];
    
    // 判断文件大小是否大于100MB
    return fileSize > oneHundredMB;
}


/**
 * 将文件大小（字节）格式化为易读的字符串
 * @param fileSize 文件大小（字节）
 * @return 格式化后的字符串（如"2.4 MB"、"56.7 KB"）
 */
+ (NSString *)formatFileSize:(unsigned long long)fileSize {
    NSByteCountFormatter *formatter = [[NSByteCountFormatter alloc] init];
    formatter.allowedUnits = NSByteCountFormatterUseMB | NSByteCountFormatterUseKB; // 只显示KB和MB
    formatter.countStyle = NSByteCountFormatterCountStyleFile;
    formatter.includesUnit = YES;
    formatter.includesCount = YES;
    formatter.allowsNonnumericFormatting = NO;
    
    return [formatter stringFromByteCount:(int64_t)fileSize];
}
@end
