//
//  TwilioHelper.swift
//  WoWonder
//
//  Created by iMac on 14/10/23.
//  Copyright © 2023 ScriptSun. All rights reserved.
//

import Foundation
import TwilioVideo

struct PlatformUtils {
    static let isSimulator: Bool = {
        var isSim = false
#if arch(i386) || arch(x86_64)
        isSim = true
#endif
        return isSim
    }()
}

enum VideoCodec: CaseIterable {
    
    case auto, VP8, VP8Simulcast, H264, VP9
    
    var codec: TwilioVideo.VideoCodec? {
        switch self {
        case .auto:
            return nil
        case .VP8:
            return Vp8Codec()
        case .VP8Simulcast:
            return Vp8Codec(simulcast: true)
        case .H264:
            return H264Codec()
        case .VP9:
            return Vp9Codec()
        }
    }
    
    var name: String {
        switch self {
        case .auto:
            return "Auto"
        case .VP8, .H264, .VP9:
            return codec?.name ?? ""
        case .VP8Simulcast:
            return "\(VideoCodec.VP8.name) Simulcast"
        }
    }
}

class Settings: NSObject {
    
    let supportedAudioCodecs: [TwilioVideo.AudioCodec] = [
        OpusCodec(),
        PcmaCodec(),
        PcmuCodec(),
        G722Codec()
    ]
    let supportedSignalingRegions: [String] = [
        "gll", "au1", "br1", "de1", "ie1", "in1", "jp1", "sg1", "us1", "us2"
    ]
    
    let supportedSignalingRegionDisplayString: [String : String] = [
        "gll": "Global Low Latency",
        "au1": "Australia",
        "br1": "Brazil",
        "de1": "Germany",
        "ie1": "Ireland",
        "in1": "India",
        "jp1": "Japan",
        "sg1": "Singapore",
        "us1": "US East Coast (Virginia)",
        "us2": "US West Coast (Oregon)"
    ]
    
    var audioCodec: TwilioVideo.AudioCodec?
    var videoCodec: VideoCodec = .auto
    
    var maxAudioBitrate = UInt()
    var maxVideoBitrate = UInt()
    
    var signalingRegion: String?
    
    func getEncodingParameters() -> EncodingParameters?  {
        if maxAudioBitrate == 0 && maxVideoBitrate == 0 {
            return nil;
        } else if videoCodec == .auto {
            return EncodingParameters(audioBitrate: maxAudioBitrate, videoBitrate: 0)
        } else {
            return EncodingParameters(audioBitrate: maxAudioBitrate, videoBitrate: maxVideoBitrate)
        }
    }
    
    private override init() {
        // Can't initialize a singleton
    }
    
    static let shared = Settings()
}
