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

import UIKit

class AudioSettingsDolbyModeOptionsViewModel: AudioSettingsBaseViewModel {
    enum DolbyOutputMode: String, CaseIterable, AudioSettingRowSource {
        case movie = "Movie"
        case music = "Music"
        case night = "Night"

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

        func title() -> String {
            return NSLocalizedString(self.rawValue, comment: "Dolby Mode")
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            return NSLocalizedString(self.rawValue, comment: "Dolby Mode")
        }

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

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

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            let index = min(Int(player.currentDolbyMode), DolbyOutputMode.allCases.count)
            let option = DolbyOutputMode.allCases[index]
            return .checked(value: self == option)
        }
    }

    override var title: String { return NSLocalizedString("DOLBY MODE", comment: "Dolby Mode") }

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

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

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