//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include <System.SysUtils.hpp>
#include <Vcl.Graphics.hpp>
#include <Winapi.Windows.hpp>
#include <Winapi.ShellAPI.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "PDFium"
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
    // Default values
    EditTitle->Text = L"Sample PDF Document";
    EditAuthor->Text = L"PDFium VCL Demo";
    UpdateStatus(L"Ready to create PDF");
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::LinkLabel1LinkClick(TObject *Sender, const UnicodeString Link, TSysLinkType LinkType)
{
    // Open the link in the default browser
    ShellExecute(Handle, L"open", Link.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonCreateClick(TObject *Sender)
{
    try
    {
        UpdateStatus(L"Creating PDF...");
        CreateSimplePDF();
        UpdateStatus(L"PDF created successfully: " + FLastCreatedFile);
    }
    catch (const Exception &E)
    {
        UpdateStatus(L"Error: " + E.Message);
        ShowMessage(L"Error creating PDF: " + E.Message + L"\nCopy the DLL to the system or program directory first.");
    }
}
//---------------------------------------------------------------------------
void TFormMain::CreateSimplePDF()
{
    // Simple color constants used by shapes
    static const System::Uitypes::TColor cLightBlue  = (System::Uitypes::TColor)0x00FFCCAA; // $FFCCAA
    static const System::Uitypes::TColor cLightGreen = (System::Uitypes::TColor)0x0000FF00; // fallback if needed
    (void)cLightBlue; (void)cLightGreen; // suppress unused if options unchecked

    Pdf->CreateDocument();
    Pdf->Active = true;
    try
    {
        // A4 size (PageNumber, Width, Height)
        Pdf->AddPage(0, 595, 842);

        // Title
        Pdf->AddText(EditTitle->Text, L"Arial", 18, 50, 750, clBlack, 0xFF, 0.0);

        // Author
        Pdf->AddText(L"By: " + EditAuthor->Text, L"Arial", 12, 50, 720, clGray, 0xFF, 0.0);

        // Content
        Pdf->AddText(L"This is a simple PDF document created with PDFium VCL.", L"Arial", 12, 50, 680, clBlack, 0xFF, 0.0);
        Pdf->AddText(L"You can add various elements to your PDF:", L"Arial", 12, 50, 650, clBlack, 0xFF, 0.0);

        // Optional features
        if (CheckShapes->Checked)
            AddShapes();
        if (CheckTable->Checked)
            AddTable();
        if (CheckWatermark->Checked)
            AddWatermark();

        // Save
        FLastCreatedFile = L"Sample.pdf";
        Pdf->SaveAs(FLastCreatedFile);
    }
    __finally
    {
        Pdf->Active = false;
    }
}
//---------------------------------------------------------------------------
void TFormMain::AddShapes()
{
    static const System::Uitypes::TColor cLightBlue  = (System::Uitypes::TColor)0x00FFCCAA; // $FFCCAA
    static const System::Uitypes::TColor cLightGreen = (System::Uitypes::TColor)0x00CCFFCC; // $CCFFCC

    // Rectangle
    Pdf->CreatePath(100, 550, 150, 80, Pdfium::fmWinding, cLightBlue, 0x80);
    Pdf->AddPath();
    Pdf->AddText(L"Rectangle", L"Arial", 10, 120, 580, clBlack, 0xFF, 0.0);

    // Simple circle placeholder (using rectangle with equal width/height)
    Pdf->CreatePath(350, 590, 80, 80, Pdfium::fmWinding, cLightGreen, 0x80);
    Pdf->AddPath();
    Pdf->AddText(L"Shape", L"Arial", 10, 370, 580, clBlack, 0xFF, 0.0);
}
//---------------------------------------------------------------------------
void TFormMain::AddTable()
{
    double X = 100;
    double Y = 450;

    // Table header
    Pdf->AddText(L"Item", L"Arial", 10, X, Y, clBlack, 0xFF, 0.0);
    Pdf->AddText(L"Quantity", L"Arial", 10, X + 100, Y, clBlack, 0xFF, 0.0);
    Pdf->AddText(L"Price", L"Arial", 10, X + 200, Y, clBlack, 0xFF, 0.0);

    // Table rows
    for (int I = 1; I <= 3; ++I)
    {
        Y -= 20;
        Pdf->AddText(L"Item " + IntToStr(I), L"Arial", 9, X, Y, clBlack, 0xFF, 0.0);
        Pdf->AddText(IntToStr(I), L"Arial", 9, X + 100, Y, clBlack, 0xFF, 0.0);
        Pdf->AddText(L"$" + IntToStr(I * 10) + L".00", L"Arial", 9, X + 200, Y, clBlack, 0xFF, 0.0);
    }
}
//---------------------------------------------------------------------------
void TFormMain::AddWatermark()
{
    Pdf->AddText(L"SAMPLE", L"Arial", 72, 200, 200, clGray, 0x30, 45.0);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonOpenClick(TObject *Sender)
{
    if (!FLastCreatedFile.IsEmpty())
    {
        if (FileExists(FLastCreatedFile))
            ShellExecute(0, L"OPEN", FLastCreatedFile.c_str(), L"", L"", SW_SHOWNORMAL);
        else
            ShowMessage(L"File not found: " + FLastCreatedFile);
    }
    else
    {
        ShowMessage(L"No PDF file created yet. Click \"Create PDF\" first.");
    }
}
//---------------------------------------------------------------------------
void TFormMain::UpdateStatus(const UnicodeString Status)
{
    LabelStatus->Caption = Status;
    Application->ProcessMessages();
}
//---------------------------------------------------------------------------
