<?php

namespace App\Providers\Leads\LeadListReport;

use App\Http\Helpers\DateTime;
use App\Modules\Shared\Domain\ArrayUtils;
use App\Modules\Shared\Domain\DateUtils;
use App\Providers\ExcelReport\Utils;

class LeadListCells
{
    private function tableTitle($data)
    {
        $fnPrint = Utils::print($data[0]);
        $fnWidth = Utils::width($data[1]);
        return Utils::wrap(Utils::top(Utils::border(Utils::bold(Utils::center($fnPrint($fnWidth(Utils::cell())))))));
    }

    private function tableCellLeft($text)
    {
        $fnPrint = Utils::print($text);
        return Utils::wrap(Utils::top(Utils::border(Utils::left($fnPrint(Utils::cell())))));
    }

    private function tableCellCenter($text)
    {
        $fnPrint = Utils::print($text);
        return Utils::wrap(Utils::top(Utils::border(Utils::center($fnPrint(Utils::cell())))));
    }
    private function tableCellRight($text)
    {
        $fnPrint = Utils::print($text);
        return Utils::wrap(Utils::top(Utils::border(Utils::right($fnPrint(Utils::cell())))));
    }

    private function getAllRows($data, $length)
    {
        return array_map(fn($b, $index) => $this->getRow($b, $index, $length), $data, array_keys($data));
    }

    private function getRow($element, $i, $length)
    {
        $table = [
            $this->tableCellCenter($i + 1),
            $this->tableCellLeft(isset($element->Id_Admin_Assigner) ? $element->Admin_Assigner_Name : ''),
            $this->tableCellLeft(isset($element->Id_Admin) ? $element->Admin_Name : ''),
            $this->tableCellLeft($element->WebContactMessage_Code),
            $this->tableCellLeft(isset($element->Id_WebChannel) ? $element->WebChannel_Name : ''),
            $this->tableCellLeft(isset($element->Id_Language) ? $element->Id_Language : ''),
            $this->tableCellLeft($element->WebContactMessage_Name . ' ' . $element->WebContactMessage_LastName),
            $this->tableCellLeft(isset($element->Id_UserCountry) ? $element->UserCountry_Name : ''),
            $this->tableCellLeft($element->WebContactMessage_Email),
            $this->tableCellLeft($element->WebContactMessage_Message),
            $this->tableCellLeft($element->WebContactMessage_Adults),
            $this->tableCellLeft($element->WebContactMessage_Children),
            $this->tableCellLeft(DateTime::dateTimeTransform($element->WebContactMessage_DateFirstContact)),
            $this->tableCellLeft(DateTime::dateTimeTransform($element->WebContactMessage_DateAssign)),
            $this->tableCellLeft($this->diferenceBetweenDates($element->WebContactMessage_DateFirstContact, $element->WebContactMessage_DateAssign)),
            $this->tableCellLeft(DateTime::dateTimeTransform($element->WebContactMessage_DateFirstAnswer)),
            $this->tableCellLeft($this->diferenceBetweenDates($element->WebContactMessage_DateFirstContact, $element->WebContactMessage_DateAssign)),
            $this->tableCellLeft(isset($element->Id_Booking) ? DateTime::dateTimeTransform($element->Booking_Date) : ''),
            $this->tableCellLeft($this->diferenceBetweenDates($element->WebContactMessage_DateFirstContact, $element->WebContactMessage_DateAssign)),
            $this->tableCellLeft(isset($element->Id_Booking) ? DateTime::dateTimeTransform($element->Booking_DateConfirm) : ''),
            $this->tableCellLeft($this->diferenceBetweenDates($element->WebContactMessage_DateFirstContact, $element->WebContactMessage_DateAssign)),
            $this->tableCellLeft($element->WebContactMessage_Status),
        ];

        for ($i = 0; $i < $length; $i++) {
            $key = 'WebContactMessageDetail_' . ($i + 1);
            if (isset($element->$key)) {
                $table[] = $this->tableCellLeft($element->$key['Package_Name'] ?? $element->$key['WebContactMessageDetail_Package']);
                $table[] = $this->tableCellLeft(DateTime::dateTransform($element->$key['WebContactMessageDetail_DateStart']));
            } else {
                $table[] = $this->tableCellLeft('');
                $table[] = $this->tableCellLeft('');
            }
        }
        return array_merge($table);
    }

    private function diferenceBetweenDates($date1, $date2)
    {
        $diff = DateUtils::dateDiference($date1, $date2, 'Y-m-d H:i:s');

        if ($diff->days > 0) {
            return $diff->format('%ad %Hh');
        }
        return $diff->format('%Hh %Im');
    }

    private function maxLength($data)
    {
        if (empty($data)) {
            return 0;
        }
        return max(array_map(fn($b) => $b->WebContactMessageDetailCount, $data));
    }

    public function run($data, $metadata)
    {
        $length = $this->maxLength($data);
        $titles = [
            ['N°', 6],
            ['Asignador', 12],
            ['Colaborador', 12],
            ['Código', 13],
            ['Canal', 13],
            ['Idioma', 14],
            ['Nombre', 20],
            ['País', 15],
            ['Correo', 30],
            ['Primer Mensaje', 60],
            ['Adultos', 8],
            ['Niños', 8],
            ['Recepción', 12],
            ['Asignación', 12],
            ['Demora Asignación', 12],
            ['Respuesta', 12],
            ['Demora Respuesta', 12],
            ['Pre-Booking', 12],
            ['Demora Pre-Booking', 13],
            ['Conversión', 12],
            ['Demora Conversión', 12],
            ['Estado', 12],
        ];
        for ($i = 0; $i < $length; $i++) {
            $titles[] = ['Paquete ' . ($i + 1), 30];
            $titles[] = ['Fecha de salida', 12];
        }
        return array_merge(
            [array_merge(array_map([$this, 'tableTitle'], $titles))],
            $this->getAllRows($data, $length)
        );
    }
}
