#!/usr/bin/env python
# This test is written to test that a deleted association isn't updated
# and cause errors like in CRL-228

import os
from coral.modeler import modeler
import coral
import time

DI = coral.lang.XMIDI20
UML14 = coral.lang.UML14

m=modeler.ModelerWindow()
project=m.openProject("3classes_with_associations_in_packages.xmi")
m.main(loop=0)

# Find the model and the diagram in the Project
model=None
diagram=None
for e in project.contents():
    if e.oclIsKindOf(UML14.Model):
        model=e
    if e.oclIsKindOf(DI.Diagram):
        diagram=e

assert model
assert diagram

# make the diagram visible
m.getDiagramEditorManager().editorSet.notifyFocusOn(diagram)
m.processEvents()

### Find the two associations
associations=[]
for e in model.__getAllParts__():
    if e.oclIsKindOf(UML14.Association):
        associations.append(e)

assert len(associations)==2

outerEdge=None
outerPackageNode=None
outerClassNode=None

for gnode in diagram.contained:
    if gnode.oclIsKindOf(DI.GraphEdge):
        outerEdge=gnode
        print gnode, " is a GraphEdge"
    if gnode.oclIsKindOf(DI.GraphNode):
        if gnode.semanticModel.element.oclIsKindOf(UML14.Package):
            outerPackageNode=gnode
            print gnode, " is a GraphNode for a Package"
        if gnode.semanticModel.element.oclIsKindOf(UML14.Class):
            outerClassNode=gnode
            print gnode, " is a GraphNode for a Class"

assert outerEdge
assert outerPackageNode
assert outerClassNode

innerPackageNode=outerPackageNode.contained[1].contained[0]
assert innerPackageNode

from coral.modeler.diagram import layout
innerPackageNode.layouter.__getPyValue__().layout( layout._SizeHinter() )

print "inside the inner package ",innerPackageNode.contained[1].contained
classesInside=[]
innerEdge=None
for gnode in innerPackageNode.contained[1].contained:
    if gnode.oclIsKindOf(DI.GraphNode):
        classesInside.append(gnode)
    if gnode.oclIsKindOf(DI.GraphEdge):
        innerEdge=gnode

assert len(classesInside)==2
assert innerEdge


graph_edge_x=innerEdge.position.x
graph_edge_y=innerEdge.position.y

inner_edge_start_x=innerEdge.waypoints[0].x
inner_edge_start_y=innerEdge.waypoints[0].y
inner_edge_end_x=innerEdge.waypoints[1].x
inner_edge_end_y=innerEdge.waypoints[1].y

print inner_edge_start_x,",",inner_edge_start_y
print inner_edge_end_x,",",inner_edge_end_y

# Move the outerPackage
coral.getTransactionManager().beginModifyCmd()
try:
    outerPackageNode.position.x+=50.0
finally:
    coral.getTransactionManager().endModifyCmd()

coral.getTransactionManager().beginModifyCmd()
try:
    outerPackageNode.position.y+=50.0
finally:
    coral.getTransactionManager().endModifyCmd()

m.processEvents()
time.sleep(0.5)

assert innerEdge.position.x==graph_edge_x
assert innerEdge.position.y==graph_edge_y

print "inner_edge_start[1]==innerEdge.waypoints[0].y-50 :", inner_edge_start_y,"==",innerEdge.waypoints[0].y
print "inner_edge_start[0]==innerEdge.waypoints[0].x-50 :", inner_edge_start_x,"==",innerEdge.waypoints[0].x

# Check that the realtive position of the graphEdge's waypoints has not changed
assert inner_edge_start_x==innerEdge.waypoints[0].x
assert inner_edge_start_y==innerEdge.waypoints[0].y

