// APIs.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//

import Foundation
enum ApiVideoUploaderError: Error {
    case invalidApplicationName
    case invalidApplicationVersion
    case missingApplicationName
}

public class ApiVideoUploader {

    public static var apiKey: String? = nil
    public static var basePath = "https://ws.api.video"
    internal  static var customHeaders:[String: String] = ["AV-Origin-Client": "ios-uploader:0.1.5"]
    private static var chunkSize: Int = 50 * 1024 * 1024
    internal static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
    internal static var credential = ApiVideoCredential()
    public static var apiResponseQueue: DispatchQueue = .main

    public static func setChunkSize(chunkSize: Int) throws {
        if (chunkSize > 128 * 1024 * 1024) {
            throw ParameterError.outOfRange
        } else if (chunkSize < 5 * 1024 * 1024) {
            throw ParameterError.outOfRange
        }
        
        ApiVideoUploader.chunkSize = chunkSize
    }

    public static func getChunkSize() -> Int {
        return ApiVideoUploader.chunkSize
    }


    public static func setApplicationName(applicationName: String, applicationVersion: String?) throws {
        if(applicationName.isEmpty) {
            if(applicationVersion != nil && !applicationVersion!.isEmpty) {
                throw ApiVideoUploaderError.missingApplicationName
            }
            ApiVideoUploader.customHeaders["AV-Origin-App"] = nil
            return
        }

        let pattern = #"^[\w\-]{1,50}$"#
        let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
        let stringRange = NSRange(location: 0, length: applicationName.utf16.count)
        let matches = regex.matches(in: applicationName, range: stringRange)
        if(matches.isEmpty) {
            throw ApiVideoUploaderError.invalidApplicationName
        }

        if(applicationVersion != nil && !applicationVersion!.isEmpty) {
            let pattern2 = #"^[\w\-]{1,50}$"#
            let regex2 = try! NSRegularExpression(pattern: pattern2, options: .anchorsMatchLines)
            let stringRange2 = NSRange(location: 0, length: applicationVersion!.utf16.count)
            let matches2 = regex2.matches(in: applicationVersion!, range: stringRange2)
            if(matches2.isEmpty) {
                throw ApiVideoUploaderError.invalidApplicationVersion
            }
            ApiVideoUploader.customHeaders["AV-Origin-App"] = applicationName + ":" + applicationVersion!
            return
        }
        ApiVideoUploader.customHeaders["AV-Origin-App"] = applicationName
    }

}

open class RequestBuilder<T> {
    var headers: [String: String]
    public let parameters: [String: Any]?
    public let method: String
    public let URLString: String

    /// Optional block to obtain a reference to the request's progress instance when available.
    public let onProgressReady: ((Progress) -> Void)?

    required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], onProgressReady: ((Progress) -> Void)? = nil) {
        self.method = method
        self.URLString = URLString
        self.parameters = parameters
        self.headers = headers
        self.onProgressReady = onProgressReady

        addHeaders(ApiVideoUploader.customHeaders)
    }

    open func addHeaders(_ aHeaders: [String: String]) {
        for (header, value) in aHeaders {
            headers[header] = value
        }
    }

    @discardableResult
    open func execute(_ apiResponseQueue: DispatchQueue = ApiVideoUploader.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, ErrorResponse>) -> Void) -> URLSessionTask? {
        return nil
    }

    public func addHeader(name: String, value: String) -> Self {
        if !value.isEmpty {
            headers[name] = value
        }
        return self
    }
}

public protocol RequestBuilderFactory {
    func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
    func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type
}
