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

import UIKit

class AudioSettingsMeloDDRCViewModel: AudioSettingsBaseViewModel {
    typealias Mode = AudioSettingsMeloDDRCModeViewModel.Preset

    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "Enable"
        case mode = "Mode"
        case level = "Level"

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

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

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.meloDDRCEnabled)
            case .mode:
                let index = min(Int(player.meloDDRCMode.rawValue), Mode.allCases.count)
                let option = NSLocalizedString(Mode.allCases[index].rawValue, comment: "DRC mode")
                return .string(value: option)
            case .level:
                return .double(value: player.meloDDRCRefLevel)
            }
        }

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

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

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

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

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

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

    override func sliderMoved(_ row: AudioSettingsRow, value: Double) {
        if
            let playerID = playerID,
            let player = playerController?.player(id: playerID) {
            player.setMeloDDRCRefLevel(value,
                                       completion: nil,
                                       completionQueue: nil)
        }
    }    
}
