<?php

namespace App\Http\Controllers;

use App\EmailsNotify;
use Carbon\Carbon;
use App\Menu;
use App\Mqtt_data_request;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class KontrolaPrijmuDatController extends Controller
{

    public function index()
    {
        $data = new EmailsNotify();
        return view('NastaveniEmails.nastaveniEmails', ['dataC' => $data->getData(),'menu' => Menu::getData()]);
    }

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

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

        if ($data->fails())
        {
            return redirect()->back()->withErrors($data->errors())->with('message-fail', 'Informovat po (min) není validní = musí být celé číslo');
        }
        EmailsNotify::create([
            'komu' => $request->get('komu'),
            'id_stanice' => $request->get('id_stanice'),
            'vlozil' => Auth::user()->id,
            'delka_odezvy_v_minutach' => $request->get('delka_odezvy_v_minutach'),
        ])->save();

        return redirect()->route('emails-index')->with('message', 'Upozornění vytvořeno');
    }

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

    public function delete(EmailsNotify $idemails_notify)
    {
        try
        {
            $idemails_notify->delete();
        }
        catch (Exception $e)
        {
            return redirect()->route('emails-index')->with('message-fail','Vyskytla se chyba'.$e);
        }
        return redirect()->route('emails-index')->with('message','Upozornění smazáno');
    }

    public function edit(EmailsNotify $data)
    {
        return view('NastaveniEmails.nastaveniEmailsEdit',['dataC' => $data,'menu' => Menu::getData()]);
    }

    public function update(Request $request, EmailsNotify $idemails_notify)
    {
        $request->except('_token');
        $request->merge(['vlozil' => Auth::user()->id]);
        $dataValidate = $this->validatorCasu($request);
        if ($dataValidate->fails())
        {
            return redirect()->back()->withErrors($dataValidate->errors())->with('message-fail', 'Informovat po (min) není validní = musí být celé číslo');
        }

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



    public function upozorniEmailem(): void
    {
        /**
         * 1) najdi vsechna pravidla
         * 2) najdi stanici a jeji posledni prijeti data
         * 3) odesli email :)
         */

        $dataPravidla = new EmailsNotify();
        foreach ($dataPravidla->getData() as $id => $data)
        {
            $posledniZprava = Mqtt_data_request::getDataLast($data->id_stanice)[0]->vlozeny;
            if(Carbon::parse($posledniZprava)->addMinutes($data->delka_odezvy_v_minutach)->format('Y-m-d H:i:s')<=Carbon::now()->format('Y-m-d H:i:s'))
            {
               $email = new EmailController();
               $email->komu = $data->email;
               $email->jmeno = $data->name;
               $email->predmet = 'Stanice '.$data->id_stanice. ' se neohlásila';
               $email->zprava = 'Stanice '.$data->id_stanice. ' se neohlásila, její poslední zpráva byla v ' .$posledniZprava;
               $email->sendEmail();
            }
        }
    }

}
