<?php

namespace Thang.luu\Word\Http\Controllers;

use App\Http\Controllers\AdminController as AdminController;
use Form;
use Thang.luu\Word\Http\Requests\WordsAdminRequest;
use Thang.luu\Word\Interfaces\WordsRepositoryInterface;
use Thang.luu\Word\Models\Words;

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

    /**
     * Display a list of words.
     *
     * @return Response
     */
    public function index(WordsAdminRequest $request)
    {
        $words  = $this->repository->setPresenter('\\Thang.luu\\Word\\Repositories\\Presenter\\WordsListPresenter')
                                            ->scopeQuery(function($query){
                                                return $query->orderBy('id','DESC');
                                            })->paginate();
        $this   ->theme->prependTitle(trans('word::words.names').' :: ');
        $view   = $this->theme->of('word::admin.words.index')->render();

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

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

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

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

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

        Form::populate($words);

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

    }

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

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

            return $this -> respond($request);

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

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

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

    /**
     * Update the words.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function update(WordsAdminRequest $request, Words $words)
    {
        try {

            $attributes = $request->all();

            $words->update($attributes);

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

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

    /**
     * Remove the words.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(WordsAdminRequest $request, Words $words)
    {

        try {

            $t = $words->delete();

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

            return $this -> respond($request);

        }
    }
}