import unittest
from datetime import datetime
from pathlib import Path

from borb.io.read.types import Decimal
from borb.pdf.canvas.color.color import HexColor
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf.canvas.layout.layout_element import Alignment
from borb.pdf.canvas.layout.page_layout.multi_column_layout import SingleColumnLayout
from borb.pdf.canvas.layout.table.fixed_column_width_table import (
    FixedColumnWidthTable as Table,
)
from borb.pdf.canvas.layout.text.paragraph import Paragraph
from borb.pdf.document import Document
from borb.pdf.page.page import Page
from borb.pdf.pdf import PDF


class TestWriteParagraphAlignment(unittest.TestCase):
    """
    This test creates a PDF with a Paragraph object in it.
    The Paragraph is aligned in all possible combinations of horizontal and vertical layout.
    """

    def __init__(self, methodName="runTest"):
        super().__init__(methodName)
        # find output dir
        p: Path = Path(__file__).parent
        while "output" not in [x.stem for x in p.iterdir() if x.is_dir()]:
            p = p.parent
        p = p / "output"
        self.output_dir = Path(p, Path(__file__).stem.replace(".py", ""))
        if not self.output_dir.exists():
            self.output_dir.mkdir()

    def test_write_document(self):

        # create output directory if it does not exist yet
        if not self.output_dir.exists():
            self.output_dir.mkdir()

        # create document
        pdf = Document()

        # add test information
        page = Page()
        pdf.append_page(page)
        layout = SingleColumnLayout(page)
        layout.add(
            Table(number_of_columns=2, number_of_rows=3)
            .add(Paragraph("Date", font="Helvetica-Bold"))
            .add(Paragraph(datetime.now().strftime("%d/%m/%Y, %H:%M:%S")))
            .add(Paragraph("Test", font="Helvetica-Bold"))
            .add(Paragraph(Path(__file__).stem))
            .add(Paragraph("Description", font="Helvetica-Bold"))
            .add(
                Paragraph(
                    "This test creates a PDF with a Paragraph object in it. "
                    "The Paragraph is aligned in all possible combinations of horizontal and vertical layout. "
                    "The dark green box is the bounding box given to the layout algorithm. The actual space taken up by the paragraph is marked in light green."
                )
            )
            .set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2))
        )

        bb = Rectangle(Decimal(59), Decimal(500), Decimal(476), Decimal(124))
        page_number: int = 1
        for va in [Alignment.TOP, Alignment.MIDDLE, Alignment.BOTTOM]:
            for ha in [Alignment.LEFT, Alignment.CENTERED, Alignment.RIGHT]:
                for ta in [
                    Alignment.LEFT,
                    Alignment.CENTERED,
                    Alignment.RIGHT,
                    Alignment.JUSTIFIED,
                ]:
                    print("building example %d / 36" % page_number)
                    page_number += 1
                    page = Page()
                    pdf.append_page(page)
                    Paragraph(
                        str(va)
                        + " "
                        + str(ha)
                        + " "
                        + str(ta)
                        + """
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
                        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
                        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
                        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.                    
                        """,
                        font_size=Decimal(10),
                        vertical_alignment=va,
                        horizontal_alignment=ha,
                        text_alignment=ta,
                        padding_top=Decimal(5),
                        padding_right=Decimal(5),
                        padding_bottom=Decimal(5),
                        padding_left=Decimal(5),
                        background_color=HexColor("56cbf9"),
                    ).layout(
                        page,
                        bb,
                    )

                    # add rectangle annotation
                    page.append_square_annotation(
                        stroke_color=HexColor("f1cd2e"),
                        rectangle=bb,
                    )

        # determine output location
        out_file = self.output_dir / "output.pdf"

        # attempt to store PDF
        with open(out_file, "wb") as in_file_handle:
            PDF.dumps(in_file_handle, pdf)
