<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateContentRequest;
use App\Http\Requests\UpdateContentRequest;
use App\Repositories\ContentRepository;
use App\Http\Controllers\AppBaseController;
use App\Models\Lesson;
use Illuminate\Http\Request;
use Laracasts\Flash\Flash;
use Response;

class ContentController extends AppBaseController
{
    /** @var ContentRepository $contentRepository*/
    private $contentRepository;

    public function __construct(ContentRepository $contentRepo)
    {
        $this->contentRepository = $contentRepo;
    }

    /**
     * Display a listing of the Content.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $contents = $this->contentRepository->paginate(10);

        return view('admin.contents.index')
            ->with('contents', $contents);
    }

    /**
     * Show the form for creating a new Content.
     *
     * @return Response
     */
    public function create()
    {
        $lessons = Lesson::all()->pluck("title", "id");
        return view('admin.contents.create', ["lessons" => $lessons]);
    }

    /**
     * Store a newly created Content in storage.
     *
     * @param CreateContentRequest $request
     *
     * @return Response
     */
    public function store(CreateContentRequest $request)
    {
        $input = $request->all();

        $content = $this->contentRepository->create($input);

        Flash::success('Content saved successfully.');

        return redirect(route('admin.contents.index'));
    }

    /**
     * Display the specified Content.
     *
     * @param int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $content = $this->contentRepository->find($id);

        if (empty($content)) {
            Flash::error('Content not found');

            return redirect(route('admin.contents.index'));
        }

        return view('admin.contents.show')->with('content', $content);
    }

    /**
     * Show the form for editing the specified Content.
     *
     * @param int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $content = $this->contentRepository->find($id);
        $lessons = Lesson::all()->pluck("title", "id");

        if (empty($content)) {
            Flash::error('Content not found');

            return redirect(route('admin.contents.index'));
        }

        return view('admin.contents.edit')->with('content', $content)->with('lessons', $lessons);
    }

    /**
     * Update the specified Content in storage.
     *
     * @param int $id
     * @param UpdateContentRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateContentRequest $request)
    {
        $content = $this->contentRepository->find($id);

        if (empty($content)) {
            Flash::error('Content not found');

            return redirect(route('admin.contents.index'));
        }

        $content = $this->contentRepository->update($request->all(), $id);

        Flash::success('Content updated successfully.');

        return redirect(route('admin.contents.index'));
    }

    /**
     * Remove the specified Content from storage.
     *
     * @param int $id
     *
     * @throws \Exception
     *
     * @return Response
     */
    public function destroy($id)
    {
        $content = $this->contentRepository->find($id);

        if (empty($content)) {
            Flash::error('Content not found');

            return redirect(route('admin.contents.index'));
        }

        $this->contentRepository->delete($id);

        Flash::success('Content deleted successfully.');

        return redirect(route('admin.contents.index'));
    }
}
