<?php

namespace Roshan\Roshan\Http\Controllers;

use App\Http\Controllers\UserWebController as UserController;
use Form;
use Roshan\Roshan\Http\Requests\RoshanUserWebRequest;
use Roshan\Roshan\Interfaces\RoshanRepositoryInterface;
use Roshan\Roshan\Models\Roshan;

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

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

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

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

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

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

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

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

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

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

            return redirect(trans_url('/user/roshan/roshan'))
                -> with('message', trans('messages.success.created', ['Module' => trans('roshan::roshan.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 Roshan $roshan
     *
     * @return Response
     */
    public function edit(RoshanUserWebRequest $request, Roshan $roshan)
    {

        Form::populate($roshan);

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

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

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

        } catch (Exception $e) {

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

        }
    }
}