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

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

use Exception;
use Illuminate\Support\Facades\Log;
use Seka\Vzdelavanie\Http\Controllers\Controller;
use Seka\Vzdelavanie\Services\KategoriaServiceInterface;
use Seka\Vzdelavanie\Http\Requests\KategoriaStoreRequest;
use Seka\Vzdelavanie\Http\Requests\KategoriaUpdateRequest;

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

    /**
     * KategoriaController constructor.
     * @param \Seka\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\Vzdelavanie\Http\Requests\KategoriaStoreRequest $storeRequest
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(KategoriaStoreRequest $storeRequest)
    {
        try {
            $this->kategoriaService->vytvorit($storeRequest->all(['nazov', 'obrazok']));
            flash()->success('Nová kategória bola vytvorená.')->important();
        } 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\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']));
        } 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 $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy(int $vzdKnzncKategoriaId)
    {
        try {
            $this->kategoriaService->odstranit($vzdKnzncKategoriaId);
            flash()->success('Kategória bola odstránená.')->important();
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

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

            return back();
        }

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