<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\View\View;

class SwitcherIndexController extends Controller
{
    public CONST SUBSCRIBESCRIPT = 'indexSubscribe.php';

    /**
     * @param Request $request
     * @return Factory|View
     */
    public function switcher(Request $request)
    {
        if(strstr($request->server()['PHP_SELF'],self::SUBSCRIBESCRIPT) === self::SUBSCRIBESCRIPT)
        {
            dump('ziju');
            $subscriber = new MqttSubscribeController();
            $server = DB::table('mqtt_server_set')->where('aktivni', '=', 1)->first();
            if($server)
            {
                $ipAdresa = $server->ipadresa;
            }
            else
            {
                $ipAdresa = env('mqtt_host_cidlo');
            }
            $subscriber->setHost($ipAdresa);
            $subscriber->subscribeToTopic('#');
        }
        else
        {
            $mapa = new StaniceController();
            return view('mapa',['mapa'=> $mapa->getMapaAStanice()]);
        }
    }
}
