# -*- coding: utf-8 -*-

import enum


class FlagEnum(enum.Enum):
    """A derivated enum that enable interraction with flags.
    Enum value should be multiple of the power of 2."""

    @classmethod
    def flag(cls):
        """Return the associated flag class."""
        return None

#---------------------------------------

    def to_int(self):
        return self.value

    def __int__(self):
        return self.to_int()

#---------------------------------------

    def __or__(self, value):
        flag_class = self.flag()
        if flag_class:
            return flag_class(self) | value
        else:
            return super().__or__(value)

    def __neg__(self):
        flag_class = self.flag()
        if flag_class:
            return flag_class(*[st for st in iter(self.__class__)
                                if st is not self])
        else:
            return super().__neg__()

#---------------------------------------

    @classmethod
    def all(cls):
        flag_class = cls.flag()
        if flag_class:
            return flag_class(*tuple(iter(cls.__members__.values())))