#include <stdlib.h>
#include <assert.h>
#include <raylib.h>

static struct {
	unsigned int	size;
	Music		*music;
}	atlas = {0};

static Music	current = {0};

#define MAX_TIME_WITHOUT_MUSIC 10

unsigned int	haven_music_init(const char **music_files, unsigned int n_music) {
	atlas.music = malloc(sizeof(Music) * n_music);
	assert(atlas.music);

	for (int i = 0; i < n_music; i++) {
		Music	span = LoadMusicStream(music_files[i]);
		if (IsMusicValid(span)) {
			atlas.size++;
			atlas.music[i] = span;
		}
	}
	return (atlas.size);
}

void	haven_music_play(unsigned int idx) {
	if (IsMusicStreamPlaying(current)) {
		StopMusicStream(current);
	}
	current = atlas.music[idx];
	PlayMusicStream(current);
}

void	haven_music_update(void) {
	static double value = 0.0;

	if (!IsMusicStreamPlaying(current)) {
		value += GetFrameTime();
		if (value >= MAX_TIME_WITHOUT_MUSIC) {
			int rand = GetRandomValue(0, atlas.size - 1);
			current = atlas.music[rand];
			PlayMusicStream(current);
			value = 0.0;
		}
	}
	UpdateMusicStream(current);
}

void	haven_music_close(void) {
	for (int i = 0; i < atlas.size; i++) {
		UnloadMusicStream(atlas.music[i]);
	}
}

#include <stdlib.h>
#include <assert.h>
#include <raylib.h>

static struct {
	unsigned int	size;
	Sound		*sound;
}	atlas;


/*
sound queue
*/

#define MAX_CONCURENT_SOUND 10
struct {
	unsigned int	idx;
	Sound		data[MAX_CONCURENT_SOUND];
}	queue;

static void	sound_queue_add(Sound sound) {
	Sound toplay = LoadSoundAlias(sound);

	if (IsSoundValid(queue.data[queue.idx])) {
		UnloadSoundAlias(queue.data[queue.idx]);
	}
	PlaySound(toplay);
	queue.data[queue.idx] = toplay;
	queue.idx ++;
	queue.idx %= 10;
}

/*
	actual implementation
*/

bool	haven_sound_play(unsigned int idx) {
	if (!IsSoundValid(atlas.sound[idx])) {
		return (false);
	}
	sound_queue_add(atlas.sound[idx]);
	return (true);
}

unsigned int	haven_sound_init(const char **sound_files, unsigned int n_sound) {
	atlas.sound = malloc(sizeof(Sound) * n_sound);
	assert(atlas.sound);

	for (int i = 0; i < n_sound; i++) {
		Sound	tmp_sound = LoadSound(sound_files[i]);
		if (IsSoundValid(tmp_sound)) {
			atlas.size++;
			atlas.sound[i] = tmp_sound;
		}
	}
	queue.idx = 0;
	return (atlas.size);
}

void	haven_sound_close(void) {
	for (int i = 0; i < atlas.size; i++) {
		UnloadSound(atlas.sound[i]);
	}
}
