<?php

namespace Crm\Brand\Http\Controllers;

use Exception;
use Litepie\Http\Controllers\ResourceController as BaseController;
use Crm\Brand\Forms\Brand as BrandForm;
use Crm\Brand\Http\Requests\BrandResourceRequest;
use Crm\Brand\Http\Resources\BrandResource;
use Crm\Brand\Http\Resources\BrandsCollection;
use Crm\Brand\Models\Brand;
use Crm\Brand\Actions\BrandAction;
use Crm\Brand\Actions\BrandActions;


/**
 * Resource controller class for brand.
 */
class BrandResourceController extends BaseController
{

    /**
     * Initialize brand resource controller.
     *
     *
     * @return null
     */
    public function __construct()
    {
        parent::__construct();
        $this->form = BrandForm::grouped(false)
                        ->setAttributes()
                        ->toArray();
        $this->modules = $this->modules(config('crm.brand.modules'), 'brand', guard_url('brand'));
    }

    /**
     * Display a list of brand.
     *
     * @return Response
     */
    public function index(BrandResourceRequest $request)
    {
        $request = $request->all();
        $page = BrandActions::run('paginate', $request);

        $data = new BrandsCollection($page);

        $form = $this->form;
        $modules = $this->modules;

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

    }

    /**
     * Display brand.
     *
     * @param Request $request
     * @param Model   $brand
     *
     * @return Response
     */
    public function show(BrandResourceRequest $request, Brand $model)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = new BrandResource($model);
        return $this->response
            ->setMetaTitle(trans('app.view') . ' ' . trans('brand::brand.name'))
            ->data(compact('data', 'form', 'modules'))
            ->view('brand::brand.show')
            ->output();
    }

    /**
     * Show the form for creating a new brand.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function create(BrandResourceRequest $request, Brand $model)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = new BrandResource($model);
        return $this->response->setMetaTitle(trans('app.new') . ' ' . trans('brand::brand.name'))
            ->view('brand::brand.create')
            ->data(compact('data', 'form', 'modules'))
            ->output();

    }

    /**
     * Create new brand.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(BrandResourceRequest $request, Brand $model)
    {
        try {
            $request = $request->all();
            $model = BrandAction::run('store', $model, $request);
            $data = new BrandResource($model);
            return $this->response->message(trans('messages.success.created', ['Module' => trans('brand::brand.name')]))
                ->code(204)
                ->data(compact('data'))
                ->status('success')
                ->url(guard_url('brand/brand/' . $model->getRouteKey()))
                ->redirect();
        } catch (Exception $e) {
            return $this->response->message($e->getMessage())
                ->code(400)
                ->status('error')
                ->url(guard_url('/brand/brand'))
                ->redirect();
        }

    }

    /**
     * Show brand for editing.
     *
     * @param Request $request
     * @param Model   $brand
     *
     * @return Response
     */
    public function edit(BrandResourceRequest $request, Brand $model)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = new BrandResource($model);
        // return view('brand::brand.edit', compact('data', 'form', 'modules'));

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

    }

    /**
     * Update the brand.
     *
     * @param Request $request
     * @param Model   $brand
     *
     * @return Response
     */
    public function update(BrandResourceRequest $request, Brand $model)
    {
        try {
            $request = $request->all();
            $model = BrandAction::run('update', $model, $request);
            $data = new BrandResource($model);

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

    }

    /**
     * Remove the brand.
     *
     * @param Model   $brand
     *
     * @return Response
     */
    public function destroy(BrandResourceRequest $request, Brand $model)
    {
        try {

            $request = $request->all();
            $model = BrandAction::run('destroy', $model, $request);
            $data = new BrandResource($model);

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

        } catch (Exception $e) {

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

    }
}