<?php

namespace Tag\Tag\Http\Controllers;

use App\Http\Controllers\UserController as UserController;
use Form;
use Tag\Tag\Http\Requests\TagUserRequest;
use Tag\Tag\Interfaces\TagRepositoryInterface;
use Tag\Tag\Models\Tag;

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

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

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

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

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

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

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

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

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

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

            $this->responseCode = 201;
            $this->responseMessage = trans('messages.success.created', ['Module' => trans('tag::tag.name')]);
            $this->responseRedirect = trans_url('/user/tag/tag');
            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 Tag $tag
     *
     * @return Response
     */
    public function edit(TagUserRequest $request, Tag $tag)
    {

        Form::populate($tag);

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

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

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

        }
    }
}