from google.appengine.ext import db
from SystemInterface import Dynamic,Static,Data
from orangescape.model.engine.exception.Error import DataError
from orangescape.model.engine.exception.ErrorMessage import Server2000
import copy
import logging

class AbstractData(Data):

    def getSheetId(self):
        return self.SheetId

    def getSheetMetadataId(self):
        return self.SheetMetadataId

    def get(self,cellMetadata):
        return self._dynamic_properties.get(cellMetadata.getSheetId())

    def set(self,cellMetadata,value):
        if isinstance(value, str):
            value = value.decode("utf-8")
        if cellMetadata.getTextLength() > 500:
            self._dynamic_properties[cellMetadata.getSheetId()] = db.Text(value)
        elif (isinstance(value, str) or isinstance(value, unicode)) and len(value) > 500:
            raise DataError, Server2000%(cellMetadata.getCellName() or cellMetadata.getCellDisplayName(),
                                         cellMetadata.getRowMetadata().getSheetMetadata().getSheetName(), self.SheetId)
        else:
            self._dynamic_properties[cellMetadata.getSheetId()] = value

    def getCopy(self):
        return OriginalData(self.SheetMetadataId, self.SheetId, self.ApplicationId, copy.copy(self._dynamic_properties))

    def getDynamicProperties(self):
        return self._dynamic_properties

    def getPrivateData(self):
        privateData = dict()
        for id in self._dynamic_properties:
            value = self._dynamic_properties[id]
            if isinstance(value, db.Text):
                privateData[id] = str(value)
            else:
                privateData[id] = value
                privateData['SheetId'] = self.getSheetId()
                privateData['SheetMetadataId'] = self.getSheetMetadataId()
        privateData['SheetId'] = self.getSheetId()
        privateData['SheetMetadataId'] = self.getSheetMetadataId()
        return privateData

class PrimaryData(AbstractData,db.Expando):
    SheetMetadataId = db.StringProperty()
    SheetId = db.StringProperty()
    ApplicationId = db.StringProperty()

    Deleted = db.BooleanProperty(default=False)
    ModifiedAt = db.FloatProperty()

    def getClass(self):
        return PrimaryData

class SecondaryData(AbstractData,db.Expando):
    SheetMetadataId = db.StringProperty()
    SheetId = db.StringProperty()
    ApplicationId = db.StringProperty()
    AppId = db.StringProperty(default=None)
    App_Id = db.StringProperty(default=None)

    Deleted = db.BooleanProperty(default=False)
    ModifiedAt = db.FloatProperty()

    def getClass(self):
        return SecondaryData

class OriginalData(AbstractData):
    SheetMetadataId = str()
    SheetId = str()
    ApplicationId = str()
    _dynamic_properties = dict()
    def __init__(self, SheetMetadataId, SheetId, ApplicationId, _dynamic_properties):
        self.SheetMetadataId = SheetMetadataId
        self.SheetId = SheetId
        self.ApplicationId = ApplicationId
        self._dynamic_properties = _dynamic_properties
