<?php

namespace App\Providers\ProvidersDistribution\ProvidersDistributionList;

class ProvidersDistributionListCells
{
    private array $toursList;
    private array $countriesList;

    public function __construct(array $toursList = [], array $countriesList = [])
    {
        $this->toursList = $toursList;
        $this->countriesList = $countriesList;
    }

    private static array $baseActions = [
        'wrap' => ['action' => 'wrap'],
        'nowrap' => ['action' => 'nowrap'],
        'center' => ['action' => 'center'],
        'left' => ['action' => 'left'],
        'border' => ['action' => 'border'],
        'bold' => ['action' => 'bold'],
        'print' => ['action' => 'print'],
        'width' => ['action' => 'width'],
        'mergeborder' => ['action' => 'mergeborder'],
        'vmerge' => ['action' => 'vmerge'],
    ];

    public function tableCellCenter($text, $bgColor = null, $width = 15, $fontColor = null, $totalColumns = null, $rowSpan = null, $bold = false): array
    {
        $cell = [];

        if ($bgColor) $cell[] = ['action' => 'background', 'color' => $bgColor];
        if ($fontColor) $cell[] = ['action' => 'color', 'color' => $fontColor];

//        $cell[] = self::$baseActions['center'];
        $cell[] = self::$baseActions['border'];
        $cell[] = ['action' => 'print', 'text' => $text];
        $cell[] = ['action' => 'width', 'width' => $width];

        if ($totalColumns) $cell[] = ['action' => 'mergeborder', 'lines' => $totalColumns];

        if ($rowSpan) $cell[] = ['action' => 'vmerge', 'lines' => $rowSpan];

        return $cell;
    }

    public function tableCellCenterTotals($text, $bgColor = null, $width = 15, $fontColor = null, $totalColumns = null, $rowSpan = null, $bold = false): array
    {
        $cell = [];

        $cell[] = self::$baseActions['center'];
        $cell[] = self::$baseActions['border'];
        $cell[] = ['action' => 'print', 'text' => $text];
//        $cell[] = ['action' => 'width', 'width' => $width];

        return $cell;
    }

    public function tableCellCenterEmpty($text): array
    {
        $cell = [];
        $cell[] = self::$baseActions['border'];
        $cell[] = ['action' => 'print', 'text' => $text];

        return $cell;
    }

    public function tableCellLeft(
        $text,
        $bgColor = null,
        $width = 15,
        $fontColor = null,
        $totalColumns = null
    ): array
    {
        $cell = [];

        $cell[] = self::$baseActions['nowrap'];

        if ($bgColor) {
            $cell[] = ['action' => 'background', 'color' => $bgColor];
        }

        if ($fontColor) {
            $cell[] = ['action' => 'color', 'color' => $fontColor];
        }

        $cell[] = self::$baseActions['left'];
        $cell[] = self::$baseActions['border'];
        $cell[] = ['action' => 'print', 'text' => $text];
        $cell[] = ['action' => 'width', 'width' => $width];

        if ($totalColumns) {
            $cell[] = ['action' => 'mergeborder', 'lines' => $totalColumns];
        }

        return $cell;
    }

    public function getAllRows($groupedData)
    {
        $rows = [];
        $colSpan = count($this->toursList) + count($this->countriesList) + 2;
        foreach ($groupedData as $group) {

            $typeName = $group['TypeProvider_Name'] ?? 'DESCONOCIDO';

            $typeRow = [];
            $typeRow[] = [
                ['action' => 'background', 'color' => 'd9d9d9'],
                ['action' => 'print', 'text' => $typeName],
                ['action' => 'mergeborder', 'lines' => $colSpan],
            ];

            $rows[] = $typeRow;

            foreach ($group['Providers'] as $provider) {
                $rows[] = $this->getRow($provider);
            }
        }

        return $rows;
    }

    private function getRow($provider)
    {
        $row = [];

        $row[] = $this->tableCellLeft($provider['Provider_BusinessName']);

        foreach ($provider['Tours'] as $tour) {
            if($tour['Count'] == ''){
                $row[] = $this->tableCellCenterEmpty($tour['Count']);
            }else{
                $row[] = $this->tableCellCenter($tour['Count'], null, 5);
            }
        }

        $row[] = $this->tableCellCenterTotals($provider['Total'], 'bdd6ee', 12);

        foreach ($provider['Countries'] as $country) {
            if($country['Count'] == ''){
                $row[] = $this->tableCellCenterEmpty($country['Count']);
            } else{
                $row[] = $this->tableCellCenter($country['Count'], null, 5);
            }
        }

        $row[] = $this->tableCellCenterTotals($provider['Total_Countries'], 'bdd6ee', 12);

        return $row;
    }

    private function mixWithWhite($hex, $percent = 0.8)
    {
        $hex = str_replace('#', '', $hex);

        $r = hexdec(substr($hex, 0, 2));
        $g = hexdec(substr($hex, 2, 2));
        $b = hexdec(substr($hex, 4, 2));

        $r = (int)($r * (1 - $percent) + 255 * $percent);
        $g = (int)($g * (1 - $percent) + 255 * $percent);
        $b = (int)($b * (1 - $percent) + 255 * $percent);

        return sprintf("FF%02X%02X%02X", $r, $g, $b);
    }

    public function generateHeader($toursList, $countriesList)
    {
        $header = [];

        $rowTop = [];

        $rowTop[] = $this->tableCellCenter('GUIA', 'd9d9d9', 25, null, null, null, true);

        $rowTop[] = $this->tableCellCenter(
            'TOURS',
            'd9d9d9',
            15,
            null,
            count($toursList),
            null,
            true
        );

        $rowTop[] = $this->tableCellCenter(
            'PAISES',
            'd9d9d9',
            15,
            null,
            count($countriesList),
            null,
            true
        );

        $row = [];

        $row[] = $this->tableCellCenter('Proveedor', 'd9d9d9', 25, null, null, null, true);

        foreach ($toursList as $tour) {
            $color = $this->mixWithWhite($tour['Tour_Color'], 0.4);
            $row[] = [
                ['action' => 'wrap'],
                ['action' => 'background', 'color' => $color],
                ['action' => 'rotate', 'angle' => 90],
                ['action' => 'height', 'height' => 100],
                ['action' => 'print', 'text' => $tour['Tour_Code']],
            ];
        }

        $row[] = $this->tableCellCenter('Total', 'bdd6ee', 12, null, null, null, true);

        foreach ($countriesList as $country) {
            $row[] = [
                ['action' => 'wrap'],
                ['action' => 'rotate', 'angle' => 90],
                ['action' => 'print', 'text' => $country['Name']],
            ];
        }

        $row[] = $this->tableCellCenter('Total', 'bdd6ee', 12, null, null, null, true);

        $header[] = $rowTop;
        $header[] = $row;

        return $header;
    }

    public function run($groupedData): array
    {
        if (empty($groupedData)) {
            return [];
        }

        $groupedData = json_decode(json_encode($groupedData), true);
        $headerRows = $this->generateHeader($this->toursList, $this->countriesList);

        $rows = $this->getAllRows($groupedData);

        return array_merge($headerRows, $rows);
    }

}
