<?php

namespace App\Http\Controllers;

use App\EmailSenderDefine;
use App\Menu;
use Config;
use Exception;
use Mail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class EmailController extends Controller
{

    public $komu;
    public $predmet;
    public $zprava;
    public $jmeno = '';

    public function sendEmail($typeEmail = null)
    {
        if (!isset(Config::all()['mail']['username']))
        {
            return 'fail';
        }
        try
        {
            if($typeEmail === null)
            {
                $mailBlade = 'mail';
            }
            else
            {
                $mailBlade = $typeEmail;
            }
            $response = Mail::send($mailBlade, ['text' => $this->zprava], function($message)
            {
                $message->to( $this->komu, $this->jmeno)->subject($this->predmet);
                $message->from(Config::all()['mail']['username'],'DP - STD');
            });
        }
        catch (Exception $e)
        {
            return $e;
        }

        return $response;
    }



    public function index()
    {
        return view('EmailSet.nastaveniEmailSet', ['dataC' => EmailSenderDefine::all(),'menu' => Menu::getData()]);
    }

    public function updateServer(Request $request)
    {
        EmailSenderDefine::where('aktivni', '=',1)->update(['aktivni' => 0]);
        $emailSettings = EmailSenderDefine::find($request->all()['aktivni']);
        $emailSettings->aktivni = 1;
        $emailSettings->update();

        return redirect()->route('emailSet-index')->with('message', 'Změněno');
    }


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

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

        if ($data->fails())
        {
            return redirect()->back()->withErrors($data->errors())->with('message-fail', 'Něco se nepovedlo :( ');
        }
        EmailSenderDefine::create([
            'driver' => $request->get('driver'),
            'host' => $request->get('host'),
            'port' => $request->get('port'),
            'username_email' => $request->get('username_email'),
            'password' => $request->get('password'),
            'encryption' => $request->get('encryption'),
            'vlozil' => Auth::user()->id,
        ])->save();

        return redirect()->route('emailSet-index')->with('message', 'Nastavení vytvořeno');
    }



    protected function validatorRequestu(Request $data)
    {
        return Validator::make($data->all(), [
            'driver' => ['required', 'string'],
            'host' => ['required'],
            'port' => ['required', 'integer'],
            'username_email' =>['required'],
            'password' => ['required'],
            'encryption' => ['required']
        ]);
    }

    public function delete(EmailSenderDefine $idemail_sender_define)
    {
        try
        {
            $idemail_sender_define->delete();
        }
        catch (Exception $e)
        {
            return redirect()->route('emailSet-index')->with('message-fail','Vyskytla se chyba'.$e);
        }
        return redirect()->route('emailSet-index')->with('message','Nastavení smazáno');
    }

    public function testovaci()
    {
        $this->komu = Auth::user()->email;
        $this->zprava = 'Toto je testovací email';
        $this->jmeno = Auth::user()->name;
        $this->predmet = 'Testovací email z aplikace DP-STD';
        $response = $this->sendEmail();
        if($response)
        {
            return redirect()->route('emailSet-index')->with('message-fail',$response);
        }
        return redirect()->route('emailSet-index')->with('message','Testovací email odeslan');

    }


    public function edit(EmailSenderDefine $idemail_sender_define)
    {
        return view('EmailSet.nastaveniEmailSetEdit',['dataC' => $idemail_sender_define,'menu' => Menu::getData()]);
    }

    public function update(Request $request, EmailSenderDefine $idemail_sender_define)
    {
        $request->except('_token');
        $request->merge(['vlozil' => Auth::user()->id]);
        $dataValidate = $this->validatorRequestu($request);
        if ($dataValidate->fails())
        {
            return redirect()->back()->withErrors($dataValidate->errors())->with('message-fail', 'Něco se nepovedlo :(');
        }

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

}
