#include "game_audio.h"

#include "soundbank.h"
#include "soundbank_bin.h"

void audio_setup(AudioContext &audio) {
    mmInitDefaultMem((mm_addr)soundbank_bin);

    mmLoad(MOD_0RIGIN);

    mmLoadEffect(SFX_332629__TREASURESOUNDS__ITEM_PICKUP);
    mmLoadEffect(SFX_BOOM);
    mmLoadEffect(SFX_167127__CRISSTANZA__PAUSE);
    mmLoadEffect(SFX_167126__CRISSTANZA__UNPAUSE);
    mmLoadEffect(SFX_273792__PACOMAV__PLOP);
    mmLoadEffect(SFX_73750__TIMBRE__REMIX_OF_BENBONCAN_SAD_TROMBONE_MORE_WAH_BRIGHT_DE_CLICKED);

    audio.sfx_pickup = {
        { SFX_332629__TREASURESOUNDS__ITEM_PICKUP },
        (int)(1.0f * (1 << 10)),
        0,
        255,
        128,
    };

    audio.sfx_boom = {
        { SFX_BOOM },
        (int)(1.0f * (1 << 10)),
        1,
        255,
        128,
    };

    audio.sfx_pause = {
        { SFX_167127__CRISSTANZA__PAUSE },
        (int)(1.0f * (1 << 10)),
        2,
        255,
        128,
    };

    audio.sfx_unpause = {
        { SFX_167126__CRISSTANZA__UNPAUSE },
        (int)(1.0f * (1 << 10)),
        3,
        255,
        128,
    };

    audio.sfx_plop = {
        { SFX_273792__PACOMAV__PLOP },
        (int)(1.0f * (1 << 10)),
        4,
        128,
        128,
    };

    audio.sfx_fail = {
        { SFX_73750__TIMBRE__REMIX_OF_BENBONCAN_SAD_TROMBONE_MORE_WAH_BRIGHT_DE_CLICKED },
        (int)(1.0f * (1 << 10)),
        4,
        255,
        128,
    };
}

void audio_start_music(const AudioContext &audio) {
    mmStart(MOD_0RIGIN, MM_PLAY_LOOP);
    if (!audio.musicEnabled) {
        mmPause();
    }
}

void audio_stop_music() {
    mmStop();
}

void audio_pause_music_if_enabled(const AudioContext &audio) {
    if (audio.musicEnabled) {
        mmPause();
    }
}

void audio_resume_music_if_enabled(const AudioContext &audio) {
    if (audio.musicEnabled) {
        mmResume();
    }
}

void audio_play_pickup(const AudioContext &audio) {
    mmEffectEx(&audio.sfx_pickup);
}

void audio_play_boom(const AudioContext &audio) {
    mmEffectEx(&audio.sfx_boom);
}

void audio_play_plop(const AudioContext &audio) {
    mmEffectEx(&audio.sfx_plop);
}

void audio_play_pause(const AudioContext &audio) {
    mmEffectEx(&audio.sfx_pause);
}

void audio_play_unpause(const AudioContext &audio) {
    mmEffectEx(&audio.sfx_unpause);
}

void audio_play_fail(const AudioContext &audio) {
    mmEffectEx(&audio.sfx_fail);
}

bool audio_toggle_music(AudioContext &audio) {
    audio.musicEnabled = !audio.musicEnabled;

    if (audio.musicEnabled) {
        mmResume();
    } else {
        mmPause();
    }

    return audio.musicEnabled;
}

bool audio_is_music_enabled(const AudioContext &audio) {
    return audio.musicEnabled;
}
