<?php

namespace Sahbaj\Sahbaj\Http\Controllers;

use App\Http\Controllers\AdminWebController as AdminController;
use Form;
use Sahbaj\Sahbaj\Http\Requests\SahbajAdminWebRequest;
use Sahbaj\Sahbaj\Interfaces\SahbajRepositoryInterface;
use Sahbaj\Sahbaj\Models\Sahbaj;

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

    /**
     * Display a list of sahbaj.
     *
     * @return Response
     */
    public function index(SahbajAdminWebRequest $request)
    {
        if ($request->wantsJson()) {
            $sahbajs  = $this->repository->setPresenter('\\Sahbaj\\Sahbaj\\Repositories\\Presenter\\SahbajListPresenter')
                                                ->scopeQuery(function($query){
                                                    return $query->orderBy('id','DESC');
                                                })->all();
            return response()->json($sahbajs, 200);

        }
        $this   ->theme->prependTitle(trans('sahbaj::sahbaj.names').' :: ');
        return $this->theme->of('sahbaj::admin.sahbaj.index')->render();
    }

    /**
     * Display sahbaj.
     *
     * @param Request $request
     * @param Model   $sahbaj
     *
     * @return Response
     */
    public function show(SahbajAdminWebRequest $request, Sahbaj $sahbaj)
    {
        if (!$sahbaj->exists) {
            return response()->view('sahbaj::admin.sahbaj.new', compact('sahbaj'));
        }

        Form::populate($sahbaj);
        return response()->view('sahbaj::admin.sahbaj.show', compact('sahbaj'));
    }

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

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

        Form::populate($sahbaj);

        return response()->view('sahbaj::admin.sahbaj.create', compact('sahbaj'));

    }

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

            return response()->json([
                'message'  => trans('messages.success.updated', ['Module' => trans('sahbaj::sahbaj.name')]),
                'code'     => 204,
                'redirect' => trans_url('/admin/sahbaj/sahbaj/'.$sahbaj->getRouteKey())
            ], 201);


        } catch (Exception $e) {
            return response()->json([
                'message'  => $e->getMessage(),
                'code'     => 400,
            ], 400);
        }
    }

    /**
     * Show sahbaj for editing.
     *
     * @param Request $request
     * @param Model   $sahbaj
     *
     * @return Response
     */
    public function edit(SahbajAdminWebRequest $request, Sahbaj $sahbaj)
    {
        Form::populate($sahbaj);
        return  response()->view('sahbaj::admin.sahbaj.edit', compact('sahbaj'));
    }

    /**
     * Update the sahbaj.
     *
     * @param Request $request
     * @param Model   $sahbaj
     *
     * @return Response
     */
    public function update(SahbajAdminWebRequest $request, Sahbaj $sahbaj)
    {
        try {

            $attributes = $request->all();

            $sahbaj->update($attributes);

            return response()->json([
                'message'  => trans('messages.success.updated', ['Module' => trans('sahbaj::sahbaj.name')]),
                'code'     => 204,
                'redirect' => trans_url('/admin/sahbaj/sahbaj/'.$sahbaj->getRouteKey())
            ], 201);

        } catch (Exception $e) {

            return response()->json([
                'message'  => $e->getMessage(),
                'code'     => 400,
                'redirect' => trans_url('/admin/sahbaj/sahbaj/'.$sahbaj->getRouteKey()),
            ], 400);

        }
    }

    /**
     * Remove the sahbaj.
     *
     * @param Model   $sahbaj
     *
     * @return Response
     */
    public function destroy(SahbajAdminWebRequest $request, Sahbaj $sahbaj)
    {

        try {

            $t = $sahbaj->delete();

            return response()->json([
                'message'  => trans('messages.success.deleted', ['Module' => trans('sahbaj::sahbaj.name')]),
                'code'     => 202,
                'redirect' => trans_url('/admin/sahbaj/sahbaj/0'),
            ], 202);

        } catch (Exception $e) {

            return response()->json([
                'message'  => $e->getMessage(),
                'code'     => 400,
                'redirect' => trans_url('/admin/sahbaj/sahbaj/'.$sahbaj->getRouteKey()),
            ], 400);
        }
    }
}