from collections import OrderedDict
from radar.utils import Enum


class ORGANISATION_ROLES(Enum):
    IT = 'IT'
    ADMIN = 'ADMIN'
    CLINICIAN = 'CLINICIAN'
    SENIOR_CLINICIAN = 'SENIOR_CLINICIAN'
    GOD = 'GOD'


class COHORT_ROLES(Enum):
    SENIOR_RESEARCHER = 'SENIOR_RESEARCHER'
    RESEARCHER = 'RESEARCHER'
    GOD = 'GOD'


ORGANISATION_ROLE_NAMES = OrderedDict([
    (ORGANISATION_ROLES.SENIOR_CLINICIAN, 'Senior Clinician'),
    (ORGANISATION_ROLES.CLINICIAN, 'Clinician'),
    (ORGANISATION_ROLES.ADMIN, 'Admin'),
    (ORGANISATION_ROLES.IT, 'IT'),
])

COHORT_ROLE_NAMES = OrderedDict([
    (COHORT_ROLES.RESEARCHER, 'Researcher'),
    (COHORT_ROLES.SENIOR_RESEARCHER, 'Senior Researcher'),
])

# Special roles
ORGANISATION_GOD = 'GOD'
COHORT_GOD = 'GOD'


class PERMISSIONS(Enum):
    VIEW_PATIENT = 'VIEW_PATIENT'
    EDIT_PATIENT = 'EDIT_PATIENT'
    RECRUIT_PATIENT = 'RECRUIT_PATIENT'
    VIEW_DEMOGRAPHICS = 'VIEW_DEMOGRAPHICS'
    VIEW_USER = 'VIEW_USER'
    EDIT_USER_MEMBERSHIP = 'EDIT_USER_MEMBERSHIP'


ORGANISATION_PERMISSIONS = {
    PERMISSIONS.VIEW_PATIENT: [
        ORGANISATION_ROLES.GOD,
        ORGANISATION_ROLES.CLINICIAN,
        ORGANISATION_ROLES.SENIOR_CLINICIAN,
        ORGANISATION_ROLES.ADMIN,
    ],
    PERMISSIONS.EDIT_PATIENT: [
        ORGANISATION_ROLES.GOD,
        ORGANISATION_ROLES.CLINICIAN,
        ORGANISATION_ROLES.SENIOR_CLINICIAN,
    ],
    PERMISSIONS.RECRUIT_PATIENT: [
        ORGANISATION_ROLES.GOD,
        ORGANISATION_ROLES.CLINICIAN,
        ORGANISATION_ROLES.SENIOR_CLINICIAN,
    ],
    PERMISSIONS.VIEW_DEMOGRAPHICS: [
        ORGANISATION_ROLES.GOD,
        ORGANISATION_ROLES.CLINICIAN,
        ORGANISATION_ROLES.SENIOR_CLINICIAN,
        ORGANISATION_ROLES.ADMIN,
    ],
    PERMISSIONS.VIEW_USER: [
        ORGANISATION_ROLES.GOD,
        ORGANISATION_ROLES.IT,
        ORGANISATION_ROLES.ADMIN,
        ORGANISATION_ROLES.SENIOR_CLINICIAN,
    ],
}

ORGANISATION_MANAGED_ROLES = {
    ORGANISATION_ROLES.IT: [
        ORGANISATION_ROLES.CLINICIAN,
    ],
    ORGANISATION_ROLES.ADMIN: [
        ORGANISATION_ROLES.CLINICIAN,
    ],
    ORGANISATION_ROLES.SENIOR_CLINICIAN: [
        ORGANISATION_ROLES.CLINICIAN,
    ],
}

COHORT_PERMISSIONS = {
    PERMISSIONS.VIEW_PATIENT: [
        COHORT_ROLES.SENIOR_RESEARCHER,
        COHORT_ROLES.RESEARCHER,
        COHORT_ROLES.GOD
    ],
    PERMISSIONS.VIEW_DEMOGRAPHICS: [
        COHORT_ROLES.SENIOR_RESEARCHER,
        COHORT_ROLES.GOD,
    ],
    PERMISSIONS.VIEW_USER: [
        COHORT_ROLES.GOD
    ],
}

COHORT_MANAGED_ROLES = {
    COHORT_ROLES.SENIOR_RESEARCHER: [
        COHORT_ROLES.RESEARCHER
    ]
}


def get_cohort_roles_with_permission(permission):
    return COHORT_PERMISSIONS.get(permission, [])


def get_organisation_roles_with_permission(permission):
    return ORGANISATION_PERMISSIONS.get(permission, [])

@@ -59,16 +59,50 @@
 t Enum%0A%0A
+# TODO move to radar.permissions%0A%0A
 %0Aclass O

Add TODO