<?php

namespace App\Http\Controllers;

use App\Entity;
use App\EntityClass;
use App\EntityClassInitialSetup;
use App\Exceptions\ViewFeederException;
use App\Http\ViewHelpers\ViewFeeder;
use App\Permission;
use DB;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Lang;
use Redirect;
use Session;

/**
 * Class EntityClassController
 * @package App\Http\Controllers
 */
class EntityClassController extends Controller implements ViewFeederAwareController
{
    const COLUMNS = [
        'id',
        'name',
        'type_name',
        'is_type',
        'has_manufacturer',
        'has_supplier',
        'has_location',
        'require_parent',
        'updated_at'
    ];

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->validate($request, [
            'sort' => Rule::in(self::COLUMNS),
            'ordering' => 'in:asc,desc',
            'count' => 'integer|min:5|max:100',
            'page' => 'integer|min:1'
        ]);

        $sort = $request->get('sort', 'name');
        $ordering = $request->get('ordering', 'asc');
        $count = intval($request->get('count', 10));

        $entityClasses = DB::table('entity_classes as dc')
            ->leftJoin('entity_classes as dtc', 'dc.entity_type_class_id', '=', 'dtc.id')
            ->select([
                'dc.id as id',
                'dc.name as name',
                'dtc.name as type_name',
                'dc.is_type as is_type',
                'dc.has_manufacturer as has_manufacturer',
                'dc.has_supplier as has_supplier',
                'dc.has_location as has_location',
                'dc.require_parent as require_parent',
                'dc.updated_at as updated_at'
            ])
            ->orderBy($sort, $ordering)
            ->orderBy('id', 'asc')
            ->paginate($count);

        $viewFeeder = new ViewFeeder($this, ViewFeeder::LIST);

        return view('results.list', $viewFeeder
            ->results($entityClasses)
            ->sorting($sort, $ordering)
            ->permissions([
                'canEdit' => Permission::EDIT_DEVICE_CLASS,
                'canDelete' => Permission::DELETE_DEVICE_CLASS
            ])
            ->size(12)
            ->feed()
        );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     * @param $entityClassId
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $entityClassId)
    {
        $viewFeeder = new ViewFeeder($this, ViewFeeder::SHOW);

        $entityClass = EntityClass::get($entityClassId, null);

        return view('entity-class.show', $viewFeeder
            ->result($entityClass)
            ->classId($entityClassId)
            ->permissions([
                'canEdit' => Permission::EDIT_DEVICE_CLASS,
                'canDelete' => Permission::DELETE_DEVICE_CLASS
            ])
            ->size(8)
            ->singular($entityClass->name)
            ->feed());
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param EntityClassInitialSetup $entityClass
     * @return \Illuminate\Http\RedirectResponse
     * @internal param int $id
     */
    public function destroy(EntityClassInitialSetup $entityClass)
    {
        $deleted = $entityClass->delete();

        if ($deleted)
            Session::flash('success', Lang::trans('entity_class.delete_success', ['id' => $entityClass->id]));
        else
            Session::flash('error', Lang::trans('entity_class.delete_failed', ['id' => $entityClass->id]));

        return Redirect::back();
    }

    /**
     * Gets the name of the router to be used from the view.
     *
     * @return string
     */
    public static function getRouterName(): string
    {
        return 'entity-classes';
    }

    /**
     * Returns singular of object controller is working with.
     *
     * @return string
     */
    public function getSingular(): string
    {
        return 'Entity Class';
    }

    /**
     * Returns plural of object controller is working with.
     *
     * @return string
     */
    public function getPlural(): string
    {
        return 'Entity Classes';
    }

    /**
     * Returns array of columns to be shown when listing objects controller is working with.
     *
     * @return array
     */
    public function getColumns(): array
    {
        return self::COLUMNS;
    }
}
