<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Http\Controllers\Frontend;

use Carbon\Carbon;
use Seka\Portal\Modul\Statistiky\Http\View\ZaznamDatumFilterEnum;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\DruhSubjektuZaznamuEnum;

/**
 * Class TechnikController
 *
 * @package Seka\Portal\Modul\Statistiky\Http\Controllers\Frontend
 */
final class TechnikController extends FilterController
{
    /**
     * @param \Seka\Portal\Modul\Statistiky\Http\View\ZaznamDatumFilterEnum $dateFilter
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     */
    protected function makeView(ZaznamDatumFilterEnum $dateFilter)
    {
        $pekId = auth()->user()->pracovisko->id;

        list($from, $to) = $this->getFromDate($dateFilter);
        $data = $this->getData($pekId, $from, $to, DruhSubjektuZaznamuEnum::POUZIVATEL());

        $newCollection = $this->createRequiredStructure($data);

        return view('Statistiky::frontend.pages.technici.index')
            ->with([
                'TekZoznamPoctuKontrolSObratomDataTableComposer' => $newCollection['zoznamPoctuKontrolSObratom'],
                'TekZoznamPoctuKontrolPodlaDruhuDataTableComposer' => $newCollection['zoznamPoctuKontrolPodlaDruhu'],
                'TekZoznamPoctuKontrolPodlaStavuDataTableComposer' => $newCollection['zoznamPoctuKontrolPodlaStavu'],
                'TekZoznamPoctuKontrolPodlaHodnoteniaDataTableComposer' => $newCollection['zoznamPoctuKontrolPodlaHodnotenia'],
                'TekZoznamPoctuKontrolPodlaKategorieVozidlaDataTableComposer' => $newCollection['zoznamPoctuKontrolPodlaKategorieVozidla'],
                'TekZoznamPoctuKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidlaDataTableComposer' => $newCollection['zoznamPoctuKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla'] ?? null,
                'datumDo' => $to->format('d.m.Y'),
                'datumOd' => $from->format('d.m.Y'),
                'datefilter' => $dateFilter,
                'filterFormActionRoute' => route('modul.statistiky.frontend.technici.filter'),
            ]);
    }

    /**
     * @param array $data
     *
     * @return array|\array[][]
     */
    private function createRequiredStructure(array $data): array
    {
        $collection = collect($data);
        $newCollection = [
            'zoznamPoctuKontrolSObratom' => [
                'stlpce' => [],
                'udaje' => [],
            ],
            'zoznamPoctuKontrolPodlaDruhu' => [
                'stlpce' => [],
                'udaje' => [],
            ],
            'zoznamPoctuKontrolPodlaStavu' => [
                'stlpce' => [],
                'udaje' => [],
            ],
            'zoznamPoctuKontrolPodlaHodnotenia' => [
                'stlpce' => [],
                'udaje' => [],
            ],
            'zoznamPoctuKontrolPodlaKategorieVozidla' => [
                'stlpce' => [],
                'udaje' => [],
            ],
            'zoznamPoctuKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla' => [
                'stlpce' => [],
                'udaje' => [],
            ],
        ];

        foreach ($newCollection as $zoznam => $arr) {
            $collection->each(function ($pouzivatel) use (&$newCollection, $zoznam) {
                foreach ($pouzivatel[$zoznam]['stlpce'] as $stlpec) {
                    $exist = false;

                    foreach ($newCollection[$zoznam]['stlpce'] as $_stlpec) {
                        if ($exist = in_array($stlpec['title'], $_stlpec)) {
                            break;
                        }
                    }

                    if (!$exist) {
                        $newCollection[$zoznam]['stlpce'][] = $stlpec;
                    }
                }

                $newCollection[$zoznam]['udaje'] = array_merge($newCollection[$zoznam]['udaje'], $pouzivatel[$zoznam]['udaje']);
            });
        }

        // Stlpec Spolu na koniec pola
        foreach ($newCollection as &$arr) {
            $i = 0;
            $a = null;

            foreach ($arr['stlpce'] as $stl) {
                if (in_array('spolu', $stl)) {
                    $a = $stl;
                    unset($arr['stlpce'][$i]);
                    $arr['stlpce'] = array_values($arr['stlpce']);
                    break;
                }
                ++$i;
            }

            if ($a) {
                array_push($arr['stlpce'], $a);
                $a = null;
            }
        }

        return $newCollection;
    }
}
