import 'dart:io';
import 'dart:ui';
import 'package:flutter_test/flutter_test.dart';
import 'package:syncfusion_flutter_pdf/pdf.dart';

void main() {
  test('Genera Preview Pagina 2 FEA', () async {
    PdfDocument document = PdfDocument();
    PdfPage page = document.pages.add();
    final Size pageSize = page.getClientSize();

    // Draw Header
    final PdfFont titleFont = PdfStandardFont(PdfFontFamily.helvetica, 18, style: PdfFontStyle.bold);
    final PdfFont subTitleFont = PdfStandardFont(PdfFontFamily.helvetica, 10, style: PdfFontStyle.italic);
    final PdfFont boldFont = PdfStandardFont(PdfFontFamily.helvetica, 12, style: PdfFontStyle.bold);
    final PdfFont regularFont = PdfStandardFont(PdfFontFamily.helvetica, 11);

    double y = 0;

    // Title
    String title = "ATTESTATO DI FIRMA ELETTRONICA AVANZATA (FEA)";
    page.graphics.drawString(title, titleFont,
        bounds: Rect.fromLTWH(0, y, pageSize.width, 30),
        format: PdfStringFormat(alignment: PdfTextAlignment.center));
    y += 25;

    String subtitle = "Ai sensi dell'art. 20 del D.Lgs. 7 marzo 2005, n. 82 (CAD) e del DPCM 22 Febbraio 2013.";
    page.graphics.drawString(subtitle, subTitleFont,
        bounds: Rect.fromLTWH(0, y, pageSize.width, 30),
        format: PdfStringFormat(alignment: PdfTextAlignment.center));
    y += 50;

    // Draw separator
    page.graphics.drawLine(PdfPen(PdfColor(100, 100, 100), width: 1), Offset(0, y), Offset(pageSize.width, y));
    y += 20;

    // Split into two columns for Conducente A and B
    double colWidth = (pageSize.width / 2) - 20;

    // Conducente A
    double currentY = y;
    page.graphics.drawString("VEICOLO A (Blu)", boldFont, bounds: Rect.fromLTWH(0, currentY, colWidth, 20), brush: PdfSolidBrush(PdfColor(0, 0, 200)));
    currentY += 25;
    page.graphics.drawString("Conducente: Mario Rossi", regularFont, bounds: Rect.fromLTWH(0, currentY, colWidth, 20));
    currentY += 20;
    page.graphics.drawString("Codice Fiscale: RSSMRA80A01H501U", regularFont, bounds: Rect.fromLTWH(0, currentY, colWidth, 20));
    currentY += 20;
    page.graphics.drawString("Cellulare Verificato: +39 333 1234567", boldFont, bounds: Rect.fromLTWH(0, currentY, colWidth, 20), brush: PdfSolidBrush(PdfColor(0, 128, 0)));
    currentY += 20;
    page.graphics.drawString("Data/Ora Verifica: 28/04/2026 14:35:12", regularFont, bounds: Rect.fromLTWH(0, currentY, colWidth, 20));
    currentY += 20;
    page.graphics.drawString("ID Transazione OTP: fb-otp-849201a", regularFont, bounds: Rect.fromLTWH(0, currentY, colWidth, 20));
    currentY += 30;
    page.graphics.drawString("Firma Elettronica Autografa:", boldFont, bounds: Rect.fromLTWH(0, currentY, colWidth, 20));
    currentY += 20;
    page.graphics.drawRectangle(pen: PdfPen(PdfColor(200, 200, 200)), bounds: Rect.fromLTWH(0, currentY, colWidth, 100));
    page.graphics.drawString("[ Disegno della Firma A ]", subTitleFont, bounds: Rect.fromLTWH(0, currentY + 40, colWidth, 20), format: PdfStringFormat(alignment: PdfTextAlignment.center));

    // Conducente B
    currentY = y;
    page.graphics.drawString("VEICOLO B (Giallo)", boldFont, bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY, colWidth, 20), brush: PdfSolidBrush(PdfColor(200, 150, 0)));
    currentY += 25;
    page.graphics.drawString("Conducente: Luigi Verdi", regularFont, bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY, colWidth, 20));
    currentY += 20;
    page.graphics.drawString("Codice Fiscale: VRDLGI90B02F205Z", regularFont, bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY, colWidth, 20));
    currentY += 20;
    page.graphics.drawString("Cellulare Verificato: +39 340 9876543", boldFont, bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY, colWidth, 20), brush: PdfSolidBrush(PdfColor(0, 128, 0)));
    currentY += 20;
    page.graphics.drawString("Data/Ora Verifica: 28/04/2026 14:36:05", regularFont, bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY, colWidth, 20));
    currentY += 20;
    page.graphics.drawString("ID Transazione OTP: fb-otp-112399b", regularFont, bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY, colWidth, 20));
    currentY += 30;
    page.graphics.drawString("Firma Elettronica Autografa:", boldFont, bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY, colWidth, 20));
    currentY += 20;
    page.graphics.drawRectangle(pen: PdfPen(PdfColor(200, 200, 200)), bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY, colWidth, 100));
    page.graphics.drawString("[ Disegno della Firma B ]", subTitleFont, bounds: Rect.fromLTWH(pageSize.width / 2 + 10, currentY + 40, colWidth, 20), format: PdfStringFormat(alignment: PdfTextAlignment.center));

    y = currentY + 150;

    // Footer Disclaimer
    page.graphics.drawLine(PdfPen(PdfColor(100, 100, 100), width: 1), Offset(0, y), Offset(pageSize.width, y));
    y += 20;
    String disclaimer = "Il presente documento è sigillato crittograficamente in modo inalterabile.\n"
        "Qualsiasi modifica apportata al file dopo l'apposizione del sigillo invaliderà le firme.\n"
        "Le identità dei firmatari sono state verificate tramite associazione univoca "
        "tra il numero di telefono cellulare e il codice OTP inserito sul dispositivo al momento della firma.";
    page.graphics.drawString(disclaimer, subTitleFont, bounds: Rect.fromLTWH(0, y, pageSize.width, 100), format: PdfStringFormat(alignment: PdfTextAlignment.center));

    // Salva il file
    final file = File('/Volumes/NVME-2TB/Sviluppo/development/cid_app/test/pagina_2_fea_preview.pdf');
    file.writeAsBytesSync(await document.save());
    document.dispose();
  });
}
