<?php

namespace Fegerer\AppTest\Http\Controllers;

use App\Http\Controllers\AdminController as AdminController;
use Form;
use Fegerer\AppTest\Http\Requests\ActionAdminRequest;
use Fegerer\AppTest\Interfaces\ActionRepositoryInterface;
use Fegerer\AppTest\Models\Action;

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

    /**
     * Display a list of action.
     *
     * @return Response
     */
    public function index(ActionAdminRequest $request)
    {
        $actions  = $this->repository->setPresenter('\\Fegerer\\AppTest\\Repositories\\Presenter\\ActionListPresenter')
                                            ->scopeQuery(function($query){
                                                return $query->orderBy('id','DESC');
                                            })->paginate();
        $this   ->theme->prependTitle(trans('app_test::action.names').' :: ');
        $view   = $this->theme->of('app_test::admin.action.index')->render();

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

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

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

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

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

        Form::populate($action);

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

    }

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

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

            return $this -> respond($request);

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

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

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

    /**
     * Update the action.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function update(ActionAdminRequest $request, Action $action)
    {
        try {

            $attributes = $request->all();

            $action->update($attributes);

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

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

    /**
     * Remove the action.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(ActionAdminRequest $request, Action $action)
    {

        try {

            $t = $action->delete();

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

            return $this -> respond($request);

        }
    }
}