from django import template

from common.consts import UserRoles

register = template.Library()


@register.filter
def has_item(list, item):
    return item in list


def _has_profile(user):
    return hasattr(user, "profile") and user.profile


@register.filter
def has_role(user, role_slugs):
    return _has_profile(user) and len(user.profile.roles.filter(slug__in=role_slugs.split(","))) > 0


@register.filter
def is_admin(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.ADMIN)


@register.filter
def is_manager(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.MANAGER)


@register.filter
def is_accountant(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.ACCOUNTANT)


@register.filter
def is_broker(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.BROKER)

@register.filter
def is_installer(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.INSTALLER)

@register.filter
def is_regional_water_authority(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.REGIONAL_WATER_AUTHORITY)

@register.filter
def is_water_resource_manager(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.WATER_RESOURCE_MANAGER)

@register.filter
def is_headquarter(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.HEADQUARTER)

@register.filter
def is_customer(user):
    return _has_profile(user) and user.profile.has_role(UserRoles.CUSTOMER)

