<?php

namespace Bixo\Description\Http\Controllers;

use App\Http\Controllers\APIController as BaseController;
use Bixo\Description\Http\Requests\DescriptionRequest;
use Bixo\Description\Interfaces\DescriptionRepositoryInterface;
use Bixo\Description\Models\Description;
use Bixo\Description\Forms\Description as Form;

/**
 * APIController  class for description.
 */
class DescriptionAPIController extends BaseController
{

    /**
     * Initialize description resource controller.
     *
     * @param type DescriptionRepositoryInterface $description
     *
     * @return null
     */
    public function __construct(DescriptionRepositoryInterface $description)
    {
        parent::__construct();
        $this->repository = $description;
        $this->repository
            ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class)
            ->pushCriteria(\Bixo\Description\Repositories\Criteria\DescriptionResourceCriteria::class);
    }

    /**
     * Display a list of description.
     *
     * @return Response
     */
    public function index(DescriptionRequest $request)
    {
        return $this->repository
            ->setPresenter(\Bixo\Description\Repositories\Presenter\DescriptionPresenter::class)
            ->paginate();
    }

    /**
     * Display description.
     *
     * @param Request $request
     * @param Model   $description
     *
     * @return Response
     */
    public function show(DescriptionRequest $request, Description $description)
    {
        return $description->setPresenter(\Bixo\Description\Repositories\Presenter\DescriptionListPresenter::class);
        ;
    }

    /**
     * Create new description.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(DescriptionRequest $request)
    {
        try {
            $data              = $request->all();
            $data['user_id']   = user_id();
            $data['user_type'] = user_type();
            $data              = $this->repository->create($data);
            $message           = trans('messages.success.created', ['Module' => trans('description::description.name')]);
            $code              = 204;
            $status            = 'success';
            $url               = guard_url('description/description/' . $description->getRouteKey());
        } catch (Exception $e) {
            $message = $e->getMessage();
            $code    = 400;
            $status  = 'error';
            $url     = guard_url('description/description');
        }
        return compact('data', 'message', 'code', 'status', 'url');
    }

    /**
     * Update the description.
     *
     * @param Request $request
     * @param Model   $description
     *
     * @return Response
     */
    public function update(DescriptionRequest $request, Description $description)
    {
        try {
            $data = $request->all();

            $description->update($data);
            $message = trans('messages.success.updated', ['Module' => trans('description::description.name')]);
            $code    = 204;
            $status  = 'success';
            $url     = guard_url('description/description/' . $description->getRouteKey());
        } catch (Exception $e) {
            $message = $e->getMessage();
            $code    = 400;
            $status  = 'error';
            $url     = guard_url('description/description/' . $description->getRouteKey());
        }
        return compact('data', 'message', 'code', 'status', 'url');
    }

    /**
     * Remove the description.
     *
     * @param Model   $description
     *
     * @return Response
     */
    public function destroy(DescriptionRequest $request, Description $description)
    {
        try {
            $description->delete();
            $message = trans('messages.success.deleted', ['Module' => trans('description::description.name')]);
            $code    = 202;
            $status  = 'success';
            $url     = guard_url('description/description/0');
        } catch (Exception $e) {
            $message = $e->getMessage();
            $code    = 400;
            $status  = 'error';
            $url     = guard_url('description/description/' . $description->getRouteKey());
        }
        return compact('message', 'code', 'status', 'url');
    }

    /**
     * Return the form elements as json.
     *
     * @param String   $element
     *
     * @return json
     */
    public function form($element = 'fields')
    {
        $form = new Form();
        return $form->form($element, true);
    }

}