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

using namespace std;

int main()
{
    int width = 640; int height = 480;
    if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE) < 0 )
    {
        cout <<  "SDL could not initialize! SDL_Error: %s\n" << SDL_GetError() << endl;
        return 1;
    }
    SDL_Window* window = SDL_CreateWindow("My test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_BORDERLESS | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
    if(window == nullptr )
    {
        cout << "Window could not be created! SDL_Error: %s\n" << SDL_GetError() << endl;
        return 2;
    }
    SDL_Surface* screenSurface = SDL_GetWindowSurface( window );
    if (screenSurface == nullptr) {
        cout << "No surface available\n" << SDL_GetError() << endl;
    }
    SDL_Surface* cursorSurface = SDL_LoadBMP("/home/or/Pictures/cur.bmp");
    if( cursorSurface == nullptr ) {
        cout << "Failed loading image: " << SDL_GetError() << endl;
    }
    SDL_Cursor* cursor = SDL_CreateColorCursor(cursorSurface, 0, 0);
    SDL_SetCursor(cursor);
    SDL_ShowCursor(true);
    SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0x00, 0x00, 0x00 ) );
    SDL_UpdateWindowSurface( window );

    SDL_Event e; bool quit = false;
    while( quit == false ){
        while( SDL_PollEvent( &e ) ) {
            if( e.type == SDL_KEYDOWN ) {
                cout << e.key.keysym.scancode << endl;
                if (e.key.keysym.scancode == 41)
                    quit = true;
                if (e.key.keysym.scancode == 30) {
                    //This happense in the 0ad code when switching between fullscreen and window modes, but
                    // I don't see any need for it...
                    SDL_SetWindowSize(window, width, height);
                    SDL_SetWindowPosition(window, 300, 300);
                    //Switch to full screen
                    if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN) < 0)
                        cout << SDL_GetError() << endl;
                    //draw on surface to update screen to full screen, without this, the full screen doesn't show
                    screenSurface = SDL_GetWindowSurface(window);
                    SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0x00, 0xFF, 0x00 ) );
                    SDL_UpdateWindowSurface( window );
                }
                if (e.key.keysym.scancode == 31) {
                    //This happense in the 0ad code when switching between fullscreen and window modes, but
                    // I don't see any need for it...
                    SDL_SetWindowSize(window, width, height);
                    SDL_SetWindowPosition(window, 300, 300);
                    if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP) < 0)
                        cout << SDL_GetError() << endl;
                    screenSurface = SDL_GetWindowSurface(window);
                    SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0x00, 0x00 ) );
                    SDL_UpdateWindowSurface( window );
                }
                if (e.key.keysym.scancode == 32) {
                    //This happense in the 0ad code when switching between fullscreen and window modes, but
                    // I don't see any need for it...
                    SDL_SetWindowSize(window, width, height);
                    SDL_SetWindowPosition(window, 300, 300);

                    if (SDL_SetWindowFullscreen(window, 0) < 0)
                        cout << SDL_GetError() << endl;
                    screenSurface = SDL_GetWindowSurface(window);
                    SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0x00, 0x00, 0xFF ) );
                    SDL_UpdateWindowSurface( window );
                }
            }
            if (e.type == SDL_MOUSEMOTION) {
                cout << e.motion.x << ", " << e.motion.y << endl;
            }
        }
    }
    SDL_DestroyWindow( window );
    SDL_FreeCursor(cursor);
    SDL_FreeSurface(cursorSurface);
    SDL_Quit();
    return 0;

}
