<?php

namespace App;

use Zizaco\Entrust\EntrustPermission;

/**
 * Class Permission
 * @package App
 */
class Permission extends EntrustPermission
{

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

    const CREATE_USER           = 'Create User';
    const CREATE_DEVICE_CLASS   = 'Create Entity Class';
    const CREATE_DEVICE_TYPE    = 'Create Entity Type';
    const CREATE_DEVICE         = 'Create Entity';
    const CREATE_MANUFACTURER   = 'Create Manufacturer';
    const CREATE_SUPPLIER       = 'Create Supplier';
    const CREATE_LOCATION       = 'Create Location';

    const EDIT_USER             = 'Edit User';
    const EDIT_DEVICE_CLASS     = 'Edit Entity Class';
    const EDIT_DEVICE_TYPE      = 'Edit Entity Type';
    const EDIT_DEVICE           = 'Edit Entity';
    const EDIT_MANUFACTURER     = 'Edit Manufacturer';
    const EDIT_SUPPLIER         = 'Edit Supplier';
    const EDIT_LOCATION         = 'Edit Location';

    const DELETE_USER           = 'Delete User';
    const DELETE_DEVICE_CLASS   = 'Delete Entity Class';
    const DELETE_DEVICE_TYPE    = 'Delete Entity Type';
    const DELETE_DEVICE         = 'Delete Entity';
    const DELETE_MANUFACTURER   = 'Delete Manufacturer';
    const DELETE_SUPPLIER       = 'Delete Supplier';
    const DELETE_LOCATION       = 'Delete Location';
    const LIST_USERS            = 'List Users';
    const LIST_DEVICE_CLASSES   = 'List Entity Classes';
    const LIST_DEVICE_TYPES     = 'List Entity Types';
    const LIST_DEVICES          = 'List Entities';
    const LIST_MANUFACTURERS    = 'List Manufacturers';
    const LIST_SUPPLIERS        = 'List Suppliers';
    const LIST_LOCATIONS        = 'List Locations';

    const STATE_TRANSITION_ACTIVE_INACTIVE = 'State from Active to Inactive';
    const STATE_TRANSITION_INACTIVE_ACTIVE = 'State from Inactive to Active';
    const STATE_TRANSITION_ACTIVE_REPAIRING = 'State from Active to Repairing';
    const STATE_TRANSITION_ACTIVE_WRITTEN_OFF = 'State from Active to Written off';
    const STATE_TRANSITION_REPAIRING_ACTIVE = 'State from Repairing to Active';

    const MANIPULATE_USER_ROLES = 'Manipulate User Roles';

//@formatter:off

    const CREATE_USER_ID                         = 1;
    const CREATE_DEVICE_CLASS_ID                 = 2;
    const CREATE_DEVICE_TYPE_ID                  = 3;
    const CREATE_DEVICE_ID                       = 4;
    const CREATE_MANUFACTURER_ID                 = 5;
    const CREATE_SUPPLIER_ID                     = 6;
    const CREATE_LOCATION_ID                     = 7;
    const EDIT_USER_ID                           = 8;
    const EDIT_DEVICE_CLASS_ID                   = 9;
    const EDIT_DEVICE_TYPE_ID                    = 10;
    const EDIT_DEVICE_ID                         = 11;
    const EDIT_MANUFACTURER_ID                   = 12;
    const EDIT_SUPPLIER_ID                       = 13;
    const EDIT_LOCATION_ID                       = 14;
    const DELETE_USER_ID                         = 15;
    const DELETE_DEVICE_CLASS_ID                 = 16;
    const DELETE_DEVICE_TYPE_ID                  = 17;
    const DELETE_DEVICE_ID                       = 18;
    const DELETE_MANUFACTURER_ID                 = 19;
    const DELETE_SUPPLIER_ID                     = 20;
    const DELETE_LOCATION_ID                     = 21;
    const LIST_USERS_ID                          = 22;
    const LIST_DEVICE_CLASSES_ID                 = 23;
    const LIST_DEVICE_TYPES_ID                   = 24;
    const LIST_DEVICES_ID                        = 25;
    const LIST_MANUFACTURER_ID                   = 26;
    const LIST_SUPPLIERS_ID                      = 27;
    const LIST_LOCATIONS_ID                      = 28;
    const STATE_TRANSITION_ACTIVE_INACTIVE_ID    = 29;
    const STATE_TRANSITION_INACTIVE_ACTIVE_ID    = 30;
    const STATE_TRANSITION_ACTIVE_REPAIRING_ID   = 31;
    const STATE_TRANSITION_ACTIVE_WRITTEN_OFF_ID = 32;
    const STATE_TRANSITION_REPAIRING_ACTIVE_ID   = 33;
    const MANIPULATE_USER_ROLES_ID               = 34;


    const AUDITOR_PERMISSIONS = [
        self::LIST_DEVICE_TYPES_ID => self::LIST_DEVICE_TYPES,
        self::LIST_DEVICES_ID => self::LIST_DEVICES,
        self::LIST_MANUFACTURER_ID => self::LIST_MANUFACTURERS,
        self::LIST_SUPPLIERS_ID => self::LIST_SUPPLIERS,
        self::LIST_LOCATIONS_ID => self::LIST_LOCATIONS
    ];

    const DEVICE_MANAGER_PERMISSIONS = [
        self::CREATE_DEVICE_TYPE_ID => self::CREATE_DEVICE_TYPE,
        self::CREATE_DEVICE_ID => self::CREATE_DEVICE,
        self::CREATE_MANUFACTURER_ID => self::CREATE_MANUFACTURER,
        self::CREATE_SUPPLIER_ID => self::CREATE_SUPPLIER,
        self::CREATE_LOCATION_ID => self::CREATE_LOCATION,
        self::EDIT_DEVICE_TYPE_ID => self::EDIT_DEVICE_TYPE,
        self::EDIT_DEVICE_ID => self::EDIT_DEVICE,
        self::EDIT_MANUFACTURER_ID => self::EDIT_MANUFACTURER,
        self::EDIT_SUPPLIER_ID => self::EDIT_SUPPLIER,
        self::EDIT_LOCATION_ID => self::EDIT_LOCATION,
        self::DELETE_DEVICE_ID => self::DELETE_DEVICE,
        self::DELETE_MANUFACTURER_ID => self::DELETE_MANUFACTURER,
        self::DELETE_SUPPLIER_ID => self::DELETE_SUPPLIER,
        self::DELETE_LOCATION_ID => self::DELETE_LOCATION,
        self::LIST_DEVICE_TYPES_ID => self::LIST_DEVICE_TYPES,
        self::LIST_DEVICES_ID => self::LIST_DEVICES,
        self::LIST_MANUFACTURER_ID => self::LIST_MANUFACTURERS,
        self::LIST_SUPPLIERS_ID => self::LIST_SUPPLIERS,
        self::LIST_LOCATIONS_ID => self::LIST_LOCATIONS,
        self::STATE_TRANSITION_ACTIVE_INACTIVE_ID => self::STATE_TRANSITION_ACTIVE_INACTIVE,
        self::STATE_TRANSITION_INACTIVE_ACTIVE_ID => self::STATE_TRANSITION_INACTIVE_ACTIVE,
    ];

    const ALL_PERMISSIONS = [
        self::CREATE_USER_ID => self::CREATE_USER,
        self::CREATE_DEVICE_CLASS_ID => self::CREATE_DEVICE_CLASS,
        self::CREATE_DEVICE_TYPE_ID => self::CREATE_DEVICE_TYPE,
        self::CREATE_DEVICE_ID => self::CREATE_DEVICE,
        self::CREATE_MANUFACTURER_ID => self::CREATE_MANUFACTURER,
        self::CREATE_SUPPLIER_ID => self::CREATE_SUPPLIER,
        self::CREATE_LOCATION_ID => self::CREATE_LOCATION,
        self::EDIT_USER_ID => self::EDIT_USER,
        self::EDIT_DEVICE_CLASS_ID => self::EDIT_DEVICE_CLASS,
        self::EDIT_DEVICE_TYPE_ID => self::EDIT_DEVICE_TYPE,
        self::EDIT_DEVICE_ID => self::EDIT_DEVICE,
        self::EDIT_MANUFACTURER_ID => self::EDIT_MANUFACTURER,
        self::EDIT_SUPPLIER_ID => self::EDIT_SUPPLIER,
        self::EDIT_LOCATION_ID => self::EDIT_LOCATION,
        self::DELETE_USER_ID => self::DELETE_USER,
        self::DELETE_DEVICE_CLASS_ID => self::DELETE_DEVICE_CLASS,
        self::DELETE_DEVICE_TYPE_ID => self::DELETE_DEVICE_TYPE,
        self::DELETE_DEVICE_ID => self::DELETE_DEVICE,
        self::DELETE_MANUFACTURER_ID => self::DELETE_MANUFACTURER,
        self::DELETE_SUPPLIER_ID => self::DELETE_SUPPLIER,
        self::DELETE_LOCATION_ID => self::DELETE_LOCATION,
        self::LIST_USERS_ID => self::LIST_USERS,
        self::LIST_DEVICE_CLASSES_ID => self::LIST_DEVICE_CLASSES,
        self::LIST_DEVICE_TYPES_ID => self::LIST_DEVICE_TYPES,
        self::LIST_DEVICES_ID => self::LIST_DEVICES,
        self::LIST_MANUFACTURER_ID => self::LIST_MANUFACTURERS,
        self::LIST_SUPPLIERS_ID => self::LIST_SUPPLIERS,
        self::LIST_LOCATIONS_ID => self::LIST_LOCATIONS,
        self::STATE_TRANSITION_ACTIVE_INACTIVE_ID => self::STATE_TRANSITION_ACTIVE_INACTIVE,
        self::STATE_TRANSITION_INACTIVE_ACTIVE_ID => self::STATE_TRANSITION_INACTIVE_ACTIVE,
        self::STATE_TRANSITION_ACTIVE_REPAIRING_ID => self::STATE_TRANSITION_ACTIVE_REPAIRING,
        self::STATE_TRANSITION_ACTIVE_WRITTEN_OFF_ID => self::STATE_TRANSITION_ACTIVE_WRITTEN_OFF,
        self::STATE_TRANSITION_REPAIRING_ACTIVE_ID => self::STATE_TRANSITION_REPAIRING_ACTIVE,
        self::MANIPULATE_USER_ROLES_ID => self::MANIPULATE_USER_ROLES,
    ];

    const ALL_STATE_TRANSITIONS = [
        self::STATE_TRANSITION_ACTIVE_INACTIVE_ID => self::STATE_TRANSITION_ACTIVE_INACTIVE,
        self::STATE_TRANSITION_INACTIVE_ACTIVE_ID => self::STATE_TRANSITION_INACTIVE_ACTIVE,
        self::STATE_TRANSITION_ACTIVE_REPAIRING_ID => self::STATE_TRANSITION_ACTIVE_REPAIRING,
        self::STATE_TRANSITION_ACTIVE_WRITTEN_OFF_ID => self::STATE_TRANSITION_ACTIVE_WRITTEN_OFF,
        self::STATE_TRANSITION_REPAIRING_ACTIVE_ID => self::STATE_TRANSITION_REPAIRING_ACTIVE,
    ];
//@formatter:on

    /**
     * Returns false if not found ..
     *
     * @param $permission
     * @return string|false
     */
    public static function getId($permission)
    {
        $id = array_search($permission, self::ALL_PERMISSIONS);

        if ($id === false) {
            $found = Permission::where('name', $permission)->first();
            if ($found)
                $id = $found->id;
        }

        return $id;
    }
}
