import SwiftUI

/// 上传状态
public enum UploadStatus: Equatable {
    case idle
    case uploading(progress: Double)
    case success
    case failure(Error)
    
    public var isUploading: Bool {
        if case .uploading = self { return true }
        return false
    }
    
    public var progress: Double {
        if case let .uploading(progress) = self { return progress }
        return 0
    }
    
    public static func == (lhs: UploadStatus, rhs: UploadStatus) -> Bool {
        switch (lhs, rhs) {
        case (.idle, .idle):
            return true
        case let (.uploading(lhsProgress), .uploading(rhsProgress)):
            // 使用近似比较来处理浮点数的精度问题
            return abs(lhsProgress - rhsProgress) < 0.001
        case (.success, .success):
            return true
        case (.failure, .failure):
            // 对于错误类型，我们简单地认为它们不相等，因为比较 Error 对象比较复杂
            // 如果需要更精确的比较，可以在这里添加具体实现
            return false
        default:
            return false
        }
    }
}

/// 上传结果
public struct UploadResult: Identifiable, Equatable {
    public let id = UUID()
    public var fileId: String
    public var previewFileId: String
    public let image: UIImage
    public var status: UploadStatus = .idle
    
    public init(fileId: String = "", previewFileId: String = "", image: UIImage, status: UploadStatus = .idle) {
        self.fileId = fileId
        self.previewFileId = previewFileId
        self.image = image
        self.status = status
    }
    
    public static func == (lhs: UploadResult, rhs: UploadResult) -> Bool {
        lhs.id == rhs.id
    }
}
