# coding=utf-8

CONTENT_TYPE_SORTING = (
    # 系统设置
    'area-area',  # 区域设置
    'building-building',  # 建筑信息
    'equipment-equipment',  # 设备信息
    'employee-employee',  # 员工管理
    'option-option',  # 系统选项
    'config-config',  # 基础设置
    'account-user',  # 权限管理
    'repair_order-repairorder',  # 报修管理
    'inspection_order-inspectionorder',  # 巡检管理
)

MENU_TO_MODEL = (
    (
        u'系统设置', (
            'area-area',
            'building-building',
            'equipment-equipment',
            'employee-employee',
            'option-option',
            'config-config',
            'account-user',
        )
    ),
    (
        u'报修管理', (
            'repair_order-repairorder',
        )
    ),
    (
        u'巡检管理', (
            'inspection_order-inspectionorder',
        )
    ),
)

class PermissionMenu(object):

    def __init__(self):
        self.sort = CONTENT_TYPE_SORTING
        self.menus = MENU_TO_MODEL

    def get_index(self, app_label, model):
        try:
            return self.sort.index('{}-{}'.format(app_label, model))
        except:
            return 9999

    def sort_perms(self, perms):
        perms = perms.order_by('content_type__model', 'id')
        perms = sorted(perms, key=lambda n: self.get_index(n.content_type.app_label, n.content_type.model))
        return perms

    def get_menuname_of_contenttype(self, app_label, model):
        for menu in self.menus:
            val = '{}-{}'.format(app_label, model)
            if val in menu[1]:
                return menu[0]
        return u'未分类'
