from siege import game
from siege.component import Component, ComponentDefinition, ComponentFactory


class DataDefinition(ComponentDefinition):
    def __init__(self, data):
        ComponentDefinition.__init__(self)
        self.data = data
        self.freeze()

    def getType(self):
        return DataComponent.TYPE


class DataComponent(Component):
    TYPE = "data"
    CID = 0
    VERSION = 1

    def getType(self):
        return DataComponent.TYPE

    @staticmethod
    def create(entity, componentType, definition):
        data = DataComponent(definition.data)
        entity.add(data)
        return data

    def __init__(self, data):
        Component.__init__(self)
        self.data = data.copy()
        self.freeze()

    @staticmethod
    def register():
        DataComponent.CID = game.entity.requestCid(DataComponent.TYPE, DataDefinition)
        game.registerComponent(DataComponent.TYPE, ComponentFactory.create(DataComponent.create))
