from orangescape.model.server.SystemInterface import Dynamic
import base64

def encrypt(s):
    return base64.encodestring(s)

def decrypt(s):
    return base64.decodestring(s)

class DynamicModel(Dynamic):
    dataObject = None
    originalObject = None
    session = None
    sheetMetadata = None

    def __init__(self,dataObject,sheetMetadata,session=None):
        self.dataObject = dataObject
        self.originalObject = dataObject.getCopy()
        self.sheetMetadata = sheetMetadata
        self.session = session

    def getSheetId(self):
        return self.dataObject.getSheetId()

    def getSession(self):
        return self.session

    def getSheetMetadata(self):
        return self.sheetMetadata

    def getCellMetadata(self, name):
        return self.getSheetMetadata().getPublicCellMetadata(name)

    def getSheetMetadataName(self):
        return self.getValue('SheetMetadataName')

    def getSheetName(self):
        return self.getValue('SheetName')

    def getValue(self, name):
        return self.get(self.getCellMetadata(name))

    def getText(self, name):
        value = self.getValue(name)
        if isinstance(value,str) or isinstance(value,unicode):
            return value
        raise Exception, "value in %s for the record %s is not a text" %(name, self.getSheetId())

    def getNumber(self, name):
        value = self.getValue(name)
        if not (isinstance(value,str) or isinstance(value,unicode)):
            return value
        raise Exception, "value in %s for the record %s is not a number" %(name, self.getSheetId())

    def getDate(self, name):
        value = self.getValue(name)
        if not (isinstance(value,str) or isinstance(value,unicode)):
            import datetime
            from orangescape.model.engine.util.DateUtil import xldate_as_tuple
            datetuple= xldate_as_tuple(value, 0)
            return datetime.datetime(datetuple[0],datetuple[1],datetuple[2],datetuple[3],datetuple[4],datetuple[5])
        raise Exception, "value in %s for the record %s is not a date" %(name, self.getSheetId())

    def getBoolean(self, name):
        value = self.getValue(name)
        if value == 1.0:
            return True
        else:
            return False
        raise Exception, "value in %s for the record %s is not a boolean" %(name, self.getSheetId())

    def getReference(self, name):
        cellMetadata = self.getCellMetadata(name)
        value = self.get(cellMetadata)
        return self.getSession().get(value, cellMetadata.getReferredSheetMetadata())

    def get(self,cellMetadata):
        value = self.dataObject.get(cellMetadata)
        if cellMetadata.getFinalFormat()=='Password' and value:
            try:
                value = decrypt(value)
            except Exception, e:
                import logging
                logging.warn('Incorrect encryption. %s. for the instance %s in the model for the cell %s. %s'%(self.getSheetId(), self.getSheetMetadataName(), cellMetadata, e))
                return value
        return value

    def set(self,cellMetadata, value):
        if cellMetadata.getFinalFormat()=='Password' and value:
            value = encrypt(value)
        self.dataObject.set(cellMetadata, value)

    def getDataObject(self):
        return self.dataObject

    def getOriginalData(self):
        return self.originalObject

    def updateOriginalData(self):
        self.originalObject = self.dataObject.getCopy()

    def printData(self):
        print self.dataObject._dynamic_properties

    def getPrivateData(self):
        return self.dataObject.getPrivateData()

    def getKey(self):
        return self.dataObject.key()