//
//  © 2019-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

class AudioSettingsDTSModeOptionsViewModel: AudioSettingsBaseViewModel {
    enum DTSMode: String, CaseIterable, AudioSettingRowSource {
		case twozero = "2.0"
        case twoone = "2.1"
        case threezero = "3.0"
        case threeone = "3.1"
        case fiveone = "5.1"

        func identifier() -> String {
            return self.rawValue
        }

        func title() -> String {
            return self.rawValue
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            return self.rawValue
        }

        func viewModel(player: IoTPlayer) -> AudioSettingsViewModel? {
            return nil
        }

        func available(player: IoTPlayer) -> Bool {
            return true
        }

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            let index = min(Int(player.currentVirtualXOutputMode), DTSMode.allCases.count)
            let option = DTSMode.allCases[index]
            return .checked(value: self == option)
        }
    }
    override var title: String { return NSLocalizedString("DTS MODE", comment: "DTS Mode") }

    override func rows()->[AudioSettingsRow] {
        return rows(rowEnum: DTSMode.self)
    }

    override func checkableOptionChecked(_ row: AudioSettingsRow) {
        guard
            let playerID = playerID,
            let player = playerController?.player(id: playerID),
            let index = DTSMode.allCases.firstIndex(where: { (mode) -> Bool in
                return mode.identifier() == row.identifier
            })
            else {
                return
        }

        player.setCurrentVirtualXOutputMode(Int32(index),
                                            completion: nil,
                                            completionQueue: nil)
    }
}
