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

import UIKit

class AudioSettingsMeloDMatrixViewModel: AudioSettingsBaseViewModel {
    typealias ModePreset = AudioSettingsMusicMovieViewModel.Preset

    private enum RowType: String, CaseIterable, AudioSettingRowSource {
        case enable = "enable"
        case decoderMode = "decoderMode"

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

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

        func value(player: IoTPlayer) -> AudioSettingRowSourceResult {
            switch self {
            case .enable:
                return .bool(value: player.meloDMatrixEnabled)
            case .decoderMode:
                let index = min(Int(player.meloDMatrixMode.rawValue), ModePreset.allCases.count)
                let option = NSLocalizedString(ModePreset.allCases[index].rawValue, comment: "Mode")
                return .string(value: option)
            }
        }

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

        func viewModel(player: IoTPlayer)->AudioSettingsViewModel? {
            let vm = AudioSettingsMusicMovieViewModel(getter: { [weak player] in
                guard let player = player else { return 0 }
                return player.meloDMatrixMode.rawValue
                }, setter: { [weak player] (intValue) in
                    guard let player = player else { return }
                    if let cValue = IoTMatrixMode(rawValue: intValue) {
                        player.setMeloDMatrixMode(cValue,
                                                  completion: nil,
                                                  completionQueue: nil)
                    }
            })
            return vm
        }

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

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

    override func rows()->[AudioSettingsRow] {
        return rows(rowEnum: RowType.self)
    }

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

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