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

import UIKit

class AudioSettingsMeloDAroundProViewModel: AudioSettingsBaseViewModel {
    typealias EffectPreset = AudioSettingsOffLowMedHighViewModel.Preset
    typealias ModePreset = AudioSettingsMusicMovieViewModel.Preset

    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "enable"
        case effectLevel = "effectLevel"
        case centerMode = "centerMode"

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

        func title() -> String {
            switch self {
            case .enable:
                return NSLocalizedString("Enabled", comment: "Enabled")
            case .effectLevel:
                return NSLocalizedString("Effect Strength", comment: "Effect Strength")
            case .centerMode:
				return NSLocalizedString("Center Mode", comment: "Center Mode")
            }
        }

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.meloDAroundProEnabled)
            case .effectLevel:
                let index = min(Int(player.meloDAroundProEffect.rawValue), EffectPreset.allCases.count)
                let option = NSLocalizedString(EffectPreset.allCases[index].rawValue, comment: "Effect Level")
                return .string(value: option)
            case .centerMode:
                let index = min(Int(player.meloDAroundProMode.rawValue), ModePreset.allCases.count)
                let option = NSLocalizedString(ModePreset.allCases[index].rawValue, comment: "Mode")
                return .string(value: option)
            }
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            switch value(player: player) {
            case .bool(let value):
                if value {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.AroundPro.enabled, comment:"")
                } else {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.AroundPro.disabled, comment:"")
                }
            case .string(let value):
                switch self {
                case .effectLevel:
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.AroundPro.strengthPrefix , comment:"") + " " + value
                case .centerMode:
					return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.AroundPro.modePrefix , comment:"") + " " + value
                default:
                    return ""
                }
            default:
                return ""
            }
        }

        func viewModel(player: IoTPlayer)->AudioSettingsViewModel? {
            if self == .effectLevel {
                let vm = AudioSettingsOffLowMedHighViewModel(getter: { [weak player] in
                    guard let player = player else { return 0 }
                    return player.meloDAroundProEffect.rawValue
                    }, setter: { [weak player] (intValue) in
                        guard let player = player else { return }
                        if let cValue = IoTAroundProEffect(rawValue: intValue) {
                            player.setMeloDAroundProEffect(cValue,
                                                           completion: nil,
                                                           completionQueue: nil)
                        }
                })
                return vm
            } else {
                let vm = AudioSettingsMusicMovieViewModel(getter: { [weak player] in
                    guard let player = player else { return 0 }
                    return player.meloDAroundProMode.rawValue
                    }, setter: { [weak player] (intValue) in
                        guard let player = player else { return }
                        if let cValue = IoTAroundProMode(rawValue: intValue) {
                            player.setMeloDAroundProMode(cValue,
                                                           completion: nil,
                                                           completionQueue: nil)
                        }
                })
                return vm
            }
        }

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

    override var title: String { return NSLocalizedString("AROUND PRO", comment: "Around Pro") }

    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.setMeloDAroundProEnabled(value,
                                            completion: nil,
                                            completionQueue: nil)
        }
    }

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