// First steps with the Graphics library.
// Creates a 400x300 canvas, draws a few shapes and some text,
// then saves the result to hello_graphics.png.

import { Graphics } from library("libs/Graphics", "1.0");
import { QRCode }   from library("QRCode",        "1.0");

fn hot filter(x:int):int
{
    if(x == 0xFF000000)
        return 0xFFFF0000;

    return x;
}

fn Main() {

    // --- Create a canvas (width, height) ---
    let g = new Graphics(400, 300);

    // --- Fill background ---
    g.Clear(0xFF1A1A2E);          // dark navy

    // --- Draw a filled circle ---
    g.SetFill(0xFFE94560);        // red-pink
    g.FillCircle(200, 130, 60);

    // --- Draw an outline circle on top ---
    g.SetStroke(0xFFFFFFFF);      // white
    g.SetThickness(2);
    g.Circle(200, 130, 60);

    // --- Draw a filled rectangle ---
    g.SetFill(0xFF0F3460);        // dark blue
    g.FillRect(30, 200, 160, 60);

    // --- Outline the same rectangle ---
    g.SetStroke(0xFF16213E);
    g.SetThickness(1);
    g.Rect(30, 200, 160, 60);

    // --- Draw a triangle (polygon outline) ---
    g.SetStroke(0xFFFFD700);      // gold
    g.SetThickness(2);
    let tri = [320, 200,  380, 280,  260, 280];
    g.Polygon(tri);

    // --- Draw some text ---
    g.SetStroke(0xFFFFFFFF);
    g.DrawText(30, 30, "Hello, Graphics!");

    // Larger text using DrawTextEx(x, y, text, scale)
    g.SetStroke(0xFFE94560);
    g.DrawTextEx(30, 50, "Flaris", 2);

    // QR code via pure-Flaris library → blit onto canvas
    let qrPx   = QRCode.ToPixels("https://example.com", 3, nil, true);
    let qrSide = QRCode.PixelSize("https://example.com", 3, nil);
    g.Blit(300, 0, qrSide, qrSide, qrPx);
    g.Filter( filter, 300, 0, 100, 100);
    g.Grayscale(0, 0, 100, 100);

    // --- Draw scaled logo in lower-left corner ---
    let logoSize = 60;
    let margin   = 10;
    g.DrawImage("flaris.png", margin, 300 - margin - logoSize, logoSize, logoSize);

    // --- Save to file ---
    let ok = g.Save("./hello_graphics.png");
    if (ok != nil) {
        Console.WriteLine("Saved to hello_graphics.png");
    } else {
        Console.WriteLine("Save failed");
    }

    return;
}
