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

namespace Seka\Vzdelavanie\Http\Controllers;

use Exception;
use Seka\Vzdelavanie\Services\KategoriaServiceInterface;
use Seka\Vzdelavanie\Http\Requests\KategoriaStoreRequest;
use Seka\Vzdelavanie\Http\Requests\KategoriaUpdateRequest;

/**
 * Class KategoriaController
 * @package Seka\Vzdelavanie\Http\Controllers
 */
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.kategorie.index', compact('kategorie'));
        }
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('Vzdelavanie::pages.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) {
            flash()->error($e->getMessage())->important();

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

        return redirect(route('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) {
            flash()->error($e->getMessage())->important();
        }

        return view('Vzdelavanie::pages.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) {
            flash()->error($e->getMessage())->important();

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

        return redirect(route('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) {
            flash()->error($e->getMessage())->important();

            return back();
        }

        return redirect(route('kategoria.index'));
    }
}
