<?php

declare(strict_types=1);

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

use Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\DruhSubjektuZaznamuEnum;
use Seka\Portal\Modul\Statistiky\Http\View\ZaznamDatumFilterEnum;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Presenters\Summary\ZaznamTechnikSummaryPresenter;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaDruhu\PocetKontrolPodlaDruhuDataTableBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidla\PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidlaDataTableBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaHodnotenia\PocetKontrolPodlaHodnoteniaDataTableBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaKategorieVozidla\PocetKontrolPodlaKategorieVozidlaDataTableBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaStavu\PocetKontrolPodlaStavuDataTableBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolSObratom\PocetKontrolSObratomDataTableBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\PocetKontrolPodlaDruhu\PocetKontrolPodlaDruhuEChartSourceBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\PocetKontrolPodlaEmisnehoSystemuVozidla\PocetKontrolPodlaEmisnehoSystemuVozidlaEChartSourceBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\PocetKontrolPodlaHodnotenia\PocetKontrolPodlaHodnoteniaEChartSourceBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\PocetKontrolPodlaKategorieVozidla\PocetKontrolPodlaKategorieVozidlaEChartSourceBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\PocetKontrolPodlaStavu\PocetKontrolPodlaStavuEChartSourceBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ECharts\Technik\PocetKontrolSObratom\PocetKontrolSObratomEChartSourceBuilder;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ZaznamTechnikSummaryDataTableTransformer;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\ZaznamTechnikSummaryEChartsDataTransformer;

/**
 * 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\Contracts\View\View|\Illuminate\Foundation\Application|\Illuminate\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     * @throws \Exception
     */
    protected function makeView(ZaznamDatumFilterEnum $dateFilter)
    {
        $pekId = auth()->user()->pracovisko->id;

        list($from, $to) = $this->getFromDate($dateFilter);
        $data = $this->getData($pekId, $from, $to, DruhSubjektuZaznamuEnum::POUZIVATEL);
        $summaryDataTables = $this->makeDataTables((new ZaznamTechnikSummaryPresenter(new ZaznamTechnikSummaryDataTableTransformer()))->present($data));
        $summaryECharts = $this->makeECharts((new ZaznamTechnikSummaryPresenter(new ZaznamTechnikSummaryEChartsDataTransformer()))->present($data));

        return view('Statistiky::frontend.pages.technici.index')
            ->with([
                'datumOd' => $from->format('d.m.Y'),
                'datumDo' => $to->format('d.m.Y'),
                'form_datefilter' => $dateFilter,
                'form_date_from' => $from->format('Y-m-d'),
                'form_date_to' => $to->format('Y-m-d'),
                'filterFormActionRoute' => route('modul.statistiky.frontend.technici.filter'),
            ])
            ->with($summaryDataTables)
            ->with($summaryECharts);
    }

    /**
     * @param array $rows
     *
     * @return array
     */
    private function makeDataTables(array $rows): array
    {
        $out = [];

        $dataTableRows = $this->flattenArray($rows);

        $dataTable = PocetKontrolPodlaDruhuDataTableBuilder::buildWithRows($dataTableRows);
        $out[class_basename($dataTable)] = $dataTable;

        $dataTable = PocetKontrolPodlaHodnoteniaDataTableBuilder::buildWithRows($dataTableRows);
        $out[class_basename($dataTable)] = $dataTable;

        $dataTable = PocetKontrolPodlaStavuDataTableBuilder::buildWithRows($dataTableRows);
        $out[class_basename($dataTable)] = $dataTable;

        $dataTable = PocetKontrolPodlaKategorieVozidlaDataTableBuilder::buildWithRows($dataTableRows);
        $out[class_basename($dataTable)] = $dataTable;

        $dataTable = PocetKontrolPodlaDruhuPalivaAEmisnehoSystemuVozidlaDataTableBuilder::buildWithRows($dataTableRows);
        $out[class_basename($dataTable)] = $dataTable;

        $dataTable = PocetKontrolSObratomDataTableBuilder::buildWithRows($dataTableRows);
        $out[class_basename($dataTable)] = $dataTable;

        return $out;
    }

    /**
     * @param $array
     *
     * @return array
     */
    private function flattenArray($array): array
    {
        $return = array();

        foreach ($array as $val) {
            $keys = array_keys($val);

            foreach ($keys as $key) {
                $return[] = is_array($val[$key]) ? $val[$key][0] : $val[$key];
            }
        }

        return $return;
    }

    /**
     * @param array $items
     *
     * @return array
     */
    private function makeECharts(array $items): array
    {
        $out = [];

        $chartItems = $this->flattenArray($items);

        $chartSource = PocetKontrolSObratomEChartSourceBuilder::buildWithItems($chartItems);
        $out[class_basename($chartSource)] = $chartSource;

        $chartSource = PocetKontrolPodlaStavuEChartSourceBuilder::buildWithItems($chartItems);
        $out[class_basename($chartSource)] = $chartSource;

        $chartSource = PocetKontrolPodlaEmisnehoSystemuVozidlaEChartSourceBuilder::buildWithItems($chartItems);
        $out[class_basename($chartSource)] = $chartSource;

        $chartSource = PocetKontrolPodlaKategorieVozidlaEChartSourceBuilder::buildWithItems($chartItems);
        $out[class_basename($chartSource)] = $chartSource;

        $chartSource = PocetKontrolPodlaDruhuEChartSourceBuilder::buildWithItems($chartItems);
        $out[class_basename($chartSource)] = $chartSource;

        $chartSource = PocetKontrolPodlaHodnoteniaEChartSourceBuilder::buildWithItems($chartItems);
        $out[class_basename($chartSource)] = $chartSource;

        return $out;
    }
}
