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

import UIKit

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

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

        func title() -> String {
            return NSLocalizedString("Enabled", comment: "Enabled")
        }

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            return .bool(value: player.meloDGEQEnabled)
        }

        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:"")
                }
            default:
                return ""
            }
        }

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

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

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

    override func rows()->[AudioSettingsRow] {
        var allRows = rows(rowEnum: RowType.self)
        guard
            let playerID = playerID,
            let player = playerController?.player(id: playerID),
            player.meloDGEQEnabled else {
                return allRows
        }
        let eqBands = player.meloDGEQBands
        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.setMeloDGEQEnabled(value,
                                      completion: nil,
                                      completionQueue: nil)
        }
    }

    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.meloDGEQBands
            eqBands[eqBandMoved].bandLevel = value
            player.setMeloDGEQBands(eqBands,
                                    completion: nil,
                                    completionQueue: nil)
        }
    }
}
