//
//  PQDownloadModel.swift
//  PQSpeed
//
//  Created by SanW on 2020/9/10.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit
import BFNetRequestKit

// MARK: - 文件扩展名

/// 文件扩展名
public enum FileExtensionType: String {
    case normal = "" // 无后缀名
    case png // png
    case jpg // jpg
    case gif // gif
    case pdf // pdf
    case mp4 // mp4
    case mp3 // mp3
    case m4a // m4a
    case txt // txt
    case zip // zip
    /// 文件类型
    /// - Returns: <#description#>
   public func fileType() -> String {
        var fileType: String = "unknow"
        switch self {
        case .png, .jpg:
            fileType = "image"
        case .gif:
            fileType = "gif"
        case .mp3, .m4a:
            fileType = "voice"
        case .mp4:
            fileType = "video"
        default:
            fileType = "unknow"
        }
        return fileType
    }
}

// MARK: - 下载model

/// 下载model
public class PQDownloadModel: NSObject {
    public var name: String? // 资源名
    public var fileExtensionType: FileExtensionType? // 文件后缀类型
    public var realFileExtensionType: FileExtensionType? // 真实文件后缀类型
    public var mimeType: String? { // 媒体类型
        didSet {
//            if mimeType?.contains("jpeg") ?? false { // image/jpeg
//                realFileExtensionType = .jpg
//            }else if mimeType?.contains("gif") ?? false { // image/gif
//                realFileExtensionType = .gif
//            }else if mimeType?.contains("pdf") ?? false{ // application/pdf
//                realFileExtensionType = .pdf
//            }else if mimeType?.contains("mp4") ?? false{ // video/mp4
//                realFileExtensionType = .mp4
//            }else if mimeType?.contains("mp3") ?? false{ // audio/mpeg
//                realFileExtensionType = .mp3
//            }else if mimeType?.contains("m4a") ?? false { // audio/x-m4a/audio/m4a
//                realFileExtensionType = .m4a
//            }
            if mimeType?.contains("m4a") ?? false { // audio/x-m4a,audio/m4a
                realFileExtensionType = .m4a
            }
        }
    }

    public var sourceURL: String? // 源地址
    public  var filePath: String? // 下载到本地地址
    public var imageURL: String? // 图片地址
    public  var totalLength: Int64? // 总大小
    public var downloadLength: Int64? // 已下载大小
    public var task: URLSessionDataTask? // 下载任务
    public var progress: Float? // 下载进度
    public var state: downloadState? // 下载状态
    public var progressHandle: ProgressHandle? // 进度的回调
    public var stateHandle: StateHandle? // 状态回调
    public var fileHandle: FileHandle? // 文件句柄
}

// MARK: - 下载状态

/// 下载状态
public enum downloadState: Int {
    case downloading = 0 // 下载中
    case compelte = 1 // 下载完成
    case error = 2 // 下载失败
    case pause = 3 // 暂停下载
    case cancel = 4 // 取消下载
}
// 进度的回调
public typealias  ProgressHandle = (_ progress: Float, _ downloadLength: Int64?, _ totalLength: Int64?) -> Void
// 状态回调
public typealias StateHandle = (_ state: downloadState, _ url: String, _ localPath: String?, _ error: PQError?) -> Void
