<?php

namespace Tet\Test\Http\Controllers;

use App\Http\Controllers\AdminController as AdminController;
use Form;
use Tet\Test\Http\Requests\NewsAdminRequest;
use Tet\Test\Interfaces\NewsRepositoryInterface;
use Tet\Test\Models\News;

/**
 * Admin controller class.
 */
class NewsAdminController extends AdminController
{
    /**
     * Initialize news controller.
     *
     * @param type NewsRepositoryInterface $news
     *
     * @return type
     */
    public function __construct(NewsRepositoryInterface $news)
    {
        parent::__construct();
        $this->repository = $news;
    }

    /**
     * Display a list of news.
     *
     * @return Response
     */
    public function index(NewsAdminRequest $request)
    {
        $news  = $this->repository->setPresenter('\\Tet\\Test\\Repositories\\Presenter\\NewsListPresenter')
                                            ->scopeQuery(function($query){
                                                return $query->orderBy('id','DESC');
                                            })->paginate();
        $this   ->theme->prependTitle(trans('test::news.names').' :: ');
        $view   = $this->theme->of('test::admin.news.index')->render();

        $this->responseCode = 200;
        $this->responseMessage = trans('messages.success.loaded', ['Module' => trans('test::news.name')]);
        $this->responseData = $news['data'];
        $this->responseMeta = $news['meta'];
        $this->responseView = $view;
        $this->responseRedirect = '';
        return $this->respond($request);
    }

    /**
     * Display news.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function show(NewsAdminRequest $request, News $news)
    {
        if (!$news->exists) {
            $this->responseCode = 404;
            $this->responseMessage = trans('messages.success.notfound', ['Module' => trans('test::news.name')]);
            $this->responseView = view('test::admin.news.new');
            return $this -> respond($request);
        }

        Form::populate($news);
        $this->responseCode = 200;
        $this->responseMessage = trans('messages.success.loaded', ['Module' => trans('test::news.name')]);
        $this->responseData = $news;
        $this->responseView = view('test::admin.news.show', compact('news'));
        return $this -> respond($request);
    }

    /**
     * Show the form for creating a new news.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function create(NewsAdminRequest $request)
    {

        $news = $this->repository->newInstance([]);

        Form::populate($news);

        $this->responseCode = 200;
        $this->responseMessage = trans('messages.success.loaded', ['Module' => trans('test::news.name')]);
        $this->responseData = $news;
        $this->responseView = view('test::admin.news.create', compact('news'));
        return $this -> respond($request);

    }

    /**
     * Create new news.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(NewsAdminRequest $request)
    {
        try {
            $attributes = $request->all();
            $attributes['user_id'] = user_id();
            $news = $this->repository->create($attributes);

            $this->responseCode = 201;
            $this->responseMessage = trans('messages.success.created', ['Module' => trans('test::news.name')]);
            $this->responseData = $news;
            $this->responseMeta = '';
            $this->responseRedirect = trans_url('/admin/test/news/'.$news->getRouteKey());
            $this->responseView = view('test::admin.news.create', compact('news'));

            return $this -> respond($request);

        } catch (Exception $e) {
            $this->responseCode = 400;
            $this->responseMessage = $e->getMessage();
            return $this -> respond($request);
        }
    }

    /**
     * Show news for editing.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function edit(NewsAdminRequest $request, News $news)
    {
        Form::populate($news);
        $this->responseCode = 200;
        $this->responseMessage = trans('messages.success.loaded', ['Module' => trans('test::news.name')]);
        $this->responseData = $news;
        $this->responseView = view('test::admin.news.edit', compact('news'));

        return $this -> respond($request);
    }

    /**
     * Update the news.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function update(NewsAdminRequest $request, News $news)
    {
        try {

            $attributes = $request->all();

            $news->update($attributes);

            $this->responseCode = 204;
            $this->responseMessage = trans('messages.success.updated', ['Module' => trans('test::news.name')]);
            $this->responseData = $news;
            $this->responseRedirect = trans_url('/admin/test/news/'.$news->getRouteKey());

            return $this -> respond($request);

        } catch (Exception $e) {

            $this->responseCode = 400;
            $this->responseMessage = $e->getMessage();
            $this->responseRedirect = trans_url('/admin/test/news/'.$news->getRouteKey());

            return $this -> respond($request);
        }
    }

    /**
     * Remove the news.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(NewsAdminRequest $request, News $news)
    {

        try {

            $t = $news->delete();

            $this->responseCode = 202;
            $this->responseMessage = trans('messages.success.deleted', ['Module' => trans('test::news.name')]);
            $this->responseRedirect = trans_url('/admin/test/news/0');

            return $this -> respond($request);

        } catch (Exception $e) {

            $this->responseCode = 400;
            $this->responseMessage = $e->getMessage();
            $this->responseRedirect = trans_url('/admin/test/news/'.$news->getRouteKey());

            return $this -> respond($request);

        }
    }
}