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

#include "App.hh"

// #ifdef _WIN32
//     #pragma comment(linker, "/subsystem:windows")
//     #define VK_USE_PLATFORM_WIN32_KHR
//     #define PLATFORM_SURFACE_EXTENSION_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME
// #endif

#define SCREEN_WIDTH    800
#define SCREEN_HEIGHT   600

namespace App
{    
    App::App() : _return_code(0)
    {
        std::cout << "Eternity Engine" << std::endl;
        _logger = new Logger();
        if(SDL_Init(SDL_INIT_VIDEO) < 0)
        {
            std::cout << "SDL could not be initialized!" << std::endl
                    << "SDL_Error: " << SDL_GetError() << std::endl;
            _return_code=-1;
            return;
        }
        // SDL_Vulkan_LoadLibrary(nullptr);
        SDL_Window *window = SDL_CreateWindow("Basic C++ SDL project",
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SCREEN_WIDTH, SCREEN_HEIGHT,
                                          SDL_WINDOW_SHOWN);
        if(!window)
        {
            std::cout << "Window could not be created!" << std::endl
                    << "SDL_Error: " << SDL_GetError() << std::endl;
            _return_code=-1;
            return;
        }
        // Create renderer
        SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
        if(!renderer)
        {
            std::cout << "Renderer could not be created!" << std::endl
                      << "SDL_Error: " << SDL_GetError() << std::endl;
            _return_code=-1;
            return;
        }
        else
        {
            // Declare rect of square
            SDL_Rect squareRect;

            // Square dimensions: Half of the min(SCREEN_WIDTH, SCREEN_HEIGHT)
            squareRect.w = std::min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
            squareRect.h = std::min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;

            // Square position: In the middle of the screen
            squareRect.x = SCREEN_WIDTH / 2 - squareRect.w / 2;
            squareRect.y = SCREEN_HEIGHT / 2 - squareRect.h / 2;


            // Event loop exit flag
            bool quit = false;

            // Event loop
            while(!quit)
            {
                SDL_Event e;

                // Wait indefinitely for the next available event
                SDL_WaitEvent(&e);

                // User requests quit
                if(e.type == SDL_QUIT)
                {
                    quit = true;
                }

                // Initialize renderer color white for the background
                SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);

                // Clear screen
                SDL_RenderClear(renderer);

                // Set renderer color red to draw the square
                SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);

                // Draw filled square
                SDL_RenderFillRect(renderer, &squareRect);

                // Update screen
                SDL_RenderPresent(renderer);
            }

            // Destroy renderer
            SDL_DestroyRenderer(renderer);
        }

        // Destroy window
        SDL_DestroyWindow(window);
        SDL_Quit();
    }
    
    App::~App()
    {
        _logger->~Logger();
        delete _logger;
    }

    int App::getReturnCode() const 
    {
        return _return_code;
    }
    
} // namespace App
