package org.framed.orm.ui.editPart.connectionkinds;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.BendpointConnectionRouter;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MidpointLocator;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.swt.SWT;
import org.framed.orm.model.Relationship;
import org.framed.orm.model.RelationshipConstraint;
import org.framed.orm.ui.editPart.types.ORMCompartmentEditPart;


public class ORMRelationshipConstraintEditPart extends ORMRelationEditPart {

  private Label lab = new Label();
  private String textInitial;
  
  public Label getLabel() {
    return lab;
  }

  public IFigure creatRelationshipConstraint(String text) {

    lab.setText(text);
    textInitial = text;
    PolylineConnection conn = new PolylineConnection();
    conn.setAntialias(SWT.ON);
    conn.setLineDash(new float[] {5.0f, 5.0f});
    conn.setLineStyle(SWT.LINE_CUSTOM);;
    conn.setConnectionRouter(new BendpointConnectionRouter());

    // add label to the connection
    // TODO: change calculation/refresh so that label is always in the mid of the connection
    MidpointLocator midL = new MidpointLocator(conn, 0);
    midL.setGap(5);
    midL.setRelativePosition(PositionConstants.SOUTH);
    // this is needed, because when the label would be just added the label can be seen in the
    // compartmentdiagram
    if (getRoot().getContents() instanceof ORMCompartmentEditPart) {
      conn.add(lab, midL);
    }
    return conn;
  }
  
  /***/
  @Override
  public void refreshVisuals() {
    super.refreshVisuals();
    Relationship rlship = ((RelationshipConstraint) getModel()).getRelation();
    if (getTarget() != null && getSource() != null && rlship != null) {
   
      List<RelationshipConstraint> relCList = new ArrayList<RelationshipConstraint>();
   
      relCList.addAll(rlship.getRlshipConstraints());

      if (relCList.size() != 0) {

        ORMRelationshipConstraintEditPart rep =
            (ORMRelationshipConstraintEditPart) getViewer().getEditPartRegistry().get(relCList.get(0));

        if (!rep.equals(this) && !rep.getLabel().getText().contains(lab.getText()) ) {
          String oldText = rep.getLabel().getText();
          rep.getLabel().setText( lab.getText()+  " , " + oldText );
          
          getConnectionFigure().setVisible(false);
        }
        if(rep.equals(this)){
          lab.setText(textInitial);
          getConnectionFigure().setVisible(true);
        }
      
      }
    }
  }
}


@@ -684,18 +684,16 @@
 nitial;%0A
-  
 %0A  publi
@@ -1586,18 +1586,16 @@
 nn;%0A  %7D%0A
-  
 %0A  /***/
@@ -1819,19 +1819,16 @@
 null) %7B%0A
-   
 %0A      L
@@ -1907,19 +1907,16 @@
 int%3E();%0A
-   
 %0A      r
@@ -2132,16 +2132,33 @@
 y().get(
+%0A                
 relCList
@@ -2163,24 +2163,50 @@
 st.get(0));%0A
+        if (rep != null) %7B
 %0A        if 
@@ -2198,24 +2198,26 @@
 ) %7B%0A        
+  
 if (!rep.equ
@@ -2282,13 +2282,14 @@
 t())
- 
 ) %7B%0A
+  
     
@@ -2347,16 +2347,18 @@
         
+ 
+ 
 rep.getL
@@ -2372,17 +2372,16 @@
 setText(
- 
 lab.getT
@@ -2385,18 +2385,18 @@
 etText()
+ 
 +
- 
  %22 , %22 +
@@ -2407,23 +2407,14 @@
 Text
- 
 );%0A
+%0A
   
-        %0A
     
@@ -2464,26 +2464,30 @@
 e);%0A        
+ 
+ 
 %7D%0A
+  
         if(r
@@ -2484,16 +2484,17 @@
       if
+ 
 (rep.equ
@@ -2503,16 +2503,17 @@
 s(this))
+ 
 %7B%0A      
@@ -2512,24 +2512,26 @@
 %7B%0A          
+ 
+ 
 lab.setText(
@@ -2544,16 +2544,18 @@
 itial);%0A
+  
         
@@ -2592,24 +2592,36 @@
 ible(true);%0A
+          %7D%0A
         %7D%0A  
@@ -2622,15 +2622,8 @@
   %7D%0A
-      %0A
     
@@ -2638,9 +2638,8 @@
 %7D%0A  %7D%0A%7D%0A
-%0A

fix for when multiple rlshipConstraints are deleted through deletion of their source or target