<?php

namespace App\Http\Middleware;

use App\Permission;
use App\Role;
use App\User;
use Auth;
use Closure;
use DB;
use Illuminate\Support\Pluralizer;
use Lang;
use Lavary\Menu\Builder;
use Menu;

class Menus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Auth::user();

        if ($user !== null) {

            Menu::make('menu', function (Builder $menu) use ($user) {

                $menu->add('Entities', '#')->active('entities/*');
                $menu->add('Entity Types', '#')->active('entity-types/*');
                $menu->add('Locations', '#')->active('locations/*');
                $menu->add('Manufacturers', '#')->active('manufacturers/*');
                $menu->add('Suppliers', '#')->active('suppliers/*');

                $menu->add('Entity Classes', '#')->active('entity-classes/*');
                $menu->add('Users', '#')->active('users/*');

                $entitiesCreate = $menu->get('entities')->add('do.Create a New Entity', 'entities/create');
                $entitiesCreate->data('permission', Permission::CREATE_DEVICE);

                $entityTypesCreate = $menu->get('entityTypes')->add('do.Create a New Entity Type', 'entity-types/create');
                $entityTypesCreate->data('permission', Permission::CREATE_DEVICE_TYPE);

                $menu->get('locations')->add('do.Create a New Location', 'locations/create')
                    ->data('permission', Permission::CREATE_LOCATION);

                $menu->get('manufacturers')->add('do.Create a New Manufacturer', 'manufacturers/create')
                    ->data('permission', Permission::CREATE_MANUFACTURER);

                $menu->get('suppliers')->add('do.Create a New Supplier', 'suppliers/create')
                    ->data('permission', Permission::CREATE_SUPPLIER);

                $menu->get('entityClasses')->add('do.Create a New Entity Class', 'entity-classes/create')
                    ->data('permission', Permission::CREATE_DEVICE_CLASS);

                $menu->get('users')->add('do.Create a New User', 'users/create')
                    ->data('permission', Permission::CREATE_USER);
                /*
                                $menu->get('entities')->add('Entities List', 'entities')
                                    ->data('permission', Permission::LIST_DEVICES)->divide();

                                $menu->get('entityTypes')->add('Entity Types List', 'entity-types')
                                    ->data('permission', Permission::LIST_DEVICE_TYPES)->divide();
                */
                $menu->get('locations')->add('Locations List', 'locations')
                    ->data('permission', Permission::LIST_LOCATIONS);

                $menu->get('manufacturers')->add('Manufacturers List', 'manufacturers')
                    ->data('permission', Permission::LIST_MANUFACTURERS);

                $menu->get('suppliers')->add('Suppliers List', 'suppliers')
                    ->data('permission', Permission::LIST_SUPPLIERS);

                $menu->get('entityClasses')->add('Entity Classes List', 'entity-classes')
                    ->data('permission', Permission::LIST_DEVICE_CLASSES);

                $menu->get('users')->add('Users List', 'users')
                    ->data('permission', Permission::LIST_USERS);


                if ($user->can([Permission::LIST_DEVICES, Permission::LIST_DEVICE_TYPES])) {
                    $entities = DB::table('entities as d')
                        ->join('entity_classes as dc', 'd.entity_class_id', '=', 'dc.id')
                        ->select([
                            'dc.id as id',
                            'dc.name as name',
                            'dc.is_type as is_type'
                        ])
                        ->distinct()
                        ->orderBy('name', 'asc')
                        ->get();

                    $entitiesMenuItem = $menu->get('entities');
                    $entityTypesMenuItem = $menu->get('entityTypes');

                    $anyEntityAdded = false;
                    $anyEntityTypeAdded = false;

                    foreach ($entities as $entity) {
                        $id = $entity->id;
                        $name = $entity->name;

                        if ($entity->is_type) {
                            if (!$anyEntityTypeAdded) {
                                $entityTypesCreate->divide();
                            }
                            $anyEntityTypeAdded = true;
                            $entityTypesMenuItem->add(Pluralizer::plural($name), 'entity-types/' . $id);

                        } else {
                            if (!$anyEntityAdded) {
                                $entitiesCreate->divide();
                            }

                            $anyEntityAdded = true;
                            $entitiesMenuItem->add(Pluralizer::plural($name), 'entities/' . $id);
                        }
                    }
                }


            })->
            filter(function ($item) use ($user) {

                $item->link->secure();

                $permissionNeeded = $item->data('permission');

                if ($permissionNeeded == null || $user->can($permissionNeeded)) {
                    return true;
                }
                return false;
            });
        }
        return $next($request);
    }
}
