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

namespace Seka\Vzdelavanie\Http\Controllers\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\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
     */
    public function index()
    {
        $kategorie = null;

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

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('Vzdelavanie::pages.kniznica.kategorie.nova');
    }

    /**
     * @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.kniznica.kategoria.index'));
    }

    /**
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(int $id)
    {
        try {
            $kategoria = $this->kategoriaService->vratPodlaId($id);
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

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

        return view('Vzdelavanie::pages.kniznica.kategorie.upravit', compact('kategoria'));
    }

    /**
     * @param \Seka\Vzdelavanie\Http\Requests\KategoriaUpdateRequest $updateRequest
     * @param int                                                    $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(KategoriaUpdateRequest $updateRequest, int $id)
    {
        try {
            $this->kategoriaService->upravit($id, $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.kniznica.kategoria.ucebnice.index', ['kategoriaId' => $id]));
    }

    /**
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy(int $id)
    {
        try {
            $this->kategoriaService->odstranit($id);
            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.kniznica.kategoria.index'));
    }
}
