<?php

namespace Litecms\KitchenBlog\Http\Controllers;

use App\Http\Controllers\APIController as BaseController;
use Litecms\KitchenBlog\Http\Requests\KitchenCategoryRequest;
use Litecms\KitchenBlog\Interfaces\KitchenCategoryRepositoryInterface;
use Litecms\KitchenBlog\Models\KitchenCategory;
use Litecms\KitchenBlog\Forms\KitchenCategory as Form;

/**
 * APIController  class for kitchen_category.
 */
class KitchenCategoryAPIController extends BaseController
{

    /**
     * Initialize kitchen_category resource controller.
     *
     * @param type KitchenCategoryRepositoryInterface $kitchen_category
     *
     * @return null
     */
    public function __construct(KitchenCategoryRepositoryInterface $kitchen_category)
    {
        parent::__construct();
        $this->repository = $kitchen_category;
        $this->repository
            ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class)
            ->pushCriteria(\Litecms\KitchenBlog\Repositories\Criteria\KitchenCategoryResourceCriteria::class);
    }

    /**
     * Display a list of kitchen_category.
     *
     * @return Response
     */
    public function index(KitchenCategoryRequest $request)
    {
        return $this->repository
            ->setPresenter(\Litecms\KitchenBlog\Repositories\Presenter\KitchenCategoryPresenter::class)
            ->paginate();
    }

    /**
     * Display kitchen_category.
     *
     * @param Request $request
     * @param Model   $kitchen_category
     *
     * @return Response
     */
    public function show(KitchenCategoryRequest $request, KitchenCategory $kitchen_category)
    {
        return $kitchen_category->setPresenter(\Litecms\KitchenBlog\Repositories\Presenter\KitchenCategoryListPresenter::class);
        ;
    }

    /**
     * Create new kitchen_category.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(KitchenCategoryRequest $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('kitchen_blog::kitchen_category.name')]);
            $code              = 204;
            $status            = 'success';
            $url               = guard_url('kitchen_blog/kitchen_category/' . $kitchen_category->getRouteKey());
        } catch (Exception $e) {
            $message = $e->getMessage();
            $code    = 400;
            $status  = 'error';
            $url     = guard_url('kitchen_blog/kitchen_category');
        }
        return compact('data', 'message', 'code', 'status', 'url');
    }

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

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

    /**
     * Remove the kitchen_category.
     *
     * @param Model   $kitchen_category
     *
     * @return Response
     */
    public function destroy(KitchenCategoryRequest $request, KitchenCategory $kitchen_category)
    {
        try {
            $kitchen_category->delete();
            $message = trans('messages.success.deleted', ['Module' => trans('kitchen_blog::kitchen_category.name')]);
            $code    = 202;
            $status  = 'success';
            $url     = guard_url('kitchen_blog/kitchen_category/0');
        } catch (Exception $e) {
            $message = $e->getMessage();
            $code    = 400;
            $status  = 'error';
            $url     = guard_url('kitchen_blog/kitchen_category/' . $kitchen_category->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);
    }

}