//
// Copyright (c) Double Symmetry GmbH
// Commercial use requires a license. See https://rntp.dev/pricing
//

import Foundation

class BrowseTreeStore {
    static let shared = BrowseTreeStore()

    static let didChangeNotification = Notification.Name("RNTPBrowseTreeDidChange")
    static let nowPlayingChangedNotification = Notification.Name("RNTPNowPlayingChanged")

    /// The current browse tree categories as raw dictionaries from JS.
    private(set) var categories: [[String: Any]] = []

    /// Reference to the AudioPlayer, set during setupPlayer().
    weak var player: AudioPlayer?

    /// The mediaId of the currently playing item, if any.
    private(set) var currentMediaId: String?

    private init() {}

    func updateNowPlaying(mediaId: String?) {
        currentMediaId = mediaId
        NotificationCenter.default.post(name: Self.nowPlayingChangedNotification, object: nil)
    }

    func update(categories: [[String: Any]]) {
        self.categories = categories
        NotificationCenter.default.post(name: Self.didChangeNotification, object: nil)
    }

    func clear() {
        self.categories = []
        self.player = nil
        NotificationCenter.default.post(name: Self.didChangeNotification, object: nil)
    }
}
