<?php

namespace App\Repository;


use App\Entity\Catalogue;
use App\Entity\CatalogueDetail;
use App\Entity\Question;
use App\Entity\QuestionType;
use App\Entity\User;
use App\Entity\UserWorksheet;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Dompdf\Dompdf;
use Dompdf\Options;
use Twig\Environment;


class UserRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, User::class);
    }

    public static function setClientData(User $user, $clientData)
    {
        $user->setFirstname($clientData->firstname);
        $user->setLastname($clientData->lastname);
        $user->setPhoneNo($clientData->phoneNo);
        $user->setMaxWorksheets($clientData->maxWorksheets);
        $user->setCompany($clientData->company);
        $user->setDepartment($clientData->department);
        $user->setOccupation($clientData->occupation);
        $user->setIndustry($clientData->industry);
        $user->setOrganisationUnit($clientData->organisationUnit);
        $user->setInvoiceRecipient($clientData->invoiceRecipient);
        $user->setInvoiceDepartment($clientData->invoiceDepartment);
        $user->setInvoiceStreet($clientData->invoiceStreet);
        $user->setInvoiceZip($clientData->invoiceZip);
        $user->setInvoiceCity($clientData->invoiceCity);
        $user->setOrderNo($clientData->orderNo);
        $user->setCostCenter($clientData->costCenter);
        $user->setActive($clientData->active);
        $user->setValidUntilDate($clientData->validUntilDate !== '' ? \DateTime::createFromFormat('Y-m-d', $clientData->validUntilDate) : null);
        return $user;
    }

    /**
     * Renders user catalogue
     */
    public static function renderWorksheetPDF(Environment $twig, UserWorksheet $worksheet)
    {
        // Configure Dompdf according to your needs
        $pdfOptions = new Options();
        $pdfOptions->set('defaultFont', 'Arial');
        $pdfOptions->set('isRemoteEnabled', true);

        // Instantiate Dompdf with our options
        $dompdf = new Dompdf($pdfOptions);
        $dompdf->setPaper('A4', 'portrait');

        // Retrieve the HTML generated in our twig file
        $html = $twig->render('pdf/pdf.html.twig',
            [
                'worksheet' => $worksheet,
                'sortedWorksheetTasks' => UserRepository::getSortedWorksheetTasks($worksheet),
                'isPdf' => true
            ]
        );
        
        // Load HTML to Dompdf
        $dompdf->loadHtml($html);

        // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
        $dompdf->setPaper('A4', 'portrait');

        // Render the HTML as PDF
        $dompdf->render();

        $x          = 505;
        $y          = 790;
        $text       = "{PAGE_NUM} von {PAGE_COUNT}";
        $font       = $dompdf->getFontMetrics()->getFont('Helvetica', 'normal');
        $size       = 10;
        $color      = array(0,0,0);
        $word_space = 0.0;
        $char_space = 0.0;
        $angle      = 0.0;

        $dompdf->getCanvas()->page_text(
            $x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle
        );

        return $dompdf;
    }

    public static function getSortedWorksheetTasks(UserWorksheet $worksheet)
    {
        $sortedWorksheetTasks = $worksheet->getUserWorkSheetTasks()->toArray();

        usort($sortedWorksheetTasks, function($a, $b) {
            $delimComparison = strcmp($a->getDelimitation(), $b->getDelimitation());

            if ($delimComparison === 0) {
                return strcmp($a->getTask(), $b->getTask());
            }

            return $delimComparison;
        });
        return $sortedWorksheetTasks;
    }
}
