<?php

namespace Agency\Contact\Http\Controllers;

use App\Http\Controllers\APIController as BaseController;
use Agency\Contact\Http\Requests\ContactRequest;
use Agency\Contact\Interfaces\ContactRepositoryInterface;
use Agency\Contact\Models\Contact;
use Agency\Contact\Forms\Contact as Form;

/**
 * APIController  class for contact.
 */
class ContactAPIController extends BaseController
{

    /**
     * Initialize contact resource controller.
     *
     * @param type ContactRepositoryInterface $contact
     *
     * @return null
     */
    public function __construct(ContactRepositoryInterface $contact)
    {
        parent::__construct();
        $this->repository = $contact;
        $this->repository
            ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class)
            ->pushCriteria(\Agency\Contact\Repositories\Criteria\ContactResourceCriteria::class);
    }

    /**
     * Display a list of contact.
     *
     * @return Response
     */
    public function index(ContactRequest $request)
    {
        return $this->repository
            ->setPresenter(\Agency\Contact\Repositories\Presenter\ContactPresenter::class)
            ->paginate();
    }

    /**
     * Display contact.
     *
     * @param Request $request
     * @param Model   $contact
     *
     * @return Response
     */
    public function show(ContactRequest $request, Contact $contact)
    {
        return $contact->setPresenter(\Agency\Contact\Repositories\Presenter\ContactListPresenter::class);
        ;
    }

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

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

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

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

}