// Copyright (c) 2023 Alex Diener. All rights reserved.

#include "audiosynth/AudioSampler_generalSynth.h"
#include "audiosynth/GeneralSynthConfiguration.h"
#include "binaryserialization/BinaryDeserializationContext.h"
#include "PROJECT_NAME/Globals.h"
#include "PROJECT_NAME/SharedDefinitions.h"
#include "PROJECT_NAME/Sounds.h"
#include <stdio.h>

static struct {
	const void * embedData;
	unsigned int embedSize;
} embeddedSounds[SOUND_COUNT];

void initSoundEffects(void) {
	#define DEFINE_SOUND(sound_id, embed_suffix, load_priority, category) { \
		extern const char EMBEDDATA_##embed_suffix[]; \
		extern unsigned int EMBEDSIZE_##embed_suffix; \
		embeddedSounds[sound_id].embedData = EMBEDDATA_##embed_suffix; \
		embeddedSounds[sound_id].embedSize = EMBEDSIZE_##embed_suffix; \
		AudioManager_registerSound(sound_id, category, load_priority); \
	}
	#define OVERRIDE_SOUND(sound_id_overriding, sound_id_overridden) \
		AudioManager_registerSoundOverride(sound_id_overriding, sound_id_overridden);
	#include "PROJECT_NAME/Sound_list.h"
	#undef DEFINE_SOUND
	#undef OVERRIDE_SOUND
}

PCMAudio * loadSoundEffect(SoundID soundID) {
	BinaryDeserializationContext * deserializationContext;
	deserializationContext = BinaryDeserializationContext_createWithBytes(embeddedSounds[soundID].embedData, embeddedSounds[soundID].embedSize);
	GeneralSynthConfiguration * configuration = GeneralSynthConfiguration_deserialize(deserializationContext);
	BinaryDeserializationContext_dispose(deserializationContext);
	AudioSamplerParameters_multitrack * parameters = GeneralSynthConfiguration_createAudioSamplerParameters(configuration);
	call_virtual(dispose, configuration);
	PCMAudio * audio = AudioSampler_generalSynth_createPCMAudio(parameters, 1.0f, 2, 1, g_audioSampleRate);
	call_virtual(dispose, parameters);
	return audio;
}

void playSoundEffect(SoundID soundID) {
	AudioManager_playSound(soundID);
}

void beginSoundEffectGroup(void) {
	AudioManager_beginSoundGroup();
}

void endSoundEffectGroup(void) {
	AudioManager_endSoundGroup();
}
