<?php

namespace App\Http\Controllers;

use App\Menu;
use App\NazvosloviVelicin;
use App\Stanice;
use Auth;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View as ViewAlias;
use Illuminate\Http\Request;
use JsonException;
use URL;

class StaniceController extends Controller
{
    public $stanice = [];

    public function getStanice($idStanice = null): void
    {
        foreach (Stanice::getData($idStanice) as $staniceInfo)
        {
            $this->stanice[$staniceInfo->idStanice] = $staniceInfo;
        }
    }

    public function index()
    {
        return view('Stanice.stanice',['stanice' => Stanice::getStaniceAll(),'menu' => Menu::getData()]);
    }

    /**
     * @param Stanice $stanice
     * @return Factory|ViewAlias
     */
    public function edit(Stanice $stanice)
    {
        return view('Stanice.staniceEdit', ['stanice' => $stanice,'menu' => Menu::getData(), 'vizitka' => NazvosloviVelicin::getDataNazvoslovi()]);
    }

    public function update(Stanice $stanice, Request $request)
    {
        $stanice->setDataToDB($request->all(['zobrazeni','vizitka']));

        if($request->has('registrovano'))
        {
            $request->merge(['registrovano' => 1]);
        }
        else
        {
            $request->merge(['registrovano' => 0]);
        }

        $request->merge(['luser' => Auth::id()]);
        $request->except('_token');
        $stanice->update($request->all());
        return redirect()->route('stanice-edit',$stanice)->with('message','Změny uloženy');
    }

    public function ban(Stanice $stanice)
    {
        $stanice->update(['ban'=> 1]);
        return redirect()->route('stanice-index')->with('message','Byla ZABANOVÁNA stanice ID: '.$stanice->appId);
    }

    /**
     * @param Stanice $stanice
     * @return RedirectResponse
     */
    public function unban(Stanice $stanice)
    {
        $stanice->update(['ban'=> 0]);
        return redirect()->route('stanice-index')->with('message','Byla ODBANOVÁNA stanice ID: '.$stanice->appId);
    }

    public function vypnout(Stanice $stanice)
    {
        $stanice->update(['registrovano'=> 0]);
        return redirect()->route('stanice-index')->with('message','Bylo VYPNUTO zobrazení stanice ID: '.$stanice->appId);
    }

    public function zapnout(Stanice $stanice)
    {
        $stanice->update(['registrovano'=> 1]);
        return redirect()->route('stanice-index')->with('message','Bylo ZAPNUTO zobrazení stanice ID: '.$stanice->appId);
    }

    public static function getMapaAStaniceVBladeVizitka($idStanice): string
    {
        $stanice = new MarkerDataController();
        try
        {
            $stanice->getDataFromDB($idStanice);
        }
        catch (JsonException $e)
        {
            dump($e);
        }
        $dataT = "<div style='width: 550px; height: 400px; border: 2px solid black; padding: 15px; border-radius: 15px; background-color: whitesmoke;'>";
        foreach ($stanice->allData as $id => $data)
        {
            $dataT .= MarkerController::geDataKartaRaw($data);
        }
        $dataT.= '</div>';

        return $dataT;
    }

    public function getMapaAStanice($idStanice = null): string
    {
        /** @var  $mapa */
        $mapa = new MapyController(env('parametr_mapy'));
        $mapa->setCentrovani(true);

        /** @var  $vrstva */
        $vrstva = new VrstvaController($mapa);
        $vrstva->nastavTypVrstvy(VrstvaController::$VRSTVA_MARKERU);
        $vrstva->setIdVrstva(VrstvaController::$VRSTVA_MARKERU. 's_layer');
        $vrstva->vytvorJavaScriptProVrstvu();

        /** @var  $markery */
        $markery = new MarkerController($vrstva);

        $stanice = new MarkerDataController();
        try
        {
            $stanice->getDataFromDB($idStanice);
        }
        catch (JsonException $e)
        {
            dump($e);
        }
        $JSpush = '';
        foreach ($stanice->allData as $id => $data)
        {
            $markery->setMarkerID('Stanice_' .$id);
            $markery->setUrlMarker(URL::asset('/resources/images/ico-spot_mensi.png'));
            $markery->setTitle('Stanice ' .$id);
            $markery->vytvorMarkerJS($data);
            if($mapa->isCentrovani() )
            {
                $JSpush.= $mapa->VytvorJavaScriptProPridaniSouradniceDoAutoZoomu($data['info']->souradnice_x,$data['info']->souradnice_y);
            }
        }

        /** @var  $JS */
        $JS =  $mapa->vytvorMapu();
        if($mapa->isCentrovani() && $markery->MarkerJS!=='')
        {
            $JS .= $JSpush;
            $JS .= $mapa->centrovaniMapyVypocet();
        }
        $JS .= $vrstva->VrstvaJS;
        $JS .= $markery->MarkerJS;

        return $JS;
    }
}
