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

import UIKit

class AudioSettingsIntroViewModel: AudioSettingsBaseViewModel {

    enum RowType: String, CaseIterable, AudioSettingRowSource {
		case dolby = "dolby"
        case dts = "dts"
        case trumpet = "trumpet"
        case melod = "melod"
        case bassboost = "bassboost"
        case reverb = "reverb"
        case equalizer = "equalizer"

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

        func title() -> String {
            switch self {
            case .dolby:
                return NSLocalizedString("Dolby", comment: "Dolby Option")
            case .dts:
                return NSLocalizedString("DTS Virtual X", comment: "DTS Option")
            case .trumpet:
                return NSLocalizedString("Trumpet", comment: "DTS Option")
            case .melod:
                return NSLocalizedString("MeloD", comment: "MeloD Option")
            case .bassboost:
                return NSLocalizedString("Bass Boost", comment: "Bass Boost Option")
            case .reverb:
                return NSLocalizedString("Preset Reverb", comment: "Preset Reverb Option")
            case .equalizer:
                return NSLocalizedString("Equalizer", comment: "Equalizer Option")
            }
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            return self.title()
        }

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

        func viewModel(player: IoTPlayer)->AudioSettingsViewModel? {
            switch self {
            case .dolby:
                return AudioSettingsDolbyViewModel()
            case .dts:
                return AudioSettingsVirtualXViewModel()
            case .trumpet:
                return AudioSettingsTrumpetViewModel()
            case .melod:
                if isLegacyMode() {
                	return AudioSettingsMeloDIntroViewModel()
                } else {
                    return AudioSettingsSimplifiedMeloDViewModel()
                }
            case .bassboost:
                return AudioSettingsBassBoostViewModel()
            case .reverb:
                return AudioSettingsPresetReverbViewModel()
            case .equalizer:
                return AudioSettingsEqualizerViewModel()
            }
        }

        func available(player: IoTPlayer) -> Bool {
            switch self {
            case .dolby:
                return player.dolbySupported
            case .dts:
                return player.virtualXSupported
            case .trumpet:
                return player.trumpetSupported
            case .melod:
                return player.meloDSupported
            case .bassboost:
                return isLegacyMode() && player.bassboostSupported
            case .reverb:
                return isLegacyMode() && player.reverbSupported
            case .equalizer:
                return isLegacyMode() && player.eqSupported
            }
        }

        func isLegacyMode() -> Bool {
            return false
        }
    }

    override var title: String { return NSLocalizedString("AUDIO SETTINGS", comment: "Audio Settings Title") }

    override func rows()->[AudioSettingsRow] {
        let allCases = RowType.allCases
        if
            let playerID = playerID,
            let player = playerController?.player(id: playerID) {
            let rows = allCases.compactMap { (rowType) -> AudioSettingsRow? in
                if rowType.available(player: player) {
                    return AudioSettingsRow(identifier: rowType.identifier(),
                                            type: AudioSettingsRowType.submenu,
                                            title: rowType.title(),
                                            accessibilityTitle: rowType.accessibilityTitle(player: player))
                } else {
                    if rowType.isLegacyMode() {
                        return AudioSettingsRow(identifier: rowType.identifier(),
                                                type: AudioSettingsRowType.simple,
                                                title: rowType.title() + " " + NSLocalizedString("(Unavailable)", comment: "Unavailable"),
                                                accessibilityTitle: rowType.accessibilityTitle(player: player))
                    } else {
                        return nil
                    }
                }
            }
            return rows
        } else {
            return []
        }
    }

    override func submenuTapped(_ row: AudioSettingsRow) {
        submenuTapped(row, rowEnum: RowType.self)
    }
}
