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

import UIKit

class AudioSettingsMeloDBassPlusViewModel: AudioSettingsBaseViewModel {
    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "Enable"
        case strength = "Strength"
        case harmonic = "Harmonic"

        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.meloDCOPPBassPlusEnabled)
            case .strength:
                return .double(value: player.meloDCOPPBassPlusStrength)
            case .harmonic:
                return .double(value: player.meloDCOPPBassPlusHarmonic)
            }
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            switch value(player: player) {
            case .bool(let value):
                if value {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.BassPlus.enabled, comment:"")
                } else {
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.BassPlus.disabled, comment:"")
                }
            case .double(value: _):
                switch self {
                case .strength:
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.BassPlus.strengthSlider, comment:"")
                case .harmonic:
                    return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.MeloD.BassPlus.harmonicSlider, 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.meloDCOPPBassPlusEnabled
            }
        }
    }

    override var title: String { return NSLocalizedString("BASS+", comment: "Bass+") }

    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.setMeloDCOPPBassPlusEnabled(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 .strength:
                player.setMeloDCOPPBassPlusStrength(value,
                                                    completion: nil,
                                                    completionQueue: nil)
            case .harmonic:
                player.setMeloDCOPPBassPlusHarmonic(value,
                                                    completion: nil,
                                                    completionQueue: nil)
            default:
                break
            }
        }
    }
}
