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

import UIKit

class AudioSettingsTrumpetViewModel: AudioSettingsBaseViewModel {
    typealias Preset = AudioSettingsTrumpetPresetsViewModel.Preset

    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "enable"
        case virtualization = "virtualization"
        case preset = "preset"
        case gain = "gain"

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

        func title() -> String {
            switch self {
            case .enable:
                return NSLocalizedString("Enabled", comment: "Enabled")
            case .virtualization:
                return NSLocalizedString("Virtualization", comment: "Virtualization")
            case .preset:
                return NSLocalizedString("Preset", comment: "Preset")
            case .gain:
                return NSLocalizedString("Gain", comment: "Gain")
            }
        }

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.trumpetEnabled)
            case .virtualization:
                return .bool(value: player.trumpetVirtualizationEnabled)
            case .preset:
                let index = min(player.currentTrumpetPreset.rawValue, Preset.allCases.count)
                let option = NSLocalizedString(Preset.allCases[index].rawValue, comment: "Trumpet Preset")
                return .string(value: option)
            case .gain:
                return .double(value: player.trumpetGain)
            }
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            switch value(player: player) {
            case .bool(let value):
                switch self {
                case .enable:
                    if value {
                        return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Trumpet.enabled, comment:"")
                    } else {
                        return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Trumpet.disabled, comment:"")
                    }
                case .virtualization:
                    if value {
                        return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Trumpet.virtualizationEnabled, comment:"")
                    } else {
                        return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Trumpet.virtualizationDisabled, comment:"")
                    }
                default:
                    return ""
                }
            case .string(let value):
                return NSLocalizedString(Accessibility.AudioEffectsSettingsScreen.Trumpet.presetPrefix , comment:"") + " " + value
            case .double(_):
				return NSLocalizedString("Gain", comment: "Gain")
            default:
                return ""
            }
        }

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

        func available(player: IoTPlayer) -> Bool {
            guard player.trumpetSupported else { return false }
/*
            return self == . virtualization ||
                self == .preset
*/
            if self == .gain {
                return false
            } else if self == .enable {
                return true
            } else {
                return player.trumpetEnabled
            }
        }
    }

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

    override func rows()->[AudioSettingsRow] {
        return rows(rowEnum: RowType.self)
        /* var allRows = rows(rowEnum: RowType.self)
        guard
            let playerID = playerID,
            let player = playerController?.player(id: playerID),
            player.trumpetEnabled else {
                return allRows
        }

        let eqBands = player.trumpetEqBands
        var eqRows = [AudioSettingsRow] ()

        for bandIndex in 0..<eqBands.count {
            let band = eqBands[bandIndex]
            eqRows.append(AudioSettingsRow(identifier: "\(bandIndex)",
                type: .slider(value: band.bandLevel),
                title: String(format: "%.0f Hz", band.centerFrequency),
                accessibilityTitle: String(format: "%.0f Hz", band.centerFrequency)))
        }

        allRows.append(contentsOf: eqRows)
        return allRows
 */
    }

    override func toggleChanged(_ row: AudioSettingsRow, value: Bool) {
        if
            let playerID = playerID,
            let player = playerController?.player(id: playerID) {
            startTimeoutTimer()

            if row.identifier == RowType.enable.identifier() {
                player.setTrumpetEnabled(value,
                                         completion: nil,
                                         completionQueue: nil)
            } else {
                player.setTrumpetVirtualizationEnabled(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) {

            if row.identifier == RowType.gain.identifier() {
                player.setTrumpetGain(value,
                                      completion: nil,
                                      completionQueue: nil)
            } else {

                if let eqBandMoved = Int(row.identifier) {

                    let eqBands = player.eqBands
                    eqBands[eqBandMoved].bandLevel = value
                    player.setTrumpetEQBands(eqBands,
                                             completion: nil,
                                             completionQueue: nil)
                }
            }
        }

    }
}
