#include "raylib.h"
#include "raygui.h"

#define SCALEFACTOR 4
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
struct Master {
    float renderWidth;
    float renderHeight;
    float virtualWidth;
    float virtualHeight;
    float scalefactor;
};

//Function Declarations
//----------------------------------------------------------------------------------
void Virtual_Screen_To_Renderer(RenderTexture2D target, struct Master* g);
//----------------------------------------------------------------------------------

int main(void)
{
    //----------------------------------------------------------------------------------
    // Initialization
    //----------------------------------------------------------------------------------
    SetConfigFlags(FLAG_FULLSCREEN_MODE);
    InitWindow(0, 0, "StockBot");
    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    GuiLoadStyle("include/styles/terminal/style_terminal.rgs");
    
    struct Master g = (struct Master)
    {
        .renderHeight = GetRenderHeight(),
        .renderWidth = GetRenderWidth(),
        .virtualHeight = g.renderHeight / SCALEFACTOR,
        .virtualWidth  = g.renderWidth  / SCALEFACTOR,
        .scalefactor = SCALEFACTOR,
    };

    // Create offscreen target and make scaling crisp
    RenderTexture2D target = LoadRenderTexture(g.virtualWidth, g.virtualHeight);
    SetTextureFilter(target.texture, TEXTURE_FILTER_POINT);

    // Tell raygui/mouse to use virtual coords
    SetMouseOffset(0, 0);
    SetMouseScale((float)g.virtualWidth/g.renderWidth, (float)g.virtualHeight/g.renderHeight);

    //----------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        //----------------------------------------------------------------------------------
        // Update
        //----------------------------------------------------------------------------------

        // keep mouse scaled to virtual space (good to set each frame)
        SetMouseScale((float)g.virtualWidth/g.renderWidth, (float)g.virtualHeight/g.renderHeight);


        //----------------------------------------------------------------------------------
        // Draw
        //----------------------------------------------------------------------------------
        // Here we draw to our virtual screen "target"
        BeginTextureMode(target);
            ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));


        EndTextureMode();

        //this is where we take what we drew to the virutal screen and actually render it on the real screen.
        BeginDrawing();
            Virtual_Screen_To_Renderer(target, &g);
        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    //----------------------------------------------------------------------------------
    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadRenderTexture(target);
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

void Virtual_Screen_To_Renderer(RenderTexture2D target, struct Master* g)
{   
    ClearBackground(BLACK);
    // Source is the texture size
    // note: Y is inverted for render textures (when drawing we go from the top left corner, but in textures they are drawing from the bottom left.)
    Rectangle src = { 0, 0, (float)target.texture.width, -(float)target.texture.height};
    Rectangle dst = { 0, 0, (float)g->renderWidth, (float)g->renderHeight }; // scale to full screen (2×)
    DrawTexturePro(target.texture, src, dst, (Vector2){0,0}, 0.0f, WHITE);
}
