#ifndef PLAYER_H
#define PLAYER_H

#include <stdint.h>
#include <SDL2/SDL.h>
#include <time.h>

#include "channel.h"
#include "frame_queue.h"

typedef struct Player {
    FrameQueue frame_queue;
    VideoBufferPool video_buffer_pool;
    SDL_Thread *thread;
    SDL_atomic_t stop_requested;
    const ChannelList *channels;
    int current_index;
    Uint64 app_start_ticks;
    Uint32 tuning_blackout_until;
    SDL_AudioDeviceID audio_device;
    SDL_AudioSpec audio_spec;
    SDL_mutex *audio_mutex;
    SDL_mutex *clock_mutex;
    double latest_audio_end_pts;
    int has_audio_stream;
    int audio_started;
    int preroll_ready;
    Uint32 catchup_until;
    char audio_driver[32];
    SDL_mutex *error_mutex;
    char last_error[256];
    uint8_t *audio_convert_buffer;
    int audio_convert_buffer_size;
    int audio_convert_buffer_samples;
} Player;

int player_init(Player *player, const ChannelList *channels, Uint64 app_start_ticks);
void player_destroy(Player *player);
int player_tune(Player *player, int channel_index);
int player_consume_latest_frame(Player *player, FrameData *out);
int player_consume_synced_frame(Player *player, FrameData *out, double clock_seconds, double lead_tolerance);
double player_live_position(const Player *player, Uint64 now_ticks);
double player_get_sync_clock(Player *player, Uint64 now_ticks);
void player_set_catchup_until(Player *player, Uint32 tick);
int player_is_in_blackout(const Player *player);
void player_resume_audio(Player *player);
void player_get_error(Player *player, char *buffer, size_t buffer_size);

#endif
