#include <iostream>
#include <SDL2/SDL.h>

#define LARGEUR 640
#define HAUTEUR 480

int main ( int argc, char** argv )
{
    /// [1] Démarrage
    // [1.1] Démarrages SDL
    if ( SDL_Init( SDL_INIT_JOYSTICK) < 0)
    {
        std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
        return 1;
    }

    // [1.2] Préparation de fermeture
    atexit(SDL_Quit);

    /// [2] Préparation des composants
    // [2.1] Préparation de la manette
    if (SDL_NumJoysticks()<1)
    {
        std::cout<< "Il n'y a aucune manette de branchée."<<std::endl
        <<"Veuillez réessayer avec un pad connecté."<<std::endl;
        return 1;
    }

    SDL_Joystick *joystick;

    //SDL_JoystickEventState(SDL_ENABLE);
    //SDL_GameControllerEventState(SDL_ENABLE);
    joystick = SDL_JoystickOpen(0);
    if (joystick == NULL)
        std::cout << "Manette bugée " << SDL_GetError() << std::endl;


    // [2.3] Affichage en console
    std::cout << "Il y a " <<SDL_NumJoysticks()<< " manette(s) disponible(s)." <<std::endl;
    std::cout << "La manette selectionnée est : "<<SDL_JoystickName(joystick)<<". "<<std::endl
    << "Elle possède " <<SDL_JoystickNumAxes(joystick)<< " axes; "<<std::endl
    << "Mais également " <<SDL_JoystickNumButtons(joystick)<< " boutons. " << std::endl;

    std::cout << "Lancement du test pour "<<SDL_JoystickName(joystick)<<". "<<std::endl;


    /// [3] Boucle principale
    bool done = false;
    while (!done)
    {
        // [3.1] Gestion évènements
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                break;
            case SDL_KEYDOWN:
                break;
            case SDL_JOYAXISMOTION:  /* Handle Joystick Motion */
                std::cout << std::endl;
                if ( ( event.jaxis.value < -1/*-3200*/ ) || (event.jaxis.value > 1/*3200*/ ) )
                {
                    if( event.jaxis.axis == 0)
                    {
                        std::cout<<"Mouvement du stick 1 en x.";
                    }

                    if( event.jaxis.axis == 1)
                    {
                        std::cout<<"Mouvement du stick 1 en y.";
                    }

                    if( event.jaxis.axis == 2)
                    {
                        std::cout<<"Mouvement vertical, axe y nemero 1.";
                    }

                    if( event.jaxis.axis == 3)
                    {
                        std::cout<<"Mouvement du deuxième stick par x.";
                    }

                    if( event.jaxis.axis == 4)
                    {
                        std::cout<<"Mouvement du deuxième stick par y.";
                    }

                    if( event.jaxis.axis == 5)
                    {
                        std::cout<<"Mouvement vertical du second axe :";
                    }

                    std::cout<<" Nouvelle position :"<<event.jaxis.value<<std::endl;
                }
                break;
            } // end switch event type
        } // end of message processing
    } //fin bcl principale

    ///[4] Destruction des composants
    SDL_JoystickClose(joystick);


    std::cout << "Aucune erreur détectée." << std::endl;
    return 0;
}