<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use URL;


class MarkerController extends Controller
{
    private $Mapa;
    public $MarkerJS = '';
    private $markerID = '';
    private $ref_x;
    private $ref_y;
    private $velikostX = 570;
    private $velikostY = 380;
    private $header;
    private $footer;
    private $barvaPozadi;
    private $teloKarty;
    private $title;
    private $urlPic = 'http://api.mapy.cz/img/api/marker/drop-red.png';


    /**
     * MarkerController constructor.
     * @param $mapa
     */
    public function __construct($mapa)
    {
        $this->Mapa = $mapa;
    }


    /**
     * @param string $markerID
     */
    public function setMarkerID(string $markerID): void
    {
        $this->markerID = $markerID;
    }

    /**
     * @param string $markerUrl
     */
    public function setUrlMarker(string $markerUrl): void
    {
        $this->urlPic = $markerUrl;
    }

    /**
     * @param float $ref_x
     */
    public function setRefX(float $ref_x): void
    {
        $this->ref_x = $ref_x;
    }

    /**
     * @param float $ref_y
     */
    public function setRefY(float $ref_y): void
    {
        $this->ref_y = $ref_y;
    }

    /**
     * @param string $title
     */
    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    public function vytvorMarkerJS(array $data): void
    {
        $this->setDataKartaRaw($data);
        $this->setRefX($data['info']->souradnice_x);
        $this->setRefY($data['info']->souradnice_y);
        $this->vytvorJavaScriptSouradnic();
        $this->vytvorJavaScriptProKartu();
        $this->vytvorJavaScriptProSimpleMarker();
        $this->vytvorJavaScriptProPridaniDekorace();
        $this->pridejDoMapy();
    }

    public static function geDataKartaRaw($dataKartaRaw): string
    {
        $telo= "<table style='font-family: Calibri,serif; font-size: 20px; color: grey;'>";
        $telo.= "<tr><td>Název čidla: </td><td style='font-weight: bold; color: black;'>{$dataKartaRaw['info']->nazev} ({$dataKartaRaw['info']->idStanice})</td></tr>";
        $telo.= "<tr><td>Adresa: </td><td style='color: black;'>" . $dataKartaRaw['info']->adresa . '</td></tr>';
        $telo.= sprintf("<tr><td>Aktualizace: </td><td style='color: black;'>%s</td></tr>", Carbon::parse($dataKartaRaw['aktualizace'])->format('d.m.Y H:i:s'));
        $telo.= '<tr><td>Historie: </td><td><a id=\'modalClick\' href=\'javascript:void('.$dataKartaRaw['info']->idStanice.');\' onclick=\'nastavModal('.$dataKartaRaw["info"]->idStanice.')\' data-toggle=\'modal\' data-target=\'#showHistory\'>Zobrazení statistik</a></td></tr>';
        $telo.= '</table>';

        $telo.= "<table style='font-family: Calibri,serif; font-size: 20px; width: 510px; margin-top: 10px;' cellspacing='0' cellpadding='0' >";
        $pocetSloupcu = (int)ceil((count($dataKartaRaw)-2)/5);
        $telo.="<tr style='background-image: url(".URL::asset('/resources/images/bg-top1.png')."); background-repeat:no-repeat; background-size: 100% 100%'>";
        for ($i=1;$i<=$pocetSloupcu;$i++)
        {
            $telo.="<td style='width: ". 100/(2*$pocetSloupcu) ."%; padding-left: 5px; color:white;'>Veličina</td><td style='width: ". 100/(2*$pocetSloupcu) ."%; padding-left: 5px; color:white;'>Hodnota</td>";
        }
        $telo.= '</tr>';
        $ai = 1;
        $celyRadek = false;
        $pocetRadku = 1;
        foreach ($dataKartaRaw as $idIterace => $dataVizitky)
        {
            if(is_array($dataVizitky))
            {
                foreach ($dataVizitky as $velicina => $hodnota)
                {
                    if($ai === 1)
                    {
                        if($pocetRadku%2!==0)
                        {
                            $telo.="<tr style='background-color: #e5e5e5'>";
                        }
                        else
                        {
                            $telo.='<tr>';
                        }
                    }
                    if($velicina !== 'info' && $velicina !== 'aktualizace')
                    {
                        $telo.="<td style='padding-left: 5px;'> $velicina :</td><td>$hodnota</td>";
                    }
                    if($ai === $pocetSloupcu)
                    {
                        $celyRadek = true;
                        $telo.= '</tr>';
                        $pocetRadku++;
                        $ai=1;
                    }
                    else
                    {
                        $ai++;
                    }
                }
            }
        }


        if($celyRadek === false)
        {
            $telo.= '<td/><td/></tr>';
        }
        $telo.= '</table>';

        return $telo;
    }

    public function setDataKartaRaw($dataKartaRaw): void
    {
        $this->teloKarty = self::geDataKartaRaw($dataKartaRaw);
    }

    public function vytvorJavaScriptProSimpleMarker(): void
    {
        $this->MarkerJS .= '
        var url_' .$this->markerID." =\"$this->urlPic\";\n
        var options_".$this->markerID." = {
                   title: \"$this->title\",
                   url: url_".$this->markerID.",
                   anchor: {left:10, bottom: 10}
               };\n
        var marker_".$this->markerID. ' = new SMap.Marker(souradnice_' .$this->markerID.", \"marker_$this->markerID\", options_".$this->markerID. ');';
    }

    private function vytvorJavaScriptProPridaniDekorace(): void
    {
        $this->MarkerJS .= 'marker_' .$this->markerID. '.decorate(SMap.Marker.Feature.Card, card_' .$this->markerID. ');';
    }

    private function vytvorJavaScriptProKartu(): void
    {
        $this->MarkerJS.="\nvar card_".$this->markerID. ' = new SMap.Card();
        card_' .$this->markerID.".setSize($this->velikostX , $this->velikostY);
        card_".$this->markerID.".getHeader().innerHTML = \"$this->header\";
        card_".$this->markerID.".getFooter().innerHTML = \"$this->footer\";
        card_".$this->markerID. '.getBody().style.margin = "0px 0px";
        card_' .$this->markerID.".getBody().style.backgroundColor = \"$this->barvaPozadi\";
        card_".$this->markerID. '.getBody().innerHTML = "' .str_replace(array("\n","\r"), ' ',$this->teloKarty)."\";\n ";
    }

    private function vytvorJavaScriptSouradnic(): void
    {
        $this->MarkerJS.="\nvar souradnice_$this->markerID = SMap.Coords.from{$this->Mapa->Mapa->TypSouradnic}($this->ref_x,$this->ref_y);\n";
    }

    private function pridejDoMapy(): void
    {
        $this->MarkerJS .= $this->Mapa->IDVrstva.'.addMarker(marker_'.$this->markerID.');';
    }



}
