<?php
/**
 * Created by PhpStorm.
 * User: radko
 * Date: 15.11.18
 * Time: 11:19
 */

namespace Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Backend\Kniznica;

use Exception;
use Illuminate\Support\Facades\Log;
use Seka\Portal\Modul\Vzdelavanie\Dtos\Kniznica\KategoriaDTO;
use Seka\Portal\Modul\Vzdelavanie\Events\Kniznica\Backend\KategoriaCreated;
use Seka\Portal\Modul\Vzdelavanie\Events\Kniznica\Backend\KategoriaDeleted;
use Seka\Portal\Modul\Vzdelavanie\Events\Kniznica\Backend\KategoriaUpdated;
use Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Controller;
use Seka\Portal\Modul\Vzdelavanie\Http\Requests\KategoriaStoreRequest;
use Seka\Portal\Modul\Vzdelavanie\Http\Requests\KategoriaUpdateRequest;
use Seka\Portal\Modul\Vzdelavanie\Services\KategoriaServiceInterface;

/**
 * Class KategoriaController
 *
 * @package Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Backend\Kniznica
 */
class KategoriaController extends Controller
{
    /**
     * @var \Seka\Portal\Modul\Vzdelavanie\Services\KategoriaServiceInterface
     */
    protected KategoriaServiceInterface $kategoriaService;

    /**
     * KategoriaController constructor.
     *
     * @param \Seka\Portal\Modul\Vzdelavanie\Services\KategoriaServiceInterface $kategoriaService
     */
    public function __construct(KategoriaServiceInterface $kategoriaService)
    {
        $this->kategoriaService = $kategoriaService;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function index()
    {
        $kategorie = null;

        try {
            $kategorie = $this->kategoriaService->vratVsetky();
        } catch (Exception $e) {
            flash()->error($e->getMessage())->important();
        } finally {
            return view('Vzdelavanie::pages.sprava.kniznica.kategorie.index')
                ->withKategorie($kategorie);
        }
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function create()
    {
        $actionRoute = null;

        try {
            $actionRoute = route('vzdelavanie.sprava.kniznica.kategoria.store');
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error($e->getMessage())->important();
        } finally {
            return view('Vzdelavanie::pages.sprava.kniznica.kategorie.nova')
                ->withActionRoute($actionRoute);
        }
    }

    /**
     * @param \Seka\Portal\Modul\Vzdelavanie\Http\Requests\KategoriaStoreRequest $storeRequest
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(KategoriaStoreRequest $storeRequest)
    {
        try {
            $kategoria = $this->kategoriaService->vytvorit($storeRequest->all(['nazov', 'obrazok']));

            flash()->success('Nová kategória bola vytvorená.')->important();

            event(new KategoriaCreated(KategoriaDTO::fromModel($kategoria)));
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error($e->getMessage())->important();

            return back()->withInput($storeRequest->all());
        }

        return redirect(route('vzdelavanie.sprava.kniznica.index'));
    }

    /**
     * @param int $vzdKnzncKategoriaId
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function edit(int $vzdKnzncKategoriaId)
    {
        try {
            $kategoria = $this->kategoriaService->vratPodlaId($vzdKnzncKategoriaId);
            $actionRoute = route('vzdelavanie.sprava.kniznica.kategoria.update', $kategoria);
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error($e->getMessage())->important();
        }

        return view('Vzdelavanie::pages.sprava.kniznica.kategorie.upravit')
            ->withActionRoute($actionRoute)
            ->withKategoria($kategoria);
    }

    /**
     * @param \Seka\Portal\Modul\Vzdelavanie\Http\Requests\KategoriaUpdateRequest $updateRequest
     * @param int                                                                 $vzdKnzncKategoriaId
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(KategoriaUpdateRequest $updateRequest, int $vzdKnzncKategoriaId)
    {
        try {
            $this->kategoriaService->upravit($vzdKnzncKategoriaId, $updateRequest->all(['nazov', 'obrazok']));
            $kategoria = $this->kategoriaService->vratPodlaId($vzdKnzncKategoriaId);

            flash()->success('Kategória bola upravená.')->important();

            event(new KategoriaUpdated(KategoriaDTO::fromModel($kategoria)));
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error($e->getMessage())->important();

            return back()->withInput($updateRequest->all());
        }

        return redirect(route('vzdelavanie.sprava.kniznica.index'));
    }

    /**
     * @param int $vzdKnzncKategoriaId
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy(int $vzdKnzncKategoriaId)
    {
        try {
            $kategoria = $this->kategoriaService->vratPodlaId($vzdKnzncKategoriaId);
            $this->kategoriaService->odstranit($vzdKnzncKategoriaId);

            flash()->success('Kategória bola odstránená.')->important();

            event(new KategoriaDeleted(KategoriaDTO::fromModel($kategoria)));
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error($e->getMessage())->important();

            return back();
        }

        return redirect(route('vzdelavanie.sprava.kniznica.kategoria.index'));
    }
}
