<?php

namespace S\\Http\Controllers;

use App\Http\Controllers\UserController as UserController;
use Form;
use S\\Http\Requests\WwwUserRequest;
use S\\Interfaces\WwwRepositoryInterface;
use S\\Models\Www;

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

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

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

        return $this->theme->of('::user.www.index', compact('wwws'))->render();
    }

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

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

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

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

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

    /**
     * Display the specified resource.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(WwwUserRequest $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->responseRedirect = trans_url('/user//www');
            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 Www $www
     *
     * @return Response
     */
    public function edit(WwwUserRequest $request, Www $www)
    {

        Form::populate($www);

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

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

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

        }
    }
}