<?php

namespace MenulisAi\Pdfgen\Document;

use PhpOffice\PhpWord\Element\Table as ElementTable;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\SimpleType\Border;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\JcTable;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\SimpleType\TextAlignment;
use PhpOffice\PhpWord\Style\Table as StyleTable;

const PER_INCH = 1440;

class Table extends Document implements Documentable
{
    public function __construct(string $jsonPath)
    {
        // $this->openFile($jsonPath);
    }

    public function templateSource(): string
    {
        return appPath("Template/Doc1.docx");
    }

    public function compile(): string
    {
        /**
         * 1 Inch = 1440 point for TWIP / dxa
         * A4 Paper (W x H) = 8.27 x 11.69 = 11908.9 x 16833.6
         * Margin Left = 0.44" = 633.6
         * Margin Right = 0.44" = 633.6
         * Margin Top = 0.79" = 1137.6
         * Margin Bottom = 0.79" = 1137.6
         */

        $proc = new TemplateProcessor($this->templateSource());

        $proc->setValue("name", "Pande Ganteng");
        $proc->setValue("full_name", "Pande Ganteng");
        $proc->setValue("email", "Pande@gmail.com");

        $tableWidth = (8.27 * PER_INCH) - ((0.44 * 2) * PER_INCH);
        $col1Width = $tableWidth * 0.4;
        $col2Width = $tableWidth * 0.15;
        $col3Width = $tableWidth * 0.15;
        $col4Width = $tableWidth * 0.15;
        $col5Width = $tableWidth * 0.15;

        $table = new ElementTable([
            'alignment' => JcTable::CENTER,
            'borderColor' => '#072442',
            'borderSize' => 10,
            'width' => $tableWidth,
            'unit' => TblWidth::TWIP,
            'layout' => StyleTable::LAYOUT_AUTO,
            'cellMarginTop' => 100,
            'cellMarginLeft' => 100,
            'cellMarginBottom' => 0,
            'cellMarginRight' => 100,
        ]);
        $table->addRow(800);
        $table->addCell($col1Width, ['valign' => 'center', 'bgColor' => '#4169E1'])->addText('Kategori', ['bold' => true, 'name' => 'Visby Round CF', 'size' => 10, 'color' => '#ffffff'], ['alignment' => Jc::CENTER]);
        $table->addCell($col2Width, ['valign' => 'center', 'bgColor' => '#4169E1'])->addText('Jawaban Benar', ['bold' => true, 'name' => 'Visby Round CF', 'size' => 10, 'color' => '#ffffff'], ['alignment' => Jc::CENTER]);
        $table->addCell($col3Width, ['valign' => 'center', 'bgColor' => '#4169E1'])->addText('Jawaban Salah', ['bold' => true, 'name' => 'Visby Round CF', 'size' => 10, 'color' => '#ffffff'], ['alignment' => Jc::CENTER]);
        $table->addCell($col4Width, ['valign' => 'center', 'bgColor' => '#4169E1'])->addText('Tidak Menjawab', ['bold' => true, 'name' => 'Visby Round CF', 'size' => 10, 'color' => '#ffffff'], ['alignment' => Jc::CENTER]);
        $table->addCell($col5Width, ['valign' => 'center', 'bgColor' => '#4169E1'])->addText('% Jawaban Benar', ['bold' => true, 'name' => 'Visby Round CF', 'size' => 10, 'color' => '#ffffff'], ['alignment' => Jc::CENTER]);

        // TWK
        $table->addRow();
        $table->addCell($col1Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderBottomSize' => 0, 'borderBottomStyle' => Border::NONE])->addText("Tes Wawasan Kebangsaan", ['bold' => true, 'name' => 'Visby Round CF', 'size' => 9, 'color' => '#072442']);
        $table->addCell($col2Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderBottomSize' => 0, 'borderBottomStyle' => Border::NONE])->addText("17", ['bold' => true, 'name' => 'Visby Round CF', 'size' => 9, 'color' => '#072442'], ['alignment' => Jc::CENTER]);
        $table->addCell($col3Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderBottomSize' => 0, 'borderBottomStyle' => Border::NONE])->addText("13", ['bold' => true, 'name' => 'Visby Round CF', 'size' => 9, 'color' => '#072442'], ['alignment' => Jc::CENTER]);
        $table->addCell($col4Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderBottomSize' => 0, 'borderBottomStyle' => Border::NONE])->addText("0", ['bold' => true, 'name' => 'Visby Round CF', 'size' => 9, 'color' => '#072442'], ['alignment' => Jc::CENTER]);
        $table->addCell($col5Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderBottomSize' => 0, 'borderBottomStyle' => Border::NONE])->addText("56%", ['bold' => true, 'name' => 'Visby Round CF', 'size' => 9, 'color' => '#072442'], ['alignment' => Jc::CENTER]);

        $dataTwk = ['Nasionalisme', 'Integritas', 'Bela Negara', 'Pilar Negara', 'Bahasa Indonesia', 'Bahasa Bali'];
        foreach ($dataTwk as $key => $dt) {
            $isLast = false;
            if (count($dataTwk) == $key + 1) {
                $isLast = true;
            }

            $table->addRow();
            $table->addCell($col1Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderTopSize' => 0, 'borderBottomSize' => $isLast ? 10 : 0, 'borderTopStyle' => Border::NONE, 'borderBottomStyle' => $isLast ? Border::SINGLE : Border::NONE])->addText("    " . mb_chr(8226, 'UTF-8') . " " . $dt, ['bold' => false, 'name' => 'Visby Round CF', 'size' => 8, 'color' => '#072442']);
            $table->addCell($col2Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderTopSize' => 0, 'borderBottomSize' => $isLast ? 10 : 0, 'borderTopStyle' => Border::NONE, 'borderBottomStyle' => $isLast ? Border::SINGLE : Border::NONE])->addText("17", ['bold' => false, 'name' => 'Visby Round CF', 'size' => 8, 'color' => '#072442'], ['alignment' => Jc::CENTER]);
            $table->addCell($col3Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderTopSize' => 0, 'borderBottomSize' => $isLast ? 10 : 0, 'borderTopStyle' => Border::NONE, 'borderBottomStyle' => $isLast ? Border::SINGLE : Border::NONE])->addText("13", ['bold' => false, 'name' => 'Visby Round CF', 'size' => 8, 'color' => '#072442'], ['alignment' => Jc::CENTER]);
            $table->addCell($col4Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderTopSize' => 0, 'borderBottomSize' => $isLast ? 10 : 0, 'borderTopStyle' => Border::NONE, 'borderBottomStyle' => $isLast ? Border::SINGLE : Border::NONE])->addText("0", ['bold' => false, 'name' => 'Visby Round CF', 'size' => 8, 'color' => '#072442'], ['alignment' => Jc::CENTER]);
            $table->addCell($col5Width, ['valign' => 'center', 'bgColor' => '#f0f3fd', 'borderTopSize' => 0, 'borderBottomSize' => $isLast ? 10 : 0, 'borderTopStyle' => Border::NONE, 'borderBottomStyle' => $isLast ? Border::SINGLE : Border::NONE])->addText("56%", ['bold' => false, 'name' => 'Visby Round CF', 'size' => 8, 'color' => '#072442'], ['alignment' => Jc::CENTER]);
        }

        $dataTkp = ['Verbal Analogi'];

        $proc->setComplexValue('table', $table);

        $proc->saveAs(rootPath("examples/Raport.docx"));

        return rootPath("examples/Raport.docx");
    }
}
