# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.

import coral
DI = coral.lang.XMIDI20
from coral.modeler.extension import *
from coral.modeler import iconcache
from coral.modeler.diagram.degen import DiagramElementGenerator
from coral import log
logger=log.getLogger("FSM10DiagramActions")
import coral

coral.lang.FSM10.__import__(globals())

class CreateAction( DiagramEditorToolbarEntry ):

    def prepare(self, diagram):
        self.__diagram = diagram

    def __call__(self, hints, parentge):
        return self.doIt(self.__diagram, self.__diagram, hints, parentge)

    def doIt( self, diagram, parent, hints=[], parentge=None  ):
        if hints:
            hint1 = hints[0]
            if hint1:
                px = hint1[0]
                py = hint1[1]
            else:
                px = 50
                py = 50

                
        model = diagram.owner.element
        assert model
        
        coral.getTransactionManager().beginModifyCmd()
        try:
            element = self.createModelElement(model,parent)
            if element:
                deg = DiagramElementGenerator.getExtension(element.__type__())
                if deg:
                    de = deg.createDiagramElement(element, position=DI.Point(x=px,y=py), size=self.getDimension() )
                    if de:
                        parent.contained.append(de)
        finally:
            coral.getTransactionManager().endModifyCmd()

    def createModelElement( self, model, parent ):
        pass #raise Exception("Not implemented")

    n = 0
    def perpetualEnumerator( cls ):
        cls.n += 1
        return cls.n
    perpetualEnumerator = classmethod(perpetualEnumerator)

    def createEdgeOrNot(self):
        return False

    def getDimension(self):
        raise NotImplemented

    
class CreateStateAction( CreateAction ):

    def getIcon( cls ):
        return iconcache.getIcon("icons/statetb","FSM10")
    getIcon = classmethod(getIcon)

    def createModelElement( self, model, parent ):
        c = State(name="ST%d"%self.perpetualEnumerator())
        model.state.insert(c)
        return c

    def getLabel(self):
        return "State"
    
    def getDimension(self):
        return DI.Dimension(width=35, height=35)



class CreateAcceptingStateAction( CreateAction ):

    def getIcon( cls ):
        return iconcache.getIcon("icons/acceptingstatetb","FSM10")
    getIcon = classmethod(getIcon)

    def createModelElement( self, model, parent ):
        c = AcceptingState(name="AST%d"%self.perpetualEnumerator())
        model.state.insert(c)
        return c

    def getLabel(self):
        return "AcceptingState"
    
    def getDimension(self):
        return DI.Dimension(width=35, height=35)

class CreateTransitionAction( CreateAction ):

    def doIt(  self, diagram, parent, hints=[], parentge=None  ):
        if hints and len(hints)==3:
            model = diagram.owner.element
            assert model

            if ((hints[0].semanticModel and hints[0].semanticModel.element) and
                (hints[1].semanticModel and hints[1].semanticModel.element)):
                me1 = hints[0].semanticModel.element
                me2 = hints[1].semanticModel.element
                if not (me1.oclIsKindOf(State) and me2.oclIsKindOf(State)):
                    logger.debug("Attempt to make illegal transition")
                    return
            else:
                logger.debug("Attempt to make illegal transition")
                return

            coral.getTransactionManager().beginModifyCmd()
            try:
                element = self.createModelElement(model,parent, hints[0], hints[1])
                if element:
                    deg = DiagramElementGenerator.getExtension(element.__type__())
                    if deg:
                        anchor0 = DI.GraphConnector(graphElement=hints[0], position=DI.Point())
                        anchor1 = DI.GraphConnector(graphElement=hints[1], position=DI.Point())
                        de = deg.createDiagramElement(element, waypoints=hints[2])
                        de.anchor.append(anchor0)
                        de.anchor.append(anchor1)
                        hints[0].anchorage.append(anchor0)
                        hints[1].anchorage.append(anchor1)
                        if de:
                            parent.contained.append(de)
            finally:
                coral.getTransactionManager().endModifyCmd()
    
    def createModelElement( self, model, parent, ge1, ge2 ):
        c = Transition()
        c.source = ge1.semanticModel.element
        c.target = ge2.semanticModel.element
        model.transition.insert(c)
        return c
    
    def createEdgeOrNot(self):
        return True    

    def getLabel(self):
        return "Transition"
    
    def getIcon( cls ):
        return iconcache.getIcon("icons/transitiontb","FSM10")
    getIcon = classmethod(getIcon)


