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

import UIKit

class AudioSettingsVirtualXViewModel: AudioSettingsBaseViewModel {
    typealias DTSMode = AudioSettingsDTSModeOptionsViewModel.DTSMode

    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "enable"
        case dialogClarity = "dialogClarity"
        case mode = "mode"

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

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

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.virtualXEnabled)
            case .dialogClarity:
                return .bool(value: player.isDialogClarityEnabled)
            case .mode:
                let index = min(Int(player.currentVirtualXOutputMode), DTSMode.allCases.count)
                let option = NSLocalizedString(DTSMode.allCases[index].rawValue, comment: "DTS mode")
                return .string(value: option)
            }
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            switch value(player: player) {
            case .bool(let value):
                switch self {
                case .enable:
                    if value {
                        return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.DTS.enabled, comment:"")
                    } else {
                        return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.DTS.disabled, comment:"")
                    }
                case .dialogClarity:
                    if value {
                        return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.DTS.dialogClarityEnabled, comment:"")
                    } else {
                        return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.DTS.dialogClarityDisabled, comment:"")
                    }
                default:
                    return ""
                }
            case .string(let value):
                return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.DTS.modePrefix , comment:"") + " " + value
            default:
                return ""
            }
        }

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

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

    override var title: String { return NSLocalizedString("DTS VIRTUAL X", comment: "DTS Virtual X") }

    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()

            if row.identifier == RowType.enable.identifier() {
                player.setVirtualXEnabled(value,
                                          completion: nil,
                                          completionQueue: nil)
            } else {
                player.setDialogClarityEnabled(value,
                                               completion: nil,
                                               completionQueue: nil)
            }
        }
    }

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