// The face the music player wears in Listening, as the core answers it // (`shell.player`: `face`, and the hand's ring in `faces`); the core keeps // the `player` row of face_settings, the face and the knobs alike. import CottonKit import CottonValues import Foundation /// The faces a song plays on, by their catalogue ids. enum PlayerFace: String, CaseIterable, Sendable { case hifi, turntable, amp, party, tray, cassette /// The face's id in the catalogue (`song.hifi`). var face: DefinitionCardKind { switch self { case .hifi: .songHifi case .turntable: .songTurntable case .amp: .songAmp case .party: .songParty case .tray: .songTray case .cassette: .songCassette } } /// The face whose catalogue id is `id`, if it is one of them. init?(face id: String) { guard let face = Self.allCases.first(where: { $0.face.rawValue == id }) else { return nil } self = face } /// Its name on the hand, as the core's ring says it. @MainActor var said: String { MusicNowPlaying.shared.faces.first { $0.id == face.rawValue }?.label ?? "" } } struct PlayerSettings: Equatable, Sendable { /// The face worn. var face: PlayerFace = .hifi /// The face worn, as the core answers it. static func read(_ core: any CottonCalling) async -> PlayerSettings { do { let answer = try await core.call(CottonMethod.shellPlayer, [String: RecordValue](), as: PlayerAnswer.self) await MusicNowPlaying.shared.hold(faces: answer.faces) guard let face = PlayerFace(face: answer.face) else { Log.write("player: the core wears a face this build does not draw: \(answer.face)") return PlayerSettings() } return PlayerSettings(face: face) } catch { Log.write("player: the face worn could not be read: \(error.localizedDescription)") return PlayerSettings() } } /// The face the hand chose, written on the player's row by the core. static func wear(_ face: PlayerFace, core: any CottonCalling) async throws { let answer = try await core.call(CottonMethod.shellPlayer, ["op": RecordValue.text("settings"), "face": .text(face.face.rawValue)], as: PlayerAnswer.self) await MusicNowPlaying.shared.hold(faces: answer.faces) } }