import AVFoundation
import NitroModules

// Sync playback-state reads and player property get/set.
// Split out of HybridPlaybackEngine.swift; shared state and helpers
// remain on the main type.
extension HybridPlaybackEngine {
    // MARK: - Playback State (sync reads — cached from domain events)

    var state: PlaybackState { _state }

    var mediaPosition: MediaPosition { _mediaPosition }

    var currentItem: (any HybridMediaItemSpec)? { _currentItem }

    var currentItemSource: ItemSource { _currentItemSource }

    var isLive: Bool { _isLive }

    var isPlaying: Bool { _state == .playing }

    var isPaused: Bool { _state == .paused }

    var isLoaded: Bool {
        _state == .ready || _state == .playing || _state == .paused || _state == .buffering
    }

    var availableSubtitleTracks: [SubtitleTrack] { _availableSubtitleTracks }

    var availableAudioTracks: [AudioTrack] { _availableAudioTracks }

    // MARK: - Player Properties (sync get/set — C coordinator is source of truth)

    var volume: Double {
        get { _volume }
        set {
            _volume = newValue
            if let coord = coordinator { aviation_coordinator_set_volume(coord, newValue) }
        }
    }

    var rate: Double {
        get { _rate }
        set {
            _rate = newValue
            if let coord = coordinator { aviation_coordinator_set_rate(coord, newValue) }
        }
    }

    var muted: Bool {
        get { _muted }
        set {
            _muted = newValue
            if let coord = coordinator { aviation_coordinator_set_muted(coord, newValue) }
        }
    }

    var loop: Bool {
        get { _loop }
        set {
            _loop = newValue
            if let coord = coordinator { aviation_coordinator_set_loop(coord, newValue) }
            decoder?.setLoop(newValue)
        }
    }
}
