<?php

namespace Article\Article\Http\Controllers;

use App\Http\Controllers\AdminController as AdminController;
use Form;
use Article\Article\Http\Requests\ArticleAdminRequest;
use Article\Article\Interfaces\ArticleRepositoryInterface;
use Article\Article\Models\Article;

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

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

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

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

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

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

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

        Form::populate($article);

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

    }

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

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

            return $this -> respond($request);

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

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

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

    /**
     * Update the article.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function update(ArticleAdminRequest $request, Article $article)
    {
        try {

            $attributes = $request->all();

            $article->update($attributes);

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

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

    /**
     * Remove the article.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(ArticleAdminRequest $request, Article $article)
    {

        try {

            $t = $article->delete();

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

            return $this -> respond($request);

        }
    }
}