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

import UIKit

class AudioSettingsMusicMovieViewModel: AudioSettingsBaseViewModel {
    var valueGetter: ()->(Int)
    var valueSetter: (Int)->(Void)

    enum Preset: String, CaseIterable, AudioSettingRowSource {
        case music = "Movie"
        case movie = "Music"

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

        func title() -> String {
            return NSLocalizedString(self.rawValue, comment: "Mode")
        }

        func accessibilityTitle(player: IoTPlayer) -> String {
            return NSLocalizedString(self.rawValue, comment: "Mode")
        }

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

        func available(player: IoTPlayer) -> Bool {
            return true
        }

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            return .none // Ignored for this class.
        }
    }

    required init(getter: @escaping ()->(Int), setter: @escaping (Int)->(Void)) {
        valueGetter = getter
        valueSetter = setter
    }

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

    override func rows()->[AudioSettingsRow] {
        guard
            let playerID = playerID,
            let player = playerController?.player(id: playerID) else {
                return []
        }

        let value = valueGetter()

        var rows = [AudioSettingsRow] ()
        let allCases = Preset.allCases
        for index in 0..<allCases.count {
            let currentCase = allCases[index]
            rows.append(AudioSettingsRow(identifier: currentCase.identifier(),
                                         type: AudioSettingsRowType.checkableOption(checked: index == value),
                                         title: currentCase.title(),
                                         accessibilityTitle: currentCase.accessibilityTitle(player: player)))

        }
        return rows

    }

    override func checkableOptionChecked(_ row: AudioSettingsRow) {
        guard
            let index = Preset.allCases.firstIndex(where: { (mode) -> Bool in
                return mode.identifier() == row.identifier
            })
            else {
                return
        }

        valueSetter(index)
    }
}
