<?php

namespace App;

use Eloquent;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

/**
 * App\Stanice
 *
 * @property int $idStanice
 * @property string|null $nazev
 * @property float|null $souradnice_x
 * @property int|null $souradnice_y
 * @property string|null $adresa
 * @property float|null $vlozil
 * @property string|null $vlozeny
 * @property string|null $zmena
 * @property int|null $luser
 * @property int|null $velikostKartyX
 * @property int|null $velikostKartyY
 * @property int|null $registrovano
 * @property int|null $ban
 * @property mixed appId
 * @method static Builder|Stanice newModelQuery()
 * @method static Builder|Stanice newQuery()
 * @method static Builder|Stanice query()
 * @method static Builder|Stanice whereAdresa($value)
 * @method static Builder|Stanice whereIdStanice($value)
 * @method static Builder|Stanice whereLuser($value)
 * @method static Builder|Stanice whereNazev($value)
 * @method static Builder|Stanice whereRegistrovano($value)
 * @method static Builder|Stanice whereSouradniceX($value)
 * @method static Builder|Stanice whereSouradniceY($value)
 * @method static Builder|Stanice whereVelikostKartyX($value)
 * @method static Builder|Stanice whereVelikostKartyY($value)
 * @method static Builder|Stanice whereVlozeny($value)
 * @method static Builder|Stanice whereVlozil($value)
 * @method static Builder|Stanice whereZmena($value)
 * @mixin Eloquent
 */
class Stanice extends Model
{
    public const NEDEFINOVANO = 1;

    protected $table = 'stanice';
    protected $primaryKey = 'appId';
    protected $fillable = ['idStanice','nazev','adresa', 'vlozil', 'registrovano', 'luser', 'ban', 'souradnice_x', 'souradnice_y'];
    public $timestamps = false;

    public static function getData($idStanice = null)
    {
        if($idStanice)
        {
            return self::where(['idStanice'=>$idStanice])->get();
        }

        return self::where(['registrovano'=>1])->get();
    }

    public static function getStaniceAll()
    {
        return self::all();
    }

    public static function isBanned($idStanice)
    {
        $data = self::where('idStanice', '=', $idStanice)->get('ban');
        if(empty($data[0]))
        {
            /**
             * toto je naprosta kktina, protože jsem udělal chybu na zacatku :( - střelil jsem se nadherně do nohy...
             */
            $a = new Stanice();
            $a->ban = 0;
            $t[] = $a;
            return $t;
        }
        else
        {
            return $data;
        }
    }


    /**
     * @param $idStanice
     * @param $user
     * @throws Exception
     */
    public static function staniceExist($idStanice, $user): void
    {
        $stanice = self::where('idStanice', '=', $idStanice)->get('idStanice');
        if(empty($stanice[0]))
        {
            self::create([
                'idStanice' => $idStanice,
                'nazev' => 'Nová stanice',
                'souradnice_x' => env('mapa_centrovani_x')+random_int(0,1000),
                'souradnice_y' => env('mapa_centrovani_y')+random_int(0,1000),
                'adresa' => 'nenastaveno',
                'vlozil' => $user,
                'luser' => $user,
                'registrovano' => 0,
                'ban' => 1
            ])->save();
        }
    }

    public function getDataEdit(): Collection
    {
        return DB::table('stanice')
            ->join('vizitky','stanice.idStanice','=','vizitky.id_stanice')
            ->leftJoin('vizitka_nazvoslovi','vizitka_nazvoslovi.id_vizitka','=','vizitky.idVizitky')
            ->leftJoin('nazvoslovi_velicin','nazvoslovi_velicin.idnazvoslovi_veliciny','=','id_nazvoslovi_velicin')
            ->where(
                        [
                            ['stanice.idStanice','=',$this->idStanice],
                            ['sloupec_velicina','!=','aktualizace']
                        ]
                    )
            ->orderByDesc('zobrazit')
            ->get(['idVizitky', 'sloupec_velicina','idvizitka_nazvoslovi','idnazvoslovi_veliciny','jednotka', 'zobrazit']);
    }

    public function setDataToDB($request): void
    {
        foreach ($request['zobrazeni'] as $sloupec => $zobrazit)
        {
           if($zobrazit === 'on')
           {
               $zobrazit = 1;
           }
           DB::table('vizitky')
               ->where(
                        [
                            ['sloupec_velicina', '=', $sloupec],
                            ['id_stanice', '=', $this->idStanice],
                        ]
                      )
               ->update(['zobrazit' => $zobrazit]);
        }

        foreach ($request['vizitka'] as $vizitka => $typ)
        {
            if(self::NEDEFINOVANO !== (int)$typ)
            {
                DB::table('vizitka_nazvoslovi')->updateOrInsert(['id_vizitka'=>$vizitka],['id_nazvoslovi_velicin'=>(int)$typ]);
            }
            else
            {
                DB::table('vizitka_nazvoslovi')->where(['id_vizitka'=>$vizitka])->delete();
            }
        }
    }


    public static function getStaniceProWu(): Collection
    {
        return self::all(['idStanice','nazev']);
    }
}
