<?php

namespace App;

use App\Http\Controllers\EntityClassController;
use App\Http\Controllers\EntityController;
use App\Http\Controllers\EntityTypeController;
use App\Http\Controllers\LocationController;
use App\Http\Controllers\ManufacturerController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\UserController;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;

/**
 * Class User
 * @package App
 */
class User extends Authenticatable implements CanResetPasswordContract
{
    use Notifiable;
    use CanResetPassword;

    use EntrustUserTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Checks, whether this user can create an object associated with provided router.
     *
     * @param $router
     * @return bool
     */
    public function canCreate($router)
    {
        $permissionToCheck = null;
        switch ($router) {
            case EntityController::getRouterName():
                $permissionToCheck = Permission::CREATE_DEVICE;
                break;
            case EntityTypeController::getRouterName():
                $permissionToCheck = Permission::CREATE_DEVICE_TYPE;
                break;
            case EntityClassController::getRouterName():
                $permissionToCheck = Permission::CREATE_DEVICE_CLASS;
                break;
            case LocationController::getRouterName():
                $permissionToCheck = Permission::CREATE_LOCATION;
                break;
            case ManufacturerController::getRouterName():
                $permissionToCheck = Permission::CREATE_MANUFACTURER;
                break;
            case SupplierController::getRouterName():
                $permissionToCheck = Permission::CREATE_SUPPLIER;
                break;
            case UserController::getRouterName():
                $permissionToCheck = Permission::CREATE_USER;
                break;
        }

        if ($permissionToCheck === null)
            return false;

        return $this->can($permissionToCheck);
    }


    /**
     * Check if user has a permission by its name.
     *
     * @param $permissionId array|int Permission id or array of permissions.
     * @param bool $requireAll All permissions in the array are required.
     * @return bool
     */
    public function canById($permissionId, $requireAll = false)
    {
        if (is_array($permissionId)) {
            foreach ($permissionId as $permName) {
                $hasPerm = $this->can($permName);

                if ($hasPerm && !$requireAll) {
                    return true;
                } elseif (!$hasPerm && $requireAll) {
                    return false;
                }
            }

            // If we've made it this far and $requireAll is FALSE, then NONE of the perms were found
            // If we've made it this far and $requireAll is TRUE, then ALL of the perms were found.
            // Return the value of $requireAll;
            return $requireAll;
        } else {
            foreach ($this->cachedRoles() as $role) {
                // Validate against the Permission table
                foreach ($role->cachedPermissions() as $perm) {
                    if ($permissionId === $perm->id) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}
