/*
- Embedded windows
- Multiple buffers at the same time

- Ctrl+C
- Ctrl+V
- Ctrl+X
- Open a document dialog, save document to disk
- Buffer bound  undo,redo
*/
import "raylib";
import "std_types";
import "libc";

InvalidCodepath :: proc() {
    assert(:*char("invalid codepath") == :*char(""));
}

main :: proc(): int {
    InitWindow(800, 600, "TextEditor");
    SetWindowState(FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_MAXIMIZED);
    SetTargetFPS(60);

    font_size: float = 14;
    font_spacing: float = 1;
    font: Font = LoadFontEx("C:/Windows/Fonts/consola.ttf", :int(font_size), nil, 0);

    SANDBOX_TEXT_EDITOR :: 1;
    SANDBOX_PROTOTYPE :: 2;
    sandbox_chosen := SANDBOX_TEXT_EDITOR;

    for !WindowShouldClose() {
        screen_size: Vector2 = {:f32(GetScreenWidth()), :f32(GetScreenHeight())};
        screen_rect := Rect2PSize(0, 0, screen_size.x, screen_size.y);
        top_bar := CutTop(&screen_rect, 30);
        top_bar_original := top_bar;

        button1_text := "Text Editor";
        button1_text_size := MeasureTextEx(font, button1_text, font_size, font_spacing);
        button1 := CutLeft(&top_bar, :float(button1_text_size.x) * 1.5);
        button1 = Shrink(button1, 4);
        button1_text_pos: Vector2 = {
            x = button1.min.x + (GetRectX(button1) - :float(button1_text_size.x)) * 0.5,
            y = button1.min.y + (GetRectY(button1) - :float(button1_text_size.y)) * 0.7,
        };

        button1_hover := false;
        button1_click := false;
        if CheckCollisionPointRec(GetMousePosition(), Rect2PToRectangle(button1)) {
            button1_hover = true;
            if IsMouseButtonPressed(MOUSE_BUTTON_LEFT) {
                button1_click = true;
            }
        }

        button2_text := "Prototype";
        button2_text_size := MeasureTextEx(font, button2_text, font_size, font_spacing);
        button2 := CutLeft(&top_bar, :float(button2_text_size.x) * 1.5);
        button2 = Shrink(button2, 4);
        button2_text_pos: Vector2 = {
            x = button2.min.x + (GetRectX(button2) - :float(button2_text_size.x)) * 0.5,
            y = button2.min.y + (GetRectY(button2) - :float(button2_text_size.y)) * 0.7,
        };

        button2_hover := false;
        button2_click := false;
        if CheckCollisionPointRec(GetMousePosition(), Rect2PToRectangle(button2)) {
            button2_hover = true;
            if IsMouseButtonPressed(MOUSE_BUTTON_LEFT) {
                button2_click = true;
            }
        }

        if button1_click {
            sandbox_chosen = SANDBOX_TEXT_EDITOR;
        } else if button2_click {
            sandbox_chosen = SANDBOX_PROTOTYPE;
        }

        BeginDrawing();
        ClearBackground(RAYWHITE);

        if sandbox_chosen == SANDBOX_TEXT_EDITOR {
            UpdateTextEditor(screen_rect, font, font_size, font_spacing);
        } else if sandbox_chosen == SANDBOX_PROTOTYPE {
            UpdatePrototype(screen_rect);
        }

        DrawRect(top_bar_original, LIGHTGRAY);

        DrawRectangleRoundedLines(Rect2PToRectangle(button1), 0.3, 12, 2, BLACK);
        if button1_hover DrawRectangleRounded(Rect2PToRectangle(button1), 0.3, 12, WHITE);
        DrawTextEx(font, button1_text, button1_text_pos, font_size, font_spacing, BLACK);

        DrawRectangleRoundedLines(Rect2PToRectangle(button2), 0.3, 12, 2, BLACK);
        if button2_hover DrawRectangleRounded(Rect2PToRectangle(button2), 0.3, 12, WHITE);
        DrawTextEx(font, button2_text, button2_text_pos, font_size, font_spacing, BLACK);


        EndDrawing();
    }
    CloseWindow();
    return 0;
}