<?php

namespace Student\Student\Http\Controllers;

use App\Http\Controllers\UserController as UserController;
use Form;
use Student\Student\Http\Requests\KeyUserRequest;
use Student\Student\Interfaces\KeyRepositoryInterface;
use Student\Student\Models\Key;

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

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

        $this->theme->prependTitle(trans('student::key.names').' :: ');

        return $this->theme->of('student::user.key.index', compact('keys'))->render();
    }

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

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

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

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

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

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

            $this->responseCode = 201;
            $this->responseMessage = trans('messages.success.created', ['Module' => trans('student::key.name')]);
            $this->responseRedirect = trans_url('/user/student/key');
            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 Key $key
     *
     * @return Response
     */
    public function edit(KeyUserRequest $request, Key $key)
    {

        Form::populate($key);

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

    /**
     * Update the specified resource.
     *
     * @param Request $request
     * @param Key $key
     *
     * @return Response
     */
    public function update(KeyUserRequest $request, Key $key)
    {
        try {
            $this->repository->update($request->all(), $key->getRouteKey());
            $this->responseCode = 204;
            $this->responseMessage = trans('messages.success.updated', ['Module' => trans('student::key.name')]);
            $this->responseRedirect = trans_url('/user/student/key');
            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(KeyUserRequest $request, Key $key)
    {
        try {
            $this->repository->delete($key->getRouteKey());
            $this->responseCode = 204;
            $this->responseMessage = trans('messages.success.deleted', ['Module' => trans('student::key.name')]);
            $this->responseRedirect = trans_url('/user/student/key');
            return $this -> respond($request);
        } catch (Exception $e) {

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

        }
    }
}