<?php

namespace Seka\Vzdelavanie\Http\Controllers\Kniznica;

use Exception;
use Illuminate\Support\Facades\Log;
use Seka\Vzdelavanie\Http\Controllers\Controller;
use Seka\Vzdelavanie\Services\SekciaServiceInterface;
use Seka\Vzdelavanie\Services\UcebnicaServiceInterface;
use Seka\Vzdelavanie\Services\KategoriaServiceInterface;
use Seka\Vzdelavanie\Http\Requests\UcebnicaStoreRequest;
use Seka\Vzdelavanie\Http\Requests\UcebnicaUpdateRequest;

/**
 * Class UcebnicaController
 * @package Seka\Vzdelavanie\Http\Controllers\Kniznica
 */
class UcebnicaController extends Controller
{
    /**
     * @var \Seka\Vzdelavanie\Services\SekciaServiceInterface
     */
    protected $sekciaService;

    /**
     * @var \Seka\Vzdelavanie\Services\UcebnicaServiceInterface
     */
    protected $ucebnicaService;

    /**
     * @var \Seka\Vzdelavanie\Services\KategoriaServiceInterface
     */
    protected $kategoriaService;

    /**
     * UcebnicaController constructor.
     * @param \Seka\Vzdelavanie\Services\SekciaServiceInterface    $sekciaService
     * @param \Seka\Vzdelavanie\Services\UcebnicaServiceInterface  $ucebnicaService
     * @param \Seka\Vzdelavanie\Services\KategoriaServiceInterface $kategoriaService
     */
    public function __construct(
        SekciaServiceInterface $sekciaService,
        UcebnicaServiceInterface $ucebnicaService,
        KategoriaServiceInterface $kategoriaService
    )
    {
        $this->sekciaService = $sekciaService;
        $this->ucebnicaService = $ucebnicaService;
        $this->kategoriaService = $kategoriaService;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $kategorie = null;

        try {
            $kategorie = $this->kategoriaService->vratVsetky();
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error($e->getMessage())->important();
        } finally {
            return view('Vzdelavanie::pages.kniznica.ucebne_materialy.novy', compact('kategorie'));
        }
    }

    /**
     * @param \Seka\Vzdelavanie\Http\Requests\UcebnicaStoreRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(UcebnicaStoreRequest $request)
    {
        try {
            $ucebnica = $this->ucebnicaService->vytvorit(
                $request->all(['nazov', 'kategoria', 'autor', 'popis', 'verejne', 'obrazok'])
            );
            flash()->success('Učebný materiál bol vytvorený.')->important();
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

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

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

        return redirect(route('vzdelavanie.kniznica.ucebnica.edit', ['id' => $ucebnica->getKey()]));
    }

    /**
     * @param int $ucebnyTextId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(int $ucebnyTextId)
    {
        $ucebnyText = null;
        $sekcie = null;

        try {
            $ucebnyText = $this->ucebnicaService->vratUcebnyText($ucebnyTextId);
            $sekcie = $this->sekciaService->vratZoznamSekciiUcebnehoTextu($ucebnyTextId, true);
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error($e->getMessage())->important();
        } finally {
            return view('Vzdelavanie::pages.kniznica.ucebne_materialy.nahlad', compact('ucebnyText', 'sekcie'));
        }
    }

    /**
     * @param int $ucebnyTextId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(int $ucebnyTextId)
    {
        $kategorie = null;
        $ucebnica = null;
        $sekcie = null;

        try {
            $kategorie = $this->kategoriaService->vratVsetky();
            $ucebnica = $this->ucebnicaService->vratUcebnyText($ucebnyTextId);
            $sekcie = $this->sekciaService->vratZoznamSekciiUcebnehoTextu($ucebnyTextId, false);
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error($e->getMessage())->important();
        } finally {
            return view(
                'Vzdelavanie::pages.kniznica.ucebne_materialy.upravit',
                compact('ucebnica', 'sekcie', 'kategorie')
            );
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Seka\Vzdelavanie\Http\Requests\UcebnicaUpdateRequest $request
     * @param int                                                   $ucebnyTextId
     *
     * @return \Illuminate\Http\Response
     */
    public function update(UcebnicaUpdateRequest $request, int $ucebnyTextId)
    {
        try {
            $this->ucebnicaService->upravit(
                $ucebnyTextId,
                $request->all(['nazov', 'kategoria', 'autor', 'popis', 'verejne', 'obrazok'])
            );
            flash()->success('Učebný materiál bol upravený.')->important();
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

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

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

        return redirect(
            route('vzdelavanie.kniznica.kategoria.ucebnice.index', ['kategoriaId' => $request->get('kategoria')])
        )->with('oznacZmenu', $ucebnyTextId);
    }

    /**
     * Remove the specified resource from storage.
     * @param  int  $ucebnyTextId
     * @return \Illuminate\Http\Response
     */
    public function destroy(int $ucebnyTextId)
    {
        try {
            $kategoriaId = $this->ucebnicaService->vratUcebnyText($ucebnyTextId)->getAttribute('kategoria_id');
            $this->ucebnicaService->odstranit($ucebnyTextId);
            flash()->success('Učebný materiál bol 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.ucebnice.index', compact('kategoriaId'));
    }
}
