<?php

namespace App\Http\Controllers;

class MapyController extends Controller
{
    public $TypSouradnic = 'JTSK';
    protected $Centrovani = false;
    protected $Ovladani = true;
    protected $ZmenaPodkladu = true;
    public $ParametrMapy;
    protected $ukazatelMysi = 'hand';
    public $MapaJS = '';

    public function __construct($parametr)
    {
        $this->ParametrMapy = $parametr;
    }

    public function setTypSouradnic($parametr): void
    {
        $this->TypSouradnic = $parametr;
    }

    public function setCentrovani(bool $parametr): void
    {
        $this->Centrovani = $parametr;
    }
    /**
     * @return bool
     */
    public function isCentrovani(): bool
    {
        return $this->Centrovani;
    }

    public function setOvladaniMapy(bool $parametr): void
    {
        $this->Ovladani = $parametr;
    }


    /**
     * @param bool $parametr
     */
    public function setZmenuPodkladuMapy(bool $parametr): void
    {
        $this->ZmenaPodkladu = $parametr;
    }

    public function ovladaniMapy(): void
    {
        if ($this->Ovladani === true)
        {
            $this->MapaJS  .= "{$this->ParametrMapy}.addDefaultControls();\n";
        }
    }

    public function setUkazatelMysi($parametr): void
    {
        $this->ukazatelMysi = $parametr;
    }

    private function zmenaUkazateleMysi(): void
    {
        $this->MapaJS  .= "{$this->ParametrMapy}.setCursor(\"$this->ukazatelMysi\");";
    }

    private function zmenaPodkladuMapy(): void
    {
        $this->MapaJS  .= "{$this->ParametrMapy}.addDefaultLayer(SMap.DEF_BASE).enable();";
        if ($this->ZmenaPodkladu === true)
        {
            $this->MapaJS  .= "
            {$this->ParametrMapy}.addDefaultLayer(SMap.DEF_OPHOTO);
            {$this->ParametrMapy}.addDefaultLayer(SMap.DEF_OPHOTO0203);
            {$this->ParametrMapy}.addDefaultLayer(SMap.DEF_OPHOTO0406);
            {$this->ParametrMapy}.addDefaultLayer(SMap.DEF_TURIST);
            {$this->ParametrMapy}.addDefaultLayer(SMap.DEF_HISTORIC);

            var layerSwitch = new SMap.Control.Layer();
            layerSwitch.addDefaultLayer(SMap.DEF_BASE);
            layerSwitch.addDefaultLayer(SMap.DEF_OPHOTO);
            layerSwitch.addDefaultLayer(SMap.DEF_TURIST);
            layerSwitch.addDefaultLayer(SMap.DEF_OPHOTO0406);
            layerSwitch.addDefaultLayer(SMap.DEF_OPHOTO0203);
            layerSwitch.addDefaultLayer(SMap.DEF_HISTORIC);
            {$this->ParametrMapy}.addControl(layerSwitch, {left:\"8px\", top:\"9px\"});
            \n";
        }
    }

    /**
     * @param $x
     * @param $y
     * @return string
     */
    public function VytvorJavaScriptProPridaniSouradniceDoAutoZoomu($x, $y): string
    {
        return "\n var souradnicePush = SMap.Coords.from{$this->TypSouradnic}({$x},{$y});
         SouradniceProZoom.push(souradnicePush);";
    }

    private function centrovaniMapyAlokace(): void
    {
        if ($this->Centrovani === true)
        {
            $this->MapaJS  .= "var {$this->ParametrMapy} = new SMap(JAK.gel(\"{$this->ParametrMapy}\"));
                {$this->ParametrMapy}.addControl(new SMap.Control.Sync());
                \n var SouradniceProZoom = [];";
        }
        else
        {
            $this->MapaJS  .= 'var center = SMap.Coords.from' . $this->TypSouradnic . '(' . env('mapa_centrovani_x') . ',' . env('mapa_centrovani_y') . ");
                    var $this->ParametrMapy = new SMap(JAK.gel(\"{$this->ParametrMapy}\"), center," . 8 . ");\n";
        }
    }

    /**
     * @return string
     */
    public function centrovaniMapyVypocet(): string
    {
        return "\n var cz = {$this->ParametrMapy}.computeCenterZoom(SouradniceProZoom,true);
                    {$this->ParametrMapy}.setCenterZoom(cz[0], cz[1]);";
    }

    private function mapaPadding(): void
    {
        $this->MapaJS .= " {$this->ParametrMapy}.setPadding(\"left\",5);
              {$this->ParametrMapy}.setPadding(\"right\",85);
              {$this->ParametrMapy}.setPadding(\"bottom\",20);
              {$this->ParametrMapy}.setPadding(\"top\",5);\n";
    }

    private function vytvorJavascriptProMapu(): void
    {
        $this->centrovaniMapyAlokace();
        $this->zmenaPodkladuMapy() ;
        $this->ovladaniMapy();
        $this->zmenaUkazateleMysi();
        $this->mapaPadding();
    }

    /**
     * @return string
     */
    public function vytvorMapu(): string
    {
        $this->vytvorJavascriptProMapu();
        return $this->MapaJS;
    }
}
