// Inludes
#include "engine.h"

// Variables
SDL_Surface* assets;
Engine *engine;

// Functions
// Quit function
void quit()
{
    // Free assets
    SDL_FreeSurface(assets);
    delete engine;
    // Exit program
    exit(0);
}

// Main function
int main(int argc, char* argv[])
{   
    engine = new Engine("Game", 800, 600);
    engine->targetFrameRate = 60;

    // Load assets
    assets = SDL_LoadBMP("example_assets/sprites.bmp");

    // frame map
    std::map<std::string, SDL_Rect> idle = {
            { "idle", { 76, 456, 68, 128 } },
    };

    std::map<std::string, SDL_Rect> front = {
            { "front", { 4, 456, 68, 125 } },
            { "front2", { 76, 456, 68, 128 } },
            { "front3", { 148, 456, 68, 125 } }
    };

    std::map<std::string, SDL_Rect> back = {
            { "back", { 662, 456, 68, 125 } },
            { "back2", { 734, 456, 68, 128 } },
            { "back3", { 806, 456, 68, 125 } }
    };

    std::map<std::string, SDL_Rect> walk = {
            { "walk", { 220, 456, 68, 124 } },
            { "walk2", { 292, 456, 73, 127 } },
            { "walk3", { 369, 456, 68, 124 } }
    };

    // Create sprite and animation
    Sprite* sprite = new Sprite(engine->renderer, assets);
    AnimationManager* animations = new AnimationManager(sprite);
    animations->addAnimation("idle", idle, 10, false, true, false, false);
    animations->addAnimation("walk_left", walk, 10, false, true, true, false);
    animations->addAnimation("walk_right", walk, 10, true, true, true, false);
    animations->addAnimation("walk_back", back, 10, false, true, true, false);
    animations->addAnimation("walk_front", front, 10, false, true, true, true);
    animations->setAnimation("idle");

    // Main loop
    while (true)
    {
        // Start time
        engine->startFrame();

        // Update input
        engine->update();

        // Exit if window is closed
        if (engine->input.exit)
        {
            quit();
        }

        if (engine->input.activeKeys[SDLK_d])
        {
            sprite->dstrect.x += 1;
            animations->setAnimation("walk_right");
        }
        if (engine->input.activeKeys[SDLK_a])
        {
            sprite->dstrect.x -= 1;
            animations->setAnimation("walk_left");

        }
        if (engine->input.activeKeys[SDLK_w])
        {
            sprite->dstrect.y -= 1;
            animations->setAnimation("walk_back");
        }
        if (engine->input.activeKeys[SDLK_s])
        {
            sprite->dstrect.y += 1;
            animations->setAnimation("walk_front");

        }
        if (!engine->input.activeKeys[SDLK_d] && !engine->input.activeKeys[SDLK_a] && !engine->input.activeKeys[SDLK_w] && !engine->input.activeKeys[SDLK_s])
        {
            animations->setAnimation("idle");
        }

        // Update animation
        animations->update();

        // Clear screen
        engine->clear({ 100, 150, 255, 255 });

        // Draw sprite
        animations->draw(engine->renderer);

        // Update screen
        engine->render();
    }
    return 0;
}