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

import UIKit

class AudioSettingsMeloDBTLToneViewModel: AudioSettingsBaseViewModel {
    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "Enable"
        case loudness = "Loudness"
        case bass = "Bass"
        case treble = "Treble"

        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.meloDBTLToneControlEnabled)
            case .loudness:
                return .double(value: player.meloDBTLToneControlLoudness)
            case .bass:
                return .double(value: player.meloDBTLToneControlBass)
            case .treble:
                return .double(value: player.meloDBTLToneControlTreble)
            }
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            switch value(player: player) {
            case .bool(let value):
                if value {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.Tone.enabled, comment:"")
                } else {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.Tone.disabled, comment:"")
                }
            case .double(value: _):
                switch self {
                case .loudness:
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.Tone.loudnessSlider, comment:"")
                case .bass:
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.Tone.bassSlider, comment:"")
                case .treble:
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.Tone.trebleSlider, comment:"")
                default:
                    return ""
                }
            default:
                return ""
            }
        }

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

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

    override var title: String { return NSLocalizedString("BTL TONE CONTROL", comment: "BTL Tone") }

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

    override func sliderMoved(_ row: AudioSettingsRow, value: Double) {
        if
            let playerID = playerID,
            let player = playerController?.player(id: playerID),
            let enumCase = RowType(rawValue: row.identifier) {
            switch enumCase {
            case .loudness:
                player.setMeloDBTLToneControlLoudness(value,
                                                      completion: nil,
                                                      completionQueue: nil)
            case .bass:
                player.setMeloDBTLToneControlBass(value,
                                                  completion: nil,
                                                  completionQueue: nil)
            case .treble:
                player.setMeloDBTLToneControlTreble(value,
                                                    completion: nil,
                                                    completionQueue: nil)
            default:
                break
            }
        }
    }
}
