#include "pch.h"

#if defined(_DEBUG)

#include "vld.h"

#endif

#undef main

#include "Renderer.h"
#include "GamePadController.h"

using namespace dae;

void ShutDown(SDL_Window *pWindow) {
    SDL_DestroyWindow(pWindow);
    SDL_Quit();
}

int main(int argc, char *args[]) {
    //Unreferenced parameters
    (void) argc;
    (void) args;

    //Create window + surfaces
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);

    const uint32_t width = 640;
    const uint32_t height = 480;

    SDL_Window *pWindow = SDL_CreateWindow(
            "DirectX - Bram Verhulst - 2GD11E",
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            width, height, 0);

    if (!pWindow)
        return 1;


    int joysticks = SDL_NumJoysticks();
    std::cout << "Number of joysticks connected: " << joysticks << std::endl;

    GamePadController::GetInstance().Init();


    //Initialize "framework"
    const auto pTimer = new Timer();
    const auto pRenderer = new Renderer(pWindow);

    //Start loop
    pTimer->Start();
    float printTimer = 0.f;
    bool isLooping = true;
    while (isLooping) {
        //--------- Get input events ---------
        SDL_Event e;
        while (SDL_PollEvent(&e)) {
            switch (e.type) {
                case SDL_QUIT:
                    isLooping = false;
                    break;
                case SDL_KEYUP: {
                    SDL_Scancode scancode = e.key.keysym.scancode;
                    if(scancode == SDL_SCANCODE_F1){
                        //Switch backend
                        pRenderer->SwitchBackend();
                    }
                    if(scancode == SDL_SCANCODE_F2){
                        //toggle rotation
                        pRenderer->ToggleRotation();
                    }
                    if(scancode == SDL_SCANCODE_F9){
                        // Cycle Cull mode
                    }
                    if(scancode == SDL_SCANCODE_F10){
                        // Toggle Uniform Clear Color
                        pRenderer->ToggleUniformClearColor();
                    }
                    if(scancode == SDL_SCANCODE_F11){
                        // toggle Print fps
                    }

                    //Hardware
                    if (scancode == SDL_SCANCODE_F3){
                        // Toggle FireFX mesh
                    }
                    if (scancode == SDL_SCANCODE_F4){
                        //Toggle texture sampling states
                        pRenderer->NextSamplingState();
                    }
                    //Software
                    if(scancode == SDL_SCANCODE_F5){
                        //Cycle shading mode
                    }
                    if(scancode == SDL_SCANCODE_F6){
                        //Toggle normal mapping
                    }
                    if(scancode == SDL_SCANCODE_F7){
                        //Toggle Depth Buffer
                    }
                    if(scancode == SDL_SCANCODE_F8) {
                        //Toggle BoundingBox
                    }

                    if (e.key.keysym.scancode == SDL_SCANCODE_F2) {
                    }
                    if (e.key.keysym.scancode == SDL_SCANCODE_F3) {

                    }
                    if (e.key.keysym.scancode == SDL_SCANCODE_F4) {
                        pRenderer->ToggleNormals();
                    }
                    break;
                }
                default:
                    break;
            }
        }

        GamePadController::GetInstance().Update();
        //--------- Update ---------
        pRenderer->Update(pTimer);

        //--------- Render ---------
        pRenderer->Render();

        //--------- Timer ---------
        pTimer->Update();
        printTimer += pTimer->GetElapsed();
        if (printTimer >= 1.f) {
            printTimer = 0.f;
            std::cout << "dFPS: " << pTimer->GetdFPS() << std::endl;
        }
    }
    pTimer->Stop();

    //Shutdown "framework"
    delete pRenderer;
    delete pTimer;

    ShutDown(pWindow);
    return 0;
}