# Path: rwslib/extras/rwscmd/odmutils.py
# class E_ODM(Enum):
#     """
#     Defines ODM Elements
#     """
#     CLINICAL_DATA = odm('ClinicalData')
#     SUBJECT_DATA = odm('SubjectData')
#     STUDY_EVENT_DATA = odm('StudyEventData')
#     FORM_DATA = odm('FormData')
#     ITEM_GROUP_DATA = odm('ItemGroupData')
#     ITEM_DATA = odm('ItemData')
#     METADATA_VERSION = odm('MetaDataVersion')
# 
#     USER_REF = odm('UserRef')
#     SOURCE_ID = odm('SourceID')
#     DATE_TIME_STAMP_ = odm('DateTimeStamp')
#     REASON_FOR_CHANGE = odm('ReasonForChange')
#     LOCATION_REF = odm('LocationRef')
#     QUERY = mdsol('Query')
#     PROTOCOL_DEVIATION = mdsol('ProtocolDeviation')
#     REVIEW = mdsol('Review')
#     COMMENT = mdsol('Comment')
#     SIGNATURE = odm('Signature')
#     SIGNATURE_REF = odm('SignatureRef')
#     SOURCEID = odm('SourceID')
# 
#     ITEM_DEF = odm('ItemDef')
#     RANGE_CHECK = odm('RangeCheck')
#     CODELIST_REF = odm('CodeListRef')
#     CODELIST = odm('CodeList')
#     CODELIST_ITEM = odm('CodeListItem')
#     ENUMERATED_ITEM = odm('EnumeratedItem')
# 
# class A_ODM(Enum):
#     """
#     Defines ODM Attributes
#     """
#     AUDIT_SUBCATEGORY_NAME = mdsol('AuditSubCategoryName')
#     METADATA_VERSION_OID = 'MetaDataVersionOID'
#     STUDY_OID = 'StudyOID'
#     TRANSACTION_TYPE = 'TransactionType'
#     SUBJECT_NAME = mdsol('SubjectName')
#     SUBJECT_KEY = 'SubjectKey'
#     USER_OID = 'UserOID'
#     LOCATION_OID = 'LocationOID'
#     ITEM_OID = 'ItemOID'
#     VALUE = 'Value'
#     STUDYEVENT_OID = 'StudyEventOID'
#     STUDYEVENT_REPEAT_KEY = 'StudyEventRepeatKey'
#     FORM_OID = 'FormOID'
#     FORM_REPEAT_KEY = 'FormRepeatKey'
#     ITEMGROUP_OID = 'ItemGroupOID'
#     ITEMGROUP_REPEAT_KEY = 'ItemGroupRepeatKey'
#     QUERY_REPEAT_KEY = 'QueryRepeatKey'
#     STATUS = 'Status'
#     RECIPIENT = 'Recipient'
#     RESPONSE = 'Response'
#     FREEZE = mdsol('Freeze')
#     VERIFY = mdsol('Verify')
#     LOCK = mdsol('Lock')
#     SUBJECT_STATUS = mdsol('Status')
#     PROTCOL_DEVIATION_REPEAT_KEY = 'ProtocolDeviationRepeatKey'
#     CLASS = 'Class'  # PV
#     CODE = 'Code'  # PV
#     REVIEWED = 'Reviewed' #Reviews
#     GROUP_NAME = 'GroupName'
#     COMMENT_REPEAT_KEY = 'CommentRepeatKey'
#     INSTANCE_NAME = mdsol('InstanceName')
#     INSTANCE_OVERDUE = mdsol('InstanceOverdue')
#     DATAPAGE_NAME = mdsol('DataPageName')
#     SIGNATURE_OID = 'SignatureOID'
# 
#     OID = 'OID'
#     DATATYPE = 'DataType'
#     LENGTH = 'Length'
#     SIGNIFICANT_DIGITS = 'SignficantDigits'
#     CODELIST_OID = 'CodeListOID'
#     CODED_VALUE = 'CodedValue'
#     DATETIME_FORMAT = mdsol('DateTimeFormat')


import datetime
import hashlib
from lxml import etree
from faker import Factory
from rwslib.extras.rwscmd.odmutils import E_ODM, A_ODM
        """Duck-type value and scramble appropriately"""
        try:
            type, format = typeof_rave_data(value)
            if type == 'float':
                i, f = value.split('.')
                return self.scramble_float(len(value) - 1, len(f))
            elif type == 'int':
                return self.scramble_int(len(value))
            elif type == 'date':
                return self.scramble_date(value, format)
            elif type == 'time':
                return self.scramble_time(format)
            elif type == 'string':
                return self.scramble_string(len(value))
            else:
                return value
        except:
            return ""

    def scramble_subjectname(self, value):
        """Scramble subject name with a consistent one-way hash"""
        # md5 will give a consistent obscured value.
        # TODO:  pattern match the subjectname and string replace?
        # TODO:  leave 'New Subject' un-encoded?
        md5 = hashlib.md5(value)
        return md5.hexdigest()

    def scramble_codelist(self, codelist):
        """Return random element from code list"""
        # TODO:  External code lists

# Path: rwslib/extras/rwscmd/odmutils.py
# class E_ODM(Enum):
#     """
#     Defines ODM Elements
#     """
#     CLINICAL_DATA = odm('ClinicalData')
#     SUBJECT_DATA = odm('SubjectData')
#     STUDY_EVENT_DATA = odm('StudyEventData')
#     FORM_DATA = odm('FormData')
#     ITEM_GROUP_DATA = odm('ItemGroupData')
#     ITEM_DATA = odm('ItemData')
#     METADATA_VERSION = odm('MetaDataVersion')
# 
#     USER_REF = odm('UserRef')
#     SOURCE_ID = odm('SourceID')
#     DATE_TIME_STAMP_ = odm('DateTimeStamp')
#     REASON_FOR_CHANGE = odm('ReasonForChange')
#     LOCATION_REF = odm('LocationRef')
#     QUERY = mdsol('Query')
#     PROTOCOL_DEVIATION = mdsol('ProtocolDeviation')
#     REVIEW = mdsol('Review')
#     COMMENT = mdsol('Comment')
#     SIGNATURE = odm('Signature')
#     SIGNATURE_REF = odm('SignatureRef')
#     SOURCEID = odm('SourceID')
# 
#     ITEM_DEF = odm('ItemDef')
#     RANGE_CHECK = odm('RangeCheck')
#     CODELIST_REF = odm('CodeListRef')
#     CODELIST = odm('CodeList')
#     CODELIST_ITEM = odm('CodeListItem')
#     ENUMERATED_ITEM = odm('EnumeratedItem')
# 
# class A_ODM(Enum):
#     """
#     Defines ODM Attributes
#     """
#     AUDIT_SUBCATEGORY_NAME = mdsol('AuditSubCategoryName')
#     METADATA_VERSION_OID = 'MetaDataVersionOID'
#     STUDY_OID = 'StudyOID'
#     TRANSACTION_TYPE = 'TransactionType'
#     SUBJECT_NAME = mdsol('SubjectName')
#     SUBJECT_KEY = 'SubjectKey'
#     USER_OID = 'UserOID'
#     LOCATION_OID = 'LocationOID'
#     ITEM_OID = 'ItemOID'
#     VALUE = 'Value'
#     STUDYEVENT_OID = 'StudyEventOID'
#     STUDYEVENT_REPEAT_KEY = 'StudyEventRepeatKey'
#     FORM_OID = 'FormOID'
#     FORM_REPEAT_KEY = 'FormRepeatKey'
#     ITEMGROUP_OID = 'ItemGroupOID'
#     ITEMGROUP_REPEAT_KEY = 'ItemGroupRepeatKey'
#     QUERY_REPEAT_KEY = 'QueryRepeatKey'
#     STATUS = 'Status'
#     RECIPIENT = 'Recipient'
#     RESPONSE = 'Response'
#     FREEZE = mdsol('Freeze')
#     VERIFY = mdsol('Verify')
#     LOCK = mdsol('Lock')
#     SUBJECT_STATUS = mdsol('Status')
#     PROTCOL_DEVIATION_REPEAT_KEY = 'ProtocolDeviationRepeatKey'
#     CLASS = 'Class'  # PV
#     CODE = 'Code'  # PV
#     REVIEWED = 'Reviewed' #Reviews
#     GROUP_NAME = 'GroupName'
#     COMMENT_REPEAT_KEY = 'CommentRepeatKey'
#     INSTANCE_NAME = mdsol('InstanceName')
#     INSTANCE_OVERDUE = mdsol('InstanceOverdue')
#     DATAPAGE_NAME = mdsol('DataPageName')
#     SIGNATURE_OID = 'SignatureOID'
# 
#     OID = 'OID'
#     DATATYPE = 'DataType'
#     LENGTH = 'Length'
#     SIGNIFICANT_DIGITS = 'SignficantDigits'
#     CODELIST_OID = 'CodeListOID'
#     CODED_VALUE = 'CodedValue'
#     DATETIME_FORMAT = mdsol('DateTimeFormat')


# Path: rwslib/extras/rwscmd/data_scrambler.py
import datetime
import hashlib
from lxml import etree
from faker import Factory
from rwslib.extras.rwscmd.odmutils import E_ODM, A_ODM

        """Duck-type value and scramble appropriately"""
        try:
            type, format = typeof_rave_data(value)
            if type == 'float':
                i, f = value.split('.')
                return self.scramble_float(len(value) - 1, len(f))
            elif type == 'int':
                return self.scramble_int(len(value))
            elif type == 'date':
                return self.scramble_date(value, format)
            elif type == 'time':
                return self.scramble_time(format)
            elif type == 'string':
                return self.scramble_string(len(value))
            else:
                return value
        except:
            return ""

    def scramble_subjectname(self, value):
        """Scramble subject name with a consistent one-way hash"""
        # md5 will give a consistent obscured value.
        # TODO:  pattern match the subjectname and string replace?
        # TODO:  leave 'New Subject' un-encoded?
        md5 = hashlib.md5(value)
        return md5.hexdigest()

    def scramble_codelist(self, codelist):
        """Return random element from code list"""
        # TODO:  External code lists

        path = ".//{0}[@{1}='{2}']".format(E_ODM.CODELIST.value, A_ODM.OID.value, codelist)