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

import UIKit

class AudioSettingsDolbyViewModel: AudioSettingsBaseViewModel {
    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        typealias DolbyOutputMode = AudioSettingsDolbyModeOptionsViewModel.DolbyOutputMode
        case enable = "enable"
        case mode = "mode"

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

        func title() -> String {
            switch self {
            case .enable:
                return NSLocalizedString("Enabled", comment: "Enabled")
            case .mode:
                return NSLocalizedString("Output Mode", comment: "Output Mode")
            }
        }

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.dolbyEnabled)
            case .mode:
                let index = min(Int(player.currentDolbyMode), DolbyOutputMode.allCases.count)
                let option = NSLocalizedString(DolbyOutputMode.allCases[index].rawValue, comment: "dolby mode")
                return .string(value: option)
            }
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            switch value(player: player) {
            case .bool(let value):
                if value {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Dolby.enabled, comment:"")
                } else {
					return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Dolby.disabled, comment:"")
                }
            case .string(let value):
                return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Dolby.modePrefix , comment:"") + " " + NSLocalizedString(value, comment: "dolby mode")
            default:
                return ""
            }
        }

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

        func available(player: IoTPlayer) -> Bool {
            guard player.dolbySupported else { return false }
            if self == .enable {
                return true
            } else {
                return player.dolbyEnabled
            }
        }
    }

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

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

    override func toggleChanged(_ row: AudioSettingsRow, value: Bool) {
        if
            let playerID = playerID,
            let player = playerController?.player(id: playerID) {
            startTimeoutTimer()
            player.setDolbyEnabled(value,
                                   completion: nil,
                                   completionQueue: nil)
        }
    }

    override func optionRowTapped(_ row: AudioSettingsRow) {
        optionRowTapped(row, rowEnum: RowType.self)
    }
}
