//
//  CloudTransferModels.swift
//  suixinkan
//

import Foundation

/// 云盘传输任务类型。
enum CloudTransferKind: String, Codable, Sendable {
    case upload
    case download
}

/// 云盘传输任务状态。
enum CloudTransferStatus: String, Codable, Sendable {
    case pending
    case uploading
    case downloading
    case paused
    case failed
    case completed

    /// 展示文案。
    var title: String {
        switch self {
        case .pending:
            "等待中"
        case .uploading:
            "上传中"
        case .downloading:
            "下载中"
        case .paused:
            "已暂停"
        case .failed:
            "失败"
        case .completed:
            "已完成"
        }
    }
}

/// 云盘本地传输任务记录。
struct CloudTransferTask: Codable, Sendable, Equatable, Hashable, Identifiable {
    let id: String
    let kind: CloudTransferKind
    let fileName: String
    let fileType: Int
    let fileSize: Int64
    let parentFolderId: Int
    let localPath: String
    let remoteURL: String
    var status: CloudTransferStatus
    var progress: Int
    var errorMessage: String
    var createdAt: TimeInterval
    var updatedAt: TimeInterval

    /// 创建传输任务。
    init(
        id: String = UUID().uuidString.replacingOccurrences(of: "-", with: ""),
        kind: CloudTransferKind,
        fileName: String,
        fileType: Int,
        fileSize: Int64,
        parentFolderId: Int = 0,
        localPath: String = "",
        remoteURL: String = "",
        status: CloudTransferStatus = .pending,
        progress: Int = 0,
        errorMessage: String = "",
        createdAt: TimeInterval = Date().timeIntervalSince1970,
        updatedAt: TimeInterval = Date().timeIntervalSince1970
    ) {
        self.id = id
        self.kind = kind
        self.fileName = fileName
        self.fileType = fileType
        self.fileSize = fileSize
        self.parentFolderId = parentFolderId
        self.localPath = localPath
        self.remoteURL = remoteURL
        self.status = status
        self.progress = progress
        self.errorMessage = errorMessage
        self.createdAt = createdAt
        self.updatedAt = updatedAt
    }
}

/// 云盘传输任务持久化协议。
protocol CloudTransferTaskStoring: AnyObject {
    /// 读取全部本地任务。
    func loadTasks() -> [CloudTransferTask]

    /// 保存全部本地任务。
    func saveTasks(_ tasks: [CloudTransferTask])
}
