#include "hbclass.ch"

#require "hbhpdf"
#require "hbct"

PROCEDURE Main()

   LOCAL cOutput := hb_DirBase() + "test_render_pdf_output.pdf"
   LOCAL cLog := hb_DirBase() + "test_render_pdf_result.log"
   LOCAL aPages := {}
   LOCAL hIR
   LOCAL oRender
   LOCAL nPage
   LOCAL nPages := iif( Lower( AllTrim( hb_ArgV( 1 ) ) ) == "large", 1000, 3 )
   LOCAL aObjects
   LOCAL nProgress := 0

   FOR nPage := 1 TO nPages
      aObjects := { ;
         TestRectangle(), ;
         TestEllipse(), ;
         TestLine(), ;
         TestText( nPage ), ;
         TestRotatedText(), ;
         TestMultiline(), ;
         TestBarcode(), ;
         TestMatrixBarcode(), ;
         TestRichText() }
      aObjects := AddTableObjects( aObjects, nPage )
      IF nPage == 1
         AAdd( aObjects, TestImage( "lst-antiguos\LST-CLIENTES\cimpra\rrss_cimpra.png", "image/png", 225, 10 ) )
         AAdd( aObjects, TestImage( "public\motor\tests\fixtures\logo_dominus.jpg", "image/jpeg", 225, 52 ) )
         AAdd( aObjects, TestImage( "public\motor\tests\fixtures\logo.png", "image/png", 225, 94 ) )
      ENDIF
      AAdd( aPages, { "objetos" => aObjects } )
   NEXT

   hIR := { ;
      "nAncho" => 297.0, ;
      "nAlto" => 210.0, ;
      "paginas" => aPages }

   oRender := TRenderPDF():New()
   oRender:SetProgressCallback( {| nCurrent, nTotal, nDataCurrent, nDataTotal | nProgress := nCurrent, .T. } )
   IF ! oRender:Render( hIR, cOutput )
      QOut( "ERROR:", oRender:GetLastError() )
      hb_MemoWrit( cLog, "ERROR: " + oRender:GetLastError() )
      ErrorLevel( 1 )
   ELSEIF ! File( cOutput ) .OR. nProgress != nPages
      QOut( "ERROR: PDF ausente o progreso incompleto" )
      hb_MemoWrit( cLog, "ERROR: PDF/progreso: " + cOutput + ", progreso=" + hb_NToS( nProgress ) )
      ErrorLevel( 2 )
   ELSE
      QOut( "OK:", cOutput )
      QOut( "Avisos:", Len( oRender:GetAvisos() ) )
      hb_MemoWrit( cLog, "OK: paginas=" + hb_NToS( nPages ) + ", avisos=" + hb_NToS( Len( oRender:GetAvisos() ) ) )
      ErrorLevel( 0 )
   ENDIF
   oRender:End()
   oRender := NIL

RETURN

STATIC FUNCTION TestBase( cType, nX, nY, nWidth, nHeight )
RETURN { ;
   "tipo" => cType, "x" => nX, "y" => nY, "ancho" => nWidth, "alto" => nHeight, ;
   "colorFondo" => "transparent", "colorBorde" => "#000000", "grosorBorde" => 0.25, ;
   "bordeSuperior" => .F., "bordeDerecho" => .F., "bordeInferior" => .F., "bordeIzquierdo" => .F., ;
   "paddingSuperior" => 0, "paddingDerecho" => 0, "paddingInferior" => 0, "paddingIzquierdo" => 0, ;
   "opacidad" => 1, "rotacion" => 0 }

STATIC FUNCTION TestRectangle()
   LOCAL h := TestBase( "rectangulo", 10, 10, 70, 28 )
   h[ "colorFondo" ] := "#e8f2ff"
   h[ "bordeSuperior" ] := .T.
   h[ "bordeDerecho" ] := .T.
   h[ "bordeInferior" ] := .T.
   h[ "bordeIzquierdo" ] := .T.
RETURN h


STATIC FUNCTION TestEllipse()
   LOCAL h := TestBase( "elipse", 90, 10, 38, 28 )
   h[ "colorFondo" ] := "#fff1b8"
   h[ "bordeSuperior" ] := .T.
RETURN h

STATIC FUNCTION TestLine()
   LOCAL h := TestBase( "linea", 10, 45, 118, 0 )
   h[ "color" ] := "#1e40af"
   h[ "grosor" ] := 0.4
RETURN h

STATIC FUNCTION TestText( nPage )
   LOCAL h := TestBase( "texto", 10, 50, 180, 16 )
   h[ "texto" ] := "Página " + hb_NToS( nPage ) + ": áéíóú ÁÉÍÓÚ ñ Ñ º ª €"
   h[ "fuente" ] := "Arial"
   h[ "tamano" ] := 16
   h[ "negrita" ] := .T.
   h[ "color" ] := "#9f1239"
   h[ "alineacion" ] := "center"
   h[ "alineacionVertical" ] := "center"
   h[ "ajusteLinea" ] := .F.
RETURN h

STATIC FUNCTION TestMultiline()
   LOCAL h := TestBase( "texto", 10, 70, 180, 30 )
   h[ "texto" ] := "Texto alineado y multilínea." + Chr( 10 ) + "Segunda línea ajustada dentro del rectángulo."
   h[ "fuente" ] := "Arial"
   h[ "tamano" ] := 11
   h[ "color" ] := "#111827"
   h[ "alineacion" ] := "right"
   h[ "alineacionVertical" ] := "center"
   h[ "ajusteLinea" ] := .T.
RETURN h

STATIC FUNCTION TestRotatedText()
   LOCAL h := TestBase( "texto", 4, 108, 88, 6 )
   h[ "texto" ] := "Texto vertical fuera de la tabla"
   h[ "fuente" ] := "Arial"
   h[ "tamano" ] := 8
   h[ "color" ] := "#111827"
   h[ "alineacion" ] := "left"
   h[ "alineacionVertical" ] := "top"
   h[ "ajusteLinea" ] := .F.
   h[ "rotacion" ] := 90
   h[ "paddingSuperior" ] := 0.4
   h[ "paddingDerecho" ] := 0.2
   h[ "paddingInferior" ] := 0.1
   h[ "paddingIzquierdo" ] := 1.3
RETURN h

STATIC FUNCTION TestBarcode()
   LOCAL h := TestBase( "barcode", 10, 110, 100, 28 )
   h[ "bits" ] := "1101001000010100011000100010110001000100011011101100011101011"
   h[ "mostrarTexto" ] := .T.
   h[ "texto" ] := "1234567890"
RETURN h

STATIC FUNCTION TestRichText()
   LOCAL h := TestBase( "texto_enriquecido", 120, 110, 100, 28 )
   h[ "plainContent" ] := "RichText: texto plano controlado"
   h[ "hasVisibleContent" ] := .T.
   h[ "alineacionVertical" ] := "center"
RETURN h

STATIC FUNCTION TestMatrixBarcode()

   LOCAL h := TestBase( "barcode", 230, 145, 30, 30 )

   h[ "matriz" ] := { ;
      "1111111010101111111", "1000001011101000001", "1011101010101011101", ;
      "1011101000101011101", "1011101011101011101", "1000001000101000001", ;
      "1111111010101111111", "0000000011100000000", "1101011110111010110", ;
      "0010110011010011010", "1111001110111100111", "0100110001000110010", ;
      "1011011110111011101", "0000000011010010010", "1111111010111110101", ;
      "1000001011000011010", "1011101010111010111", "1011101001010110010", ;
      "1011101011111101111", "1000001001000100010", "1111111011111111101" }
   h[ "margenModulos" ] := 1

RETURN h

STATIC FUNCTION TestImage( cRelativeFile, cMime, nX, nY )

   LOCAL h := TestBase( "imagen", nX, nY, 55, 32 )
   LOCAL cFile := hb_DirBase() + "..\..\..\" + cRelativeFile

   IF File( cFile )
      h[ "dataUri" ] := "data:" + cMime + ";base64," + hb_Base64Encode( hb_MemoRead( cFile ) )
   ELSE
      h[ "dataUri" ] := ""
   ENDIF
   h[ "mime" ] := cMime
   h[ "ajuste" ] := "contain"
   h[ "mantenerProporcion" ] := .T.

RETURN h

STATIC FUNCTION AddTableObjects( aObjects, nPage )

   LOCAL h
   LOCAL nRow

   h := TestBase( "rectangulo", 10, 150, 210, 35 )
   h[ "bordeSuperior" ] := .T.
   h[ "bordeDerecho" ] := .T.
   h[ "bordeInferior" ] := .T.
   h[ "bordeIzquierdo" ] := .T.
   AAdd( aObjects, h )
   FOR nRow := 1 TO 3
      h := TestBase( "texto", 12, 151 + ( nRow - 1 ) * 10, 200, 8 )
      h[ "texto" ] := "Tabla multipágina " + hb_NToS( nPage ) + " - fila " + hb_NToS( nRow )
      h[ "fuente" ] := "Arial"
      h[ "tamano" ] := 9
      h[ "alineacionVertical" ] := "center"
      h[ "ajusteLinea" ] := .F.
      AAdd( aObjects, h )
      IF nRow < 3
         h := TestBase( "linea", 10, 150 + nRow * 10, 210, 0 )
         h[ "grosor" ] := 0.15
         AAdd( aObjects, h )
      ENDIF
   NEXT

RETURN aObjects
