<?php

namespace Lavalite\Location\Http\Controllers;

use App\Http\Controllers\ResourceController as BaseController;
use Form;
use Lavalite\Location\Http\Requests\LocationRequest;
use Lavalite\Location\Interfaces\LocationRepositoryInterface;
use Lavalite\Location\Models\Location;

/**
 * Resource controller class for location.
 */
class LocationResourceController extends BaseController
{

    /**
     * Initialize location resource controller.
     *
     * @param type LocationRepositoryInterface $location
     *
     * @return null
     */
    public function __construct(LocationRepositoryInterface $location)
    {
        parent::__construct();
        $this->repository = $location;
        $this->repository
            ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class)
            ->pushCriteria(\Lavalite\Location\Repositories\Criteria\LocationResourceCriteria::class);
    }

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

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

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

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

    /**
     * Display location.
     *
     * @param Request $request
     * @param Model   $location
     *
     * @return Response
     */
    public function show(LocationRequest $request, Location $location)
    {

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

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

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

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

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

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

    }

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

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

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

    }

    /**
     * Remove the location.
     *
     * @param Model   $location
     *
     * @return Response
     */
    public function destroy(LocationRequest $request, Location $location)
    {
        try {

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

        } catch (Exception $e) {

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

    }

    /**
     * Remove multiple location.
     *
     * @param Model   $location
     *
     * @return Response
     */
    public function delete(LocationRequest $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('location::location.name')]))
                ->status("success")
                ->code(202)
                ->url(guard_url('location/location'))
                ->redirect();

        } catch (Exception $e) {

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

    }

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

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

        } catch (Exception $e) {

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

    }

}