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

import UIKit

class AudioSettingsEqualizerViewModel: AudioSettingsBaseViewModel {
    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "enable"
        case preset = "preset"

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

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

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.eqEnabled)
            case .preset:
                return .string(value: player.currentEQPreset)
            }
        }

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

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

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

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

    override func rows()->[AudioSettingsRow] {
        var allRows = rows(rowEnum: RowType.self)
        guard
            let playerID = playerID,
            let player = playerController?.player(id: playerID),
            player.eqEnabled else {
                return allRows
        }
        let eqBands = player.eqBands
        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()
            player.setEQEnabled(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),
            let eqBandMoved = Int(row.identifier) {
            
            let eqBands = player.eqBands
            eqBands[eqBandMoved].bandLevel = value
            player.setEQBands(eqBands,
                              completion: nil,
                              completionQueue: nil)
        }
    }
}
