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

import UIKit

class AudioSettingsMeloDDRCModeViewModel: AudioSettingsBaseViewModel {
    enum Preset: String, CaseIterable, AudioSettingRowSource {
        case off = "Off"
        case nightPlus = "Night+"
        case volumePlus = "Volume+"

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

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

        func accessibilityTitle(player: IoTPlayer) -> String {
            return NSLocalizedString(self.rawValue, comment: "Effect Level")
        }

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

        func available(player: IoTPlayer) -> Bool {
            return true
        }

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            let value = player.meloDDRCMode.rawValue
            let enumValue = Preset.allCases[value]
            return .checked(value: self == enumValue)
        }
    }

    override var title: String { return NSLocalizedString("EFFECT LEVEL", comment: "Effect level") }

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

    override func checkableOptionChecked(_ row: AudioSettingsRow) {
        guard
            let playerID = playerID,
            let player = playerController?.player(id: playerID),
            let index = Preset.allCases.firstIndex(where: { (mode) -> Bool in
                return mode.identifier() == row.identifier
            }),
            let enumValue = IoTDrcMode(rawValue: index)
            else {
                return
        }

        player.setMeloDDRCMode(enumValue,
                               completion: nil,
                               completionQueue: nil)
    }
}
