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

import UIKit

class AudioSettingsBassBoostViewModel: AudioSettingsBaseViewModel {
    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("Level", comment: "Effect Level")
            }
        }

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.bassboostEnabled)
            case .effectLevel:
                return .double(value: player.bassBoost)
            }
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            switch value(player: player) {
            case .bool(let value):
                if value {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Bassboost.enabled, comment:"")
                } else {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Bassboost.disabled, comment:"")
                }
            case .string(_):
                return NSLocalizedString("Level", comment: "Effect Level")
            default:
                return ""
            }
        }

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

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

    override var title: String { return NSLocalizedString("BASS BOOST", comment: "Dolby") }

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

    override func sliderMoved(_ row: AudioSettingsRow, value: Double) {

        if
            let playerID = playerID,
            let player = playerController?.player(id: playerID) {
            startTimeoutTimer()
            player.setBassboost(value,
                                completion: nil,
                                completionQueue: nil)
        }
    }
}

