<?php

namespace S\\Http\Controllers;

use App\Http\Controllers\AdminController as AdminController;
use Form;
use S\\Http\Requests\WwwAdminRequest;
use S\\Interfaces\WwwRepositoryInterface;
use S\\Models\Www;

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

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

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

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

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

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

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

        Form::populate($www);

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

    }

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

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

            return $this -> respond($request);

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

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

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

    /**
     * Update the www.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function update(WwwAdminRequest $request, Www $www)
    {
        try {

            $attributes = $request->all();

            $www->update($attributes);

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

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

    /**
     * Remove the www.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(WwwAdminRequest $request, Www $www)
    {

        try {

            $t = $www->delete();

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

            return $this -> respond($request);

        } catch (Exception $e) {

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

            return $this -> respond($request);

        }
    }
}