<?php

namespace Litecms\Representative\Http\Controllers;

use App\Http\Controllers\ResourceController as BaseController;
use Form;
use Litecms\Representative\Http\Requests\RepresentativesRequest;
use Litecms\Representative\Interfaces\RepresentativesRepositoryInterface;
use Litecms\Representative\Models\Representatives;

/**
 * Resource controller class for representatives.
 */
class RepresentativesResourceController extends BaseController
{

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

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

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

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

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

    /**
     * Display representatives.
     *
     * @param Request $request
     * @param Model   $representatives
     *
     * @return Response
     */
    public function show(RepresentativesRequest $request, Representatives $representatives)
    {

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

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

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

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

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

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

    }

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

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

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

    }

    /**
     * Remove the representatives.
     *
     * @param Model   $representatives
     *
     * @return Response
     */
    public function destroy(RepresentativesRequest $request, Representatives $representatives)
    {
        try {

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

        } catch (Exception $e) {

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

    }

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

        } catch (Exception $e) {

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

    }

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

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

        } catch (Exception $e) {

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

    }

}