package transformation.wizards;

import com.nomagic.magicdraw.core.Application;
import com.nomagic.magicdraw.openapi.uml.PresentationElementsManager;
import com.nomagic.magicdraw.openapi.uml.ReadOnlyElementException;
import com.nomagic.magicdraw.openapi.uml.SessionManager;
import com.nomagic.magicdraw.ui.actions.DefaultDiagramAction;
import com.nomagic.magicdraw.uml.symbols.DiagramPresentationElement;
import com.nomagic.magicdraw.uml.symbols.PresentationElement;
import com.nomagic.magicdraw.uml.symbols.shapes.NoteView;
import com.nomagic.magicdraw.uml.symbols.shapes.ShapeElement;
import com.nomagic.uml2.ext.magicdraw.actions.mdbasicactions.CallBehaviorAction;
import com.nomagic.uml2.ext.magicdraw.activities.mdbasicactivities.ActivityFinalNode;
import com.nomagic.uml2.ext.magicdraw.activities.mdbasicactivities.InitialNode;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Type;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

public class UMLsec_Wizard {
	
	ShapeElement note;
	DiagramPresentationElement activityDiagramPE;
	PresentationElementsManager PEMInstance;
	
	/**
	 * Creates a note on the model with a list of missing elements.
	 */
	
	public UMLsec_Wizard() {
		
		PEMInstance = PresentationElementsManager.getInstance();
		activityDiagramPE = Application.getInstance().getProject().getActiveDiagram();
		
		SessionManager.getInstance().createSession("Create element");
		
		NoteView note = null;
		
		// Looks for an existing note element
		for (PresentationElement element : activityDiagramPE.getPresentationElements()) {
			if (element instanceof NoteView) {
				note = (NoteView)element;
			}
		}
		
		// If none exist, creates a new note
		if (note == null) {
			try {
				note = (NoteView)(PEMInstance.createNote(activityDiagramPE));
				note.setLocation(700, 300);
				note.setSize(200, 200);
			}
			catch (ReadOnlyElementException e) {
				JOptionPane.showMessageDialog(null, "Read-only element found, cannot create help note");
			}
		}
		
		// Adds text to note
		String noteText = validate();
		if (noteText != "") {
			note.setName(noteText);
		}
		SessionManager.getInstance().closeSession();
		
		// Makes the project saveable
		Application.getInstance().getProject().setDirty(true);
	}
	
	/**
	 * Validates the diagram to see which elements are missing
	 * 
	 * @return List of elements missing from a complete UMLsec model
	 */
	private String validate() {
		
		boolean hasInitialNode = false;
		boolean hasFinalNode = false;
		
		// A list of actions that have no control flow attached to it
		List<String> controlflowlessActions = new ArrayList<String>();
		
		for (Element element : activityDiagramPE.getActualElement().getOwner().getOwnedElement()) {
			if (element instanceof InitialNode) {
				hasInitialNode = true;
			}
			if (element instanceof ActivityFinalNode) {
				hasFinalNode = true;
			}
			
			// For each activity on the diagram finds its control flows 
			if (element instanceof CallBehaviorAction) {
				if (((CallBehaviorAction) element).getIncoming().size() == 0 &&
						((CallBehaviorAction)element).getOutgoing().size() == 0) {
					controlflowlessActions.add(((CallBehaviorAction)element).getName());
				}
			}		 
		}
		
		String returnString = "";
		
		if (hasInitialNode == false) {
			returnString += "* Add an initial node.\n";
		}
		
		if (hasFinalNode == false) {
			returnString += "\n* Add a final node.\n";
		}
		
		if (controlflowlessActions.size() != 0) {
			returnString += "\n * The following actions have no control flows:";
			
			for (String actionName : controlflowlessActions) {
				returnString += "\n" + actionName;
			}
		}
		
		if (returnString.equals("")) {
			returnString += "Nothing could be found missing, diagram is OK";
		}
		
		return returnString;
	}
}
