contract UserRoles is StoringCreationMeta, Approvable {
    struct Roles {
        uint[] list;
        mapping(uint => uint) position;
    }
    mapping(address => Roles) userRoles;

    event RolesChanged(address indexed user, uint[] roles);
    // Known roles:
    // 1    - can create events

    function setRole(address _user, uint _role) public onlyApproved {
        _setRole(userRoles[_user], _role);
        emit RolesChanged(_user, userRoles[_user].list);
    }

    function setRoles(address _user, uint[] memory _roles) public onlyApproved {
        for(uint i = 0; i < _roles.length; ++i) {
            _setRole(userRoles[_user], _roles[i]);
        }
        emit RolesChanged(_user, userRoles[_user].list);
    }

    function setRoleForUsers(address[] memory _users, uint _role) public onlyApproved {
        for(uint i = 0; i < _users.length; ++i) {
            _setRole(userRoles[_users[i]], _role);
            emit RolesChanged(_users[i], userRoles[_users[i]].list);
        }
    }

    function _setRole(Roles storage _roles, uint _role) private {
        if (_roles.position[_role] != 0) {
            // Already has role
            return;
        } else {
            _roles.list.push(_role);
            _roles.position[_role] = _roles.list.length;
        }
    }

    function removeRole(address _user, uint _role) public onlyApproved {
        _removeRole(userRoles[_user], _role);
        emit RolesChanged(_user, userRoles[_user].list);
    }

    function removeRoles(address _user, uint[] memory _roles) public onlyApproved {
        for(uint i = 0; i < _roles.length; ++i) {
            _removeRole(userRoles[_user], _roles[i]);
        }
        emit RolesChanged(_user, userRoles[_user].list);
    }

    function _removeRole(Roles storage _roles, uint _role) private {
        if (_roles.position[_role] == 0) {
            // Role not present
            return;
        }

        uint nIndex = _roles.position[_role] - 1;
        uint lastIndex = _roles.list.length  - 1;
        uint lastItem = _roles.list[lastIndex];

        _roles.list[nIndex] = lastItem;
        _roles.position[lastItem] = nIndex + 1;
        _roles.position[_role] = 0;

        _roles.list.pop();
    }

    function hasRole(address _user, uint _role) public view returns(bool) {
        return userRoles[_user].position[_role] != 0;
    }

    function hasAnyRole(address _user, uint[] memory _roles) public view returns(bool) {
        for(uint i = 0; i < _roles.length; ++i) {
            if(hasRole(_user, _roles[i])) {
                return true;
            }
        }
        return false;
    }

    function getUserRoles(address _user) public view returns(uint[] memory) {
        return userRoles[_user].list;
    }

    function clearUserRoles(address _user) public onlyApproved {
        Roles storage _roles = userRoles[_user];

        for(uint i = 0; i < _roles.list.length; ++i) {
            _roles.position[_roles.list[i]] = 0;
        }

        delete _roles.list;
    }
}
function setRole(address _user, uint _role) public onlyApproved {
    _setRole(userRoles[_user], _role);
    emit RolesChanged(_user, userRoles[_user].list);
}

// Known roles:
// 1    - can create events