<?php

namespace Student\Student\Http\Controllers;

use App\Http\Controllers\AdminController as AdminController;
use Form;
use Student\Student\Http\Requests\KeyAdminRequest;
use Student\Student\Interfaces\KeyRepositoryInterface;
use Student\Student\Models\Key;

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

    /**
     * Display a list of key.
     *
     * @return Response
     */
    public function index(KeyAdminRequest $request)
    {
        $keys  = $this->repository->setPresenter('\\Student\\Student\\Repositories\\Presenter\\KeyListPresenter')
                                            ->scopeQuery(function($query){
                                                return $query->orderBy('id','DESC');
                                            })->paginate();
        $this   ->theme->prependTitle(trans('student::key.names').' :: ');
        $view   = $this->theme->of('student::admin.key.index')->render();

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

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

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

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

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

        Form::populate($key);

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

    }

    /**
     * Create new key.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(KeyAdminRequest $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->responseData = $key;
            $this->responseMeta = '';
            $this->responseRedirect = trans_url('/admin/student/key/'.$key->getRouteKey());
            $this->responseView = view('student::admin.key.create', compact('key'));

            return $this -> respond($request);

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

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

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

    /**
     * Update the key.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function update(KeyAdminRequest $request, Key $key)
    {
        try {

            $attributes = $request->all();

            $key->update($attributes);

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

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

    /**
     * Remove the key.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(KeyAdminRequest $request, Key $key)
    {

        try {

            $t = $key->delete();

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

            return $this -> respond($request);

        }
    }
}