#include "jail_audio.h"
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <stdio.h>

SDL_Event event;
SDL_Window *sdlWindow;


int main(int argc, char **argv) {
    SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS);
    sdlWindow = SDL_CreateWindow("JailAudio", 320, 240, 0);
    JA_Init(48000, SDL_AUDIO_S16, 2);

    JA_Music_t *music = JA_LoadMusic("intro2.ogg");
    JA_Sound_t *peiv = JA_LoadSound("menu_select.wav");
    int channel = -1;

    JA_PlayMusic(music, -1);
    float volume = 1.0f;
    bool should_exit = false;
    while(!should_exit) {
        while(SDL_PollEvent(&event)) {
            if (event.type == SDL_EVENT_QUIT) { should_exit = true; break; }
            if (event.type == SDL_EVENT_KEY_DOWN) {
                switch (event.key.scancode) {
                    case SDL_SCANCODE_1:                // Si pulsem la tecla '1' pausem o despausem la música
                        if (JA_GetMusicState() == JA_MUSIC_PLAYING) { JA_PauseMusic(); } else { JA_ResumeMusic(); }
                        break;
                    case SDL_SCANCODE_2:                // Si pulsem la tecla '2' sona el wav 1 vegada
                        JA_PlaySound(peiv);
                        break;
                    case SDL_SCANCODE_3:                // Si pulsem la tecla '3' sona el wav 3 vegades
                        JA_PlaySound(peiv, 2);
                        break;
                    case SDL_SCANCODE_4:                // Si pulsem la tecla '4' sona el wav infinitament
                        channel = JA_PlaySound(peiv, -1);
                        break;
                    case SDL_SCANCODE_5:                // Si pulsem la tecla '5' pausem o despausem el wav que sonaba infinitament
                        if (JA_GetChannelState(channel) == JA_CHANNEL_PLAYING) { JA_PauseChannel(channel); } else { JA_ResumeChannel(channel); }
                        break;
                    case SDL_SCANCODE_6:                // Si pulsem la tecla '6' stopem definitivament el wav infinit
                        JA_StopChannel(channel);
                        break;
                    case SDL_SCANCODE_7:
                        JA_PlayMusic(music, 0);         // Si pulsem la tecla '7' fa sonar la música UNA negada nomes.
                        break;
                    case SDL_SCANCODE_0:                // Si pulsem la tecla '0' stopem la música amb un fade
                        JA_FadeOutMusic(1000);
                        break;
                    case SDL_SCANCODE_UP:
                        volume = JA_SetVolume(volume+0.1f);
                        break;
                    case SDL_SCANCODE_DOWN:
                        volume = JA_SetVolume(volume-0.1f);
                        break;
                    case SDL_SCANCODE_ESCAPE:
                        should_exit = true;
                        break;
                    default:
                        break;
                }
                
            }
        }
    }
    JA_DeleteSound(peiv);
    JA_DeleteMusic(music);
    JA_Quit();
    SDL_DestroyWindow(sdlWindow);
    SDL_Quit();
    return 0;
}

