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

import UIKit

class AudioSettingsMeloDVoicePlusViewModel: AudioSettingsBaseViewModel {
    typealias Preset = AudioSettingsOffLowMedHighViewModel.Preset
    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "enable"
        case effectLevel = "effectLevel"

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

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

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.meloDCOPPVoicePlusEnabled)
            case .effectLevel:
                let index = min(Int(player.meloDVoicePlusEffect.rawValue), Preset.allCases.count)
                let option = NSLocalizedString(Preset.allCases[index].rawValue, comment: "Effect Level")
                return .string(value: option)
            }
        }

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

        func viewModel(player: IoTPlayer)->AudioSettingsViewModel? {
            let vm = AudioSettingsOffLowMedHighViewModel(getter: { [weak player] in
                guard let player = player else { return 0 }
                return player.meloDVoicePlusEffect.rawValue
            }, setter: { [weak player] (intValue) in
                guard let player = player else { return }
                if let cValue = IoTVoicePlusEffect(rawValue: intValue) {
                    player.setMeloDVoicePlusEffect(cValue,
                                                   completion: nil,
                                                   completionQueue: nil)
                }
            })
            return vm
        }

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

    override var title: String { return NSLocalizedString("VOICE+", comment: "VOICE+") }

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

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