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

SDL_Event event;
SDL_Window *sdlWindow;


int main(int argc, char **argv) {
    SDL_Init(SDL_INIT_EVERYTHING);
    sdlWindow = SDL_CreateWindow("JailAudio", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_SHOWN);
    JA_Init(48000, AUDIO_S16, 2);

    JA_Music music = JA_LoadMusic("intro2.ogg");
    JA_Sound peiv = JA_LoadSound("menu_select.wav");
    int channel = -1;

    JA_PlayMusic(music, true);
    int volume = 128;
    bool should_exit = false;
    while(!should_exit) {
        while(SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) { should_exit = true; break; }
            if (event.type == SDL_KEYDOWN) {
                switch (event.key.keysym.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_UP:
                        volume = JA_SetVolume(volume+16);
                        break;
                    case SDL_SCANCODE_DOWN:
                        volume = JA_SetVolume(volume-16);
                        break;
                    default:
                        break;
                }
                
            }
        }
    }
    JA_DeleteSound(peiv);
    JA_DeleteMusic(music);
    SDL_DestroyWindow(sdlWindow);
    SDL_Quit();
    return 0;
}

