<?php

namespace SharzonTest\SharzonTest\Http\Controllers;

use App\Http\Controllers\UserController as BaseController;
use Form;
use SharzonTest\SharzonTest\Http\Requests\SharzonTestRequest;
use SharzonTest\SharzonTest\Interfaces\SharzonTestRepositoryInterface;
use SharzonTest\SharzonTest\Models\SharzonTest;

class SharzonTestUserController extends BaseController
{
    /**
     * Initialize sharzon_test controller.
     *
     * @param type SharzonTestRepositoryInterface $sharzon_test
     *
     * @return type
     */
    public function __construct(SharzonTestRepositoryInterface $sharzon_test)
    {
        $this->repository = $sharzon_test;
        $this->repository
                ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria'))
                ->pushCriteria(new \SharzonTest\SharzonTest\Repositories\Criteria\SharzonTestUserCriteria());
        parent::__construct();
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(SharzonTestRequest $request)
    {
        $sharzon_tests = $this->repository->scopeQuery(function($query){
            return $query->orderBy('id','DESC');
        })->paginate();

        $this->theme->prependTitle(trans('sharzon-test::sharzon_test.names'));

        return $this->theme->of('sharzon-test::user.sharzon_test.index', compact('sharzon_tests'))->render();
    }

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

        return $this->theme->of('sharzon-test::user.sharzon_test.show', compact('sharzon_test'))->render();
    }

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

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

        $this->theme->prependTitle(trans('sharzon-test::sharzon_test.names'));
        return $this->theme->of('sharzon-test::user.sharzon_test.create', compact('sharzon_test'))->render();
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(SharzonTestRequest $request)
    {
        try {
            $attributes = $request->all();
            $attributes['user_id'] = user_id();
            $sharzon_test = $this->repository->create($attributes);

            return redirect(trans_url('/user/sharzon-test/sharzon_test'))
                -> with('message', trans('messages.success.created', ['Module' => trans('sharzon-test::sharzon_test.name')]))
                -> with('code', 201);

        } 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 SharzonTest $sharzon_test
     *
     * @return Response
     */
    public function edit(SharzonTestRequest $request, SharzonTest $sharzon_test)
    {

        Form::populate($sharzon_test);
        $this->theme->prependTitle(trans('sharzon-test::sharzon_test.names'));

        return $this->theme->of('sharzon-test::user.sharzon_test.edit', compact('sharzon_test'))->render();
    }

    /**
     * Update the specified resource.
     *
     * @param Request $request
     * @param SharzonTest $sharzon_test
     *
     * @return Response
     */
    public function update(SharzonTestRequest $request, SharzonTest $sharzon_test)
    {
        try {
            $this->repository->update($request->all(), $sharzon_test->getRouteKey());

            return redirect(trans_url('/user/sharzon-test/sharzon_test'))
                ->with('message', trans('messages.success.updated', ['Module' => trans('sharzon-test::sharzon_test.name')]))
                ->with('code', 204);

        } 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(SharzonTestRequest $request, SharzonTest $sharzon_test)
    {
        try {
            $this->repository->delete($sharzon_test->getRouteKey());
            return redirect(trans_url('/user/sharzon-test/sharzon_test'))
                ->with('message', trans('messages.success.deleted', ['Module' => trans('sharzon-test::sharzon_test.name')]))
                ->with('code', 204);

        } catch (Exception $e) {

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

        }
    }
}