<?php

namespace App\Http\Controllers;

use App\AutomatickeBanovani;
use App\Menu;
use App\Mqtt_data_request;
use App\Stanice;
use Auth;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AutomatickeBanovaniController extends Controller
{
    public function index()
    {
        return view('BanovaniAutomaticky.banovani',['menu' => Menu::getData()]);
    }

    public function edit(AutomatickeBanovani $idban_automat)
    {
        return view('BanovaniAutomaticky.banovaniEdit',['dataC' => $idban_automat,'menu' => Menu::getData()]);
    }
    public function update(Request $request,AutomatickeBanovani $idban_automat)
    {
        $request->except('_token');
        $dataValidate = $this->validatorFormu($request);
        if ($dataValidate->fails())
        {
            return redirect()->back()->withErrors($dataValidate->errors())->with('message-fail', 'Nastala chyba počty nejsou celá čísla');
        }

        $idban_automat->update($request->all());
        return redirect()->route('ban-index')->with('message', 'Nastavení upraveno');
    }

    public function add()
    {
        return view('BanovaniAutomaticky.banovaniAdd',['menu' => Menu::getData()]);
    }

    public function create(Request $request)
    {
        $data = $this->validatorFormu($request);

        if ($data->fails())
        {
            return redirect()->back()->withErrors($data->errors())->with('message-fail', 'Nastala chyba počty nejsou celá čísla');
        }
        AutomatickeBanovani::create([
            'id_stanice' => $request->get('id_stanice'),
            'pocet_zprav' => $request->get('pocet_zprav'),
            'pocet_minut' => $request->get('pocet_minut'),
            'vlozil' => Auth::user()->id,
        ])->save();
        return redirect()->route('ban-index')->with('message', 'Přístup vytvořen');
    }

    protected function validatorFormu(Request $data)
    {
        return Validator::make($data->all(), [
            'pocet_zprav' => ['required','integer'],
            'pocet_minut' => ['required','integer'],
        ]);
    }

    public function delete(AutomatickeBanovani $idban_automat)
    {
        try
        {
            $idban_automat->delete();
        }
        catch (Exception $e)
        {
            return redirect()->route('ban-index')->with('message','Vyskytla se chyba'.$e);
        }
        return redirect()->route('ban-index')->with('message','Veličina smazána');
    }


    /**
     * @param $idStanice
     * @return bool true když je možné povolit
     */
    public function banChecker($idStanice): bool
    {
       $banovani =  AutomatickeBanovani::where('id_stanice','=',$idStanice)->first();
       if($banovani)
       {
           $pocetZprav =  Mqtt_data_request::getCountMsg($banovani);
           if($banovani->pocet_zprav <= $pocetZprav)
           {
                Stanice::where('idStanice','=', $idStanice)->update(['ban' => 1]);
                return false;
           }
            return true;
       }
       else
       {
           return true;
       }
    }
}
