<?php

namespace Litematrimony\Preference\Http\Controllers;

use Exception;
use Litepie\Http\Controllers\ResourceController as BaseController;
use Litepie\Repository\Filter\RequestFilter;
use Litematrimony\Preference\Forms\Preference as PreferenceForm;
use Litematrimony\Preference\Http\Requests\PreferenceRequest;
use Litematrimony\Preference\Interfaces\PreferenceRepositoryInterface;
use Litematrimony\Preference\Repositories\Eloquent\Filters\PreferenceResourceFilter;
use Litematrimony\Preference\Repositories\Eloquent\Presenters\PreferenceListPresenter;

/**
 * Resource controller class for preference.
 */
class PreferenceResourceController extends BaseController
{

    /**
     * Initialize preference resource controller.
     *
     *
     * @return null
     */
    public function __construct(PreferenceRepositoryInterface $preference)
    {
        parent::__construct();
        $this->form = PreferenceForm::setAttributes()->toArray();
        $this->modules = $this->modules(config('litematrimony.preference.modules'), 'preference', guard_url('preference'));
        $this->repository = $preference;
    }

    /**
     * Display a list of preference.
     *
     * @return Response
     */
    public function index(PreferenceRequest $request)
    {

        $pageLimit = $request->input('pageLimit', config('database.pagination.limit'));
        $data = $this->repository
            ->pushFilter(RequestFilter::class)
            ->pushFilter(PreferenceResourceFilter::class)
            ->setPresenter(PreferenceListPresenter::class)
            ->simplePaginate($pageLimit)
            // ->withQueryString()
            ->toArray();

        extract($data);
        $form = $this->form;
        $modules = $this->modules;

        return $this->response->setMetaTitle(trans('preference::preference.names'))
            ->view('preference::preference.index')
            ->data(compact('data', 'meta', 'links', 'modules', 'form'))
            ->output();
    }

    /**
     * Display preference.
     *
     * @param Request $request
     * @param Model   $preference
     *
     * @return Response
     */
    public function show(PreferenceRequest $request, PreferenceRepositoryInterface $repository)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = $repository->toArray();
        return $this->response
            ->setMetaTitle(trans('app.view') . ' ' . trans('preference::preference.name'))
            ->data(compact('data', 'form', 'modules', 'form'))
            ->view('preference::preference.show')
            ->output();
    }

    /**
     * Show the form for creating a new preference.
     *
     * @param Request $request
     *x
     * @return Response
     */
    public function create(PreferenceRequest $request, PreferenceRepositoryInterface $repository)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = $repository->toArray();
        return $this->response->setMetaTitle(trans('app.new') . ' ' . trans('preference::preference.name'))
            ->view('preference::preference.create')
            ->data(compact('data', 'form', 'modules'))
            ->output();
    }

    /**
     * Create new preference.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(PreferenceRequest $request, PreferenceRepositoryInterface $repository)
    {
        try {
            $attributes = $request->all();
            $attributes['user_id'] = user_id();
            $attributes['user_type'] = user_type();
            $repository->create($attributes);
            $data = $repository->toArray();

            return $this->response->message(trans('messages.success.created', ['Module' => trans('preference::preference.name')]))
                ->code(204)
                ->data(compact('data'))
                ->status('success')
                ->url(guard_url('preference/preference/' . $data['id']))
                ->redirect();
        } catch (Exception $e) {
            return $this->response->message($e->getMessage())
                ->code(400)
                ->status('error')
                ->url(guard_url('/preference/preference'))
                ->redirect();
        }

    }

    /**
     * Show preference for editing.
     *
     * @param Request $request
     * @param Model   $preference
     *
     * @return Response
     */
    public function edit(PreferenceRequest $request, PreferenceRepositoryInterface $repository)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = $repository->toArray();

        return $this->response->setMetaTitle(trans('app.edit') . ' ' . trans('preference::preference.name'))
            ->view('preference::preference.edit')
            ->data(compact('data', 'form', 'modules'))
            ->output();
    }

    /**
     * Update the preference.
     *
     * @param Request $request
     * @param Model   $preference
     *
     * @return Response
     */
    public function update(PreferenceRequest $request, PreferenceRepositoryInterface $repository)
    {
        try {
            $attributes = $request->all();
            $repository->update($attributes);
            $data = $repository->toArray();

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

    }

    /**
     * Remove the preference.
     *
     * @param Model   $preference
     *
     * @return Response
     */
    public function destroy(PreferenceRequest $request, PreferenceRepositoryInterface $repository)
    {
        try {
            $repository->delete();
            $data = $repository->toArray();

            return $this->response->message(trans('messages.success.deleted', ['Module' => trans('preference::preference.name')]))
                ->code(202)
                ->status('success')
                ->data(compact('data'))
                ->url(guard_url('preference/preference/0'))
                ->redirect();

        } catch (Exception $e) {

            return $this->response->message($e->getMessage())
                ->code(400)
                ->status('error')
                ->url(guard_url('preference/preference/' .  $repository->getRouteKey()))
                ->redirect();
        }

    }
}