from django.utils.translation import gettext_lazy as _
from rolepermissions.roles import registered_roles

from customrolepermissions.roles import BaseRole
from customrolepermissions.states import GRANT, TRUE, GRANT_NINP, FALSE_NI, TRUE_NI


# همه کاربران سایت دارند
class BaseUserRole(BaseRole):
    selectable_role = False

    propagate_permissions = False
    grant_all_permissions = True
    available_permissions = {
        'userprofile__read__own': GRANT,
        'userprofile__update__own': TRUE,
    }


# ---

# مدیر کل
class ManagementManagerRole(BaseUserRole):
    old_role_name = 'management_manager_role'
    verbose_name = _('Management Manager')
    verbose_name_short = _('M.Manager')

    propagate_permissions = False
    grant_all_permissions = True
    available_permissions = {
        'is_management': GRANT_NINP,
        'is_management_manager': GRANT_NINP,

    }


# دستیار مدیر کل
class ManagementStaffRole(ManagementManagerRole):
    old_role_name = 'management_staff_role'
    verbose_name = _('Management Staff')
    verbose_name_short = _('M.Staff')
    grant_all_permissions = False
    available_permissions = {
        'is_management_staff': GRANT_NINP,

        'user__create': FALSE_NI,
        'user__read': FALSE_NI,  # used to list all system users
        'user__update': FALSE_NI,
        'user_role__update': FALSE_NI,
        'user_permission__update__all': FALSE_NI,
        'user_permission__update__rel': FALSE_NI,
        'user_permission__update__own': FALSE_NI,
        # sbc
        'sbc_workers__read__all': FALSE_NI,
        'sbc_workers__read__rel': FALSE_NI,
        'sbc_workers__read__own': FALSE_NI,
        'sbc_workers_log__read__all': FALSE_NI,
        'sbc_workers_log__read__rel': FALSE_NI,
        'sbc_workers_log__read__own': FALSE_NI,
        'sbc_menu__read': FALSE_NI,
        # sniper
        'sniper_workers__read__all': FALSE_NI,
        'sniper_workers__read__rel': FALSE_NI,
        'sniper_workers__read__own': FALSE_NI,

        'discharge__read__all': FALSE_NI,
        # trade
        'trade_workers__read__all': FALSE_NI,
        'trade_workers__read__rel': FALSE_NI,
        'trade_workers__read__own': FALSE_NI,
        'trade_menu__read': FALSE_NI,
        # account healthy
        'account_check_healthy_workers__read__all': FALSE_NI,
        'account_check_healthy_workers__read__rel': FALSE_NI,
        'account_check_healthy_workers__read__own': FALSE_NI,
        'account_check_healthy_menu__read': FALSE_NI,
        # proxy
        'proxy_menu__read': FALSE_NI,

        'mule_accounts__read__all': TRUE_NI,
        'mule_accounts__create': FALSE_NI,

        'fifa_account_menu__read': FALSE_NI,
        'pcs__read': TRUE_NI,
        'fifa_proxy__delete': FALSE_NI,
        'dashboard_export_lists': TRUE,

        'console_stock_map': FALSE_NI,

        'console_log__read__all': FALSE_NI,

        'player_card_menu': FALSE_NI,
        'console_menu': FALSE_NI,

        'manager_commands': FALSE_NI,

        'fifa_account__read__all': FALSE_NI,
        'fifa_account__update__all': FALSE_NI,

        'console__read__all': FALSE_NI,
        'console__update__all': FALSE_NI,

        'sbc_worker__read__all': FALSE_NI,
        'sbc_worker__update__all': FALSE_NI,

        'console_report__read__all': FALSE_NI,

        'consoles_command__create__all': FALSE_NI,

        'transactions__read': FALSE_NI,

        'investor_log__read__all': FALSE_NI,
    }


# کاربر ویژه
class SpecialUserRole(ManagementStaffRole):
    old_role_name = 'special_user_role'
    verbose_name = _('Special User')
    verbose_name_short = _('S.User')

    # grant_all_permissions = True
    available_permissions = {
        'is_special_user': GRANT_NINP,
    }


# سرمایه گذار
class InvestorRole(ManagementStaffRole):
    old_role_name = 'investor_role'
    verbose_name = _('Investor')
    verbose_name_short = _('Investor')

    # grant_all_permissions = True
    available_permissions = {
        'is_investor': GRANT_NINP,

        'console_log__read__rel': FALSE_NI,
        'console_log__read__own': FALSE_NI,

        'fifa_account__create': FALSE_NI,
        'fifa_account__read__rel': FALSE_NI,
        'fifa_account__read__own': FALSE_NI,
        'fifa_account__update__rel': FALSE_NI,
        'fifa_account__update__own': FALSE_NI,

        'console__read__rel': FALSE_NI,
        'console__read__own': FALSE_NI,
        'console__update__rel': FALSE_NI,
        'console__update__own': FALSE_NI,

        'console_report__read__rel': FALSE_NI,
        'console_report__read__own': FALSE_NI,

        'investor_log__read__rel': TRUE_NI,
        'investor_log__read__own': TRUE_NI,
    }


# اپراتور
class OperatorRole(ManagementStaffRole):
    old_role_name = 'operator_role'
    verbose_name = _('Operator')
    verbose_name_short = _('Operator')

    # grant_all_permissions = True
    available_permissions = {
        'is_operator': GRANT_NINP,

        'console_log__read__rel': FALSE_NI,
        'console_log__read__own': FALSE_NI,

        'fifa_account__create': FALSE_NI,
        'fifa_account__read__rel': FALSE_NI,
        'fifa_account__read__own': FALSE_NI,
        'fifa_account__update__rel': FALSE_NI,
        'fifa_account__update__own': FALSE_NI,

        'sbc_worker__read__rel': FALSE_NI,
        'sbc_worker__read__own': FALSE_NI,
        'sbc_worker__update__rel': FALSE_NI,
        'sbc_worker__update__own': FALSE_NI,

        'console_report__read__rel': FALSE_NI,
        'console_report__read__own': FALSE_NI,

        'consoles_command__create__rel': FALSE_NI,
        'consoles_command__create__own': FALSE_NI,

        'mule_accounts__read__rel': FALSE_NI,
        'mule_accounts__read__own': FALSE_NI,
        'mule_accounts__create': FALSE_NI,

        'utils': FALSE_NI,
        'consoles_power': FALSE_NI,

        'add_remove_account': FALSE_NI,

        'add_auto_sbc': FALSE_NI,

        'command_menu': FALSE_NI,
        'operator_commands': FALSE_NI,

        'daily_invest_trade__read__all': FALSE_NI,
        'daily_credit_generate__read__all': FALSE_NI,

    }

    
# اپراتور اسنایپر
class SniperOperatorRole(ManagementStaffRole):
    old_role_name = 'sniper_operator_role'
    verbose_name = _('Sniper Operator')
    verbose_name_short = _('Sniper Operator')

    # grant_all_permissions = True
    available_permissions = {
        'is_sniper_operator': GRANT_NINP,
        # sniper
        'sniper_workers__read__all': FALSE_NI,
        'sniper_workers__read__rel': FALSE_NI,
        'sniper_workers__read__own': FALSE_NI,
    }


AnyRole = list(sorted(
    (role for role in registered_roles.values() if getattr(role, 'selectable_role', False)),
    key=lambda role: (getattr(role, 'order', 10000), role.get_name()),
))
