<?php

namespace Litematrimony\Profile\Http\Controllers;

use App\Http\Controllers\ResourceController as BaseController;
use Form;
use Litematrimony\Profile\Http\Requests\ShortlistRequest;
use Litematrimony\Profile\Interfaces\ShortlistRepositoryInterface;
use Litematrimony\Profile\Models\Shortlist;

/**
 * Resource controller class for shortlist.
 */
class ShortlistResourceController extends BaseController
{

    /**
     * Initialize shortlist resource controller.
     *
     * @param type ShortlistRepositoryInterface $shortlist
     *
     * @return null
     */
    public function __construct(ShortlistRepositoryInterface $shortlist)
    {
        parent::__construct();
        $this->repository = $shortlist;
        $this->repository
            ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class)
            ->pushCriteria(\Litematrimony\Profile\Repositories\Criteria\ShortlistResourceCriteria::class);
    }

    /**
     * Display a list of shortlist.
     *
     * @return Response
     */
    public function index(ShortlistRequest $request)
    {
        $view = $this->response->theme->listView();

        if ($this->response->typeIs('json')) {
            $function = camel_case('get-' . $view);
            return $this->repository
                ->setPresenter(\Litematrimony\Profile\Repositories\Presenter\ShortlistPresenter::class)
                ->$function();
        }

        $shortlists = $this->repository->paginate();

        return $this->response->title(trans('profile::shortlist.names'))
            ->view('profile::shortlist.index', true)
            ->data(compact('shortlists'))
            ->output();
    }

    /**
     * Display shortlist.
     *
     * @param Request $request
     * @param Model   $shortlist
     *
     * @return Response
     */
    public function show(ShortlistRequest $request, Shortlist $shortlist)
    {

        if ($shortlist->exists) {
            $view = 'profile::shortlist.show';
        } else {
            $view = 'profile::shortlist.new';
        }

        return $this->response->title(trans('app.view') . ' ' . trans('profile::shortlist.name'))
            ->data(compact('shortlist'))
            ->view($view, true)
            ->output();
    }

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

        $shortlist = $this->repository->newInstance([]);
        return $this->response->title(trans('app.new') . ' ' . trans('profile::shortlist.name')) 
            ->view('profile::shortlist.create', true) 
            ->data(compact('shortlist'))
            ->output();
    }

    /**
     * Create new shortlist.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(ShortlistRequest $request)
    {
        try {
            $attributes              = $request->all();
            $attributes['user_id']   = user_id();
            $attributes['user_type'] = user_type();
            $shortlist                 = $this->repository->create($attributes);

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

    }

    /**
     * Show shortlist for editing.
     *
     * @param Request $request
     * @param Model   $shortlist
     *
     * @return Response
     */
    public function edit(ShortlistRequest $request, Shortlist $shortlist)
    {
        return $this->response->title(trans('app.edit') . ' ' . trans('profile::shortlist.name'))
            ->view('profile::shortlist.edit', true)
            ->data(compact('shortlist'))
            ->output();
    }

    /**
     * Update the shortlist.
     *
     * @param Request $request
     * @param Model   $shortlist
     *
     * @return Response
     */
    public function update(ShortlistRequest $request, Shortlist $shortlist)
    {
        try {
            $attributes = $request->all();

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

    }

    /**
     * Remove the shortlist.
     *
     * @param Model   $shortlist
     *
     * @return Response
     */
    public function destroy(ShortlistRequest $request, Shortlist $shortlist)
    {
        try {

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

        } catch (Exception $e) {

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

    }

    /**
     * Remove multiple shortlist.
     *
     * @param Model   $shortlist
     *
     * @return Response
     */
    public function delete(ShortlistRequest $request, $type)
    {
        try {
            $ids = hashids_decode($request->input('ids'));

            if ($type == 'purge') {
                $this->repository->purge($ids);
            } else {
                $this->repository->delete($ids);
            }

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

        } catch (Exception $e) {

            return $this->response->message($e->getMessage())
                ->status("error")
                ->code(400)
                ->url(guard_url('/profile/shortlist'))
                ->redirect();
        }

    }

    /**
     * Restore deleted shortlists.
     *
     * @param Model   $shortlist
     *
     * @return Response
     */
    public function restore(ShortlistRequest $request)
    {
        try {
            $ids = hashids_decode($request->input('ids'));
            $this->repository->restore($ids);

            return $this->response->message(trans('messages.success.restore', ['Module' => trans('profile::shortlist.name')]))
                ->status("success")
                ->code(202)
                ->url(guard_url('/profile/shortlist'))
                ->redirect();

        } catch (Exception $e) {

            return $this->response->message($e->getMessage())
                ->status("error")
                ->code(400)
                ->url(guard_url('/profile/shortlist/'))
                ->redirect();
        }

    }

}