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

import UIKit

class AudioSettingsSimplifiedMeloDViewModel: AudioSettingsBaseViewModel {
    enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "Enabled"
        case voicePlus = "Voice+"
        case drcMode = "DRC Mode"
        case eqPreset = "EQ Preset"

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

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

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

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
				let overall = player.meloDDRCEnabled && player.meloDGEQEnabled
                return .bool(value: overall)
            case .voicePlus:
                return .bool(value: player.meloDCOPPVoicePlusEnabled)
            case .drcMode:
                let options = AudioSettingsMeloDDRCModeViewModel.Preset.allCases
                let index = min(Int(player.meloDDRCMode.rawValue), options.count)
                let option = NSLocalizedString(options[index].rawValue, comment: "DRC mode")
                return .string(value: option)
            case .eqPreset:
                if player.currentMeloDGEQPreset.count > 0 {
                	return .string(value: player.currentMeloDGEQPreset)
                } else {
					return .string(value: "Setting Unsupported")
                }

            }
        }

        func viewModel(player: IoTPlayer)->AudioSettingsViewModel? {
            switch self {
            case .enable:
                return nil
            case .voicePlus:
                return nil
            case .drcMode:
                return AudioSettingsMeloDDRCModeViewModel()
            case .eqPreset:
                return player.currentMeloDGEQPreset.count > 0 ?
                AudioSettingsMeloDEQPresetViewModel() : nil
            }
        }

        func available(player: IoTPlayer) -> Bool {
            if self == .enable {
                return true
            } else {
                let overall = player.meloDDRCEnabled && player.meloDGEQEnabled
                return overall
            }
        }
    }

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

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

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

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

            if let enumCase = RowType(rawValue: row.identifier) {
                switch enumCase {
                case .enable:
                    player.setMeloDDRCEnabled(value,
                                              completion: nil,
                                              completionQueue: nil)
                    player.setMeloDGEQEnabled(value,
                                              completion: nil,
                                              completionQueue: nil)
                    if !value {
                        player.setMeloDCOPPVoicePlusEnabled(value,
                                                            completion: nil,
                                                            completionQueue: nil)
                    }
                case .voicePlus:
                    player.setMeloDCOPPVoicePlusEnabled(value,
                                                        completion: nil,
                                                        completionQueue: nil)
                default:
                    break
                }
            }
        }
    }
}
