<?php

namespace Fegerer\AppTest\Http\Controllers;

use App\Http\Controllers\UserController as UserController;
use Form;
use Fegerer\AppTest\Http\Requests\ActionUserRequest;
use Fegerer\AppTest\Interfaces\ActionRepositoryInterface;
use Fegerer\AppTest\Models\Action;

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

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(ActionUserRequest $request)
    {
        $this->repository->pushCriteria(new \Lavalite\AppTest\Repositories\Criteria\ActionUserCriteria());
        $actions = $this->repository->scopeQuery(function($query){
            return $query->orderBy('id','DESC');
        })->paginate();

        $this->theme->prependTitle(trans('app_test::action.names').' :: ');

        return $this->theme->of('app_test::user.action.index', compact('actions'))->render();
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     * @param Action $action
     *
     * @return Response
     */
    public function show(ActionUserRequest $request, Action $action)
    {
        Form::populate($action);

        return $this->theme->of('app_test::user.action.show', compact('action'))->render();
    }

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

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

        return $this->theme->of('app_test::user.action.create', compact('action'))->render();
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(ActionUserRequest $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->responseRedirect = trans_url('/user/app_test/action');
            return $this -> respond($request);
        } catch (Exception $e) {
            redirect()->back()->withInput()->with('message', $e->getMessage())->with('code', 400);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Request $request
     * @param Action $action
     *
     * @return Response
     */
    public function edit(ActionUserRequest $request, Action $action)
    {

        Form::populate($action);

        return $this->theme->of('app_test::user.action.edit', compact('action'))->render();
    }

    /**
     * Update the specified resource.
     *
     * @param Request $request
     * @param Action $action
     *
     * @return Response
     */
    public function update(ActionUserRequest $request, Action $action)
    {
        try {
            $this->repository->update($request->all(), $action->getRouteKey());
            $this->responseCode = 204;
            $this->responseMessage = trans('messages.success.updated', ['Module' => trans('app_test::action.name')]);
            $this->responseRedirect = trans_url('/user/app_test/action');
            return $this -> respond($request);
        } catch (Exception $e) {
            redirect()->back()->withInput()->with('message', $e->getMessage())->with('code', 400);
        }
    }

    /**
     * Remove the specified resource.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(ActionUserRequest $request, Action $action)
    {
        try {
            $this->repository->delete($action->getRouteKey());
            $this->responseCode = 204;
            $this->responseMessage = trans('messages.success.deleted', ['Module' => trans('app_test::action.name')]);
            $this->responseRedirect = trans_url('/user/app_test/action');
            return $this -> respond($request);
        } catch (Exception $e) {

            redirect()->back()->withInput()->with('message', $e->getMessage())->with('code', 400);

        }
    }
}