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

import coral
from coral.modeler.extension import *
DI = coral.lang.XMIDI20
from coral.modeler.diagram.degen import DiagramElementGenerator

from coral.modeler.diagram import di_support as DISUPP

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


        if parentge:
            model = parentge.semanticModel.element
        else:
            model = diagram.owner.element
        assert model
        
        coral.getTransactionManager().beginModifyCmd()
        try:
            element = self.createModelElement(model,parent)
            if not element:
                return
            deg = DiagramElementGenerator.getExtension(element.__type__())
            if not deg:
                return
            de = deg.createDiagramElement(element, position=DI.Point(x=px,y=py) )
            if not de:
                return
            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

class CreateLinkAction(CreateAction):

     def check(self,e1,e2):
          return True
     
     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)):
                clsf1 = hints[0].semanticModel.element
                clsf2 = hints[1].semanticModel.element
                if not self.check(clsf1,clsf2):
                    return
            else:
                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])
                    
                        if de:
                            de.contained[0].position = DISUPP.calcGENameDIPoints(hints[2])
                            de.anchor.append(anchor0)
                            de.anchor.append(anchor1)
                            hints[0].anchorage.append(anchor0)
                            hints[1].anchorage.append(anchor1)
                            parent.contained.append(de)
                            
                  
            finally:
                coral.getTransactionManager().endModifyCmd()
     
     def createModelElement(self, model, parent, ge1, ge2):
         return None

     def createEdgeOrNot(self):
         return True
