<?php

namespace Cushbu\Artwork\Http\Controllers;

use App\Http\Controllers\UserWebController as UserController;
use Form;
use Cushbu\Artwork\Http\Requests\ArtworkUserWebRequest;
use Cushbu\Artwork\Interfaces\ArtworkRepositoryInterface;
use Cushbu\Artwork\Models\Artwork;

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

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

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

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

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

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

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

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

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

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

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

        Form::populate($artwork);

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

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

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

        } catch (Exception $e) {

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

        }
    }
}