import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;

public class Graph_Node 
{
    private String            				name_state;
    private ArrayList<Node_Rectangular>   	node_rectangles;
  
    
    public Graph_Node() 
    {
    	this("", new ArrayList<Node_Rectangular>()); 	
    
    	//new ArrayList<Node_Rectangular>();
    }
    

    public Graph_Node(String name, ArrayList<Node_Rectangular> initialNodes) 
    {
        name_state = name;
        node_rectangles = initialNodes;
    }
  
    public String getName() 
    { 
    	return name_state ; 
   	}

  
    public String getId() 
    { 
    	return "label"; 
   	}
    
    public void setName(String name) 
    { 
    	name_state = name; 
   	}
    
    public void addEdge(Node_Rectangular start, Node_Rectangular end) 
    {
    	Edge_transition anEdge = new Edge_transition(start, end);
        start.addIncidentEdge(anEdge);
        end.addIncidentEdge(anEdge);
    }
  
    public void addNode(Node_Rectangular obj_node_rectangle) 
    {
    	node_rectangles.add(obj_node_rectangle);
    }
    
    public ArrayList<Node_Rectangular> getNodeRectangle() 
    { 
    	return node_rectangles; 
   	}
    
    
    public boolean buildEdge( Point2D p1, Point2D p2) // Edge e,
    {
       Node_Rectangular n1 = findNode(p1);
       Node_Rectangular n2 = findNode(p2);
       if (n1 != null && n2 != null)
       {
    	   
    	  addEdge(n1, n2 );
    	   
    	  ArrayList<Edge_transition>    edges = getEdges();
    	   
          return true;
       }
       return false;
    }
    
    
    public ArrayList<Edge_transition> getEdges() 
    {
        ArrayList<Edge_transition>  edges = new ArrayList<Edge_transition>();

        for (Node_Rectangular n: node_rectangles) 
        {
            for (Edge_transition e: n.incidentEdges()) 
            {
                if (!edges.contains(e)) //so that it is not added twice
                    edges.add(e);
            }
        }

        return edges;
    }
    
  
    
    public Node_Rectangular findNode(Point2D p)
    {
       for (int i = node_rectangles.size() - 1; i >= 0; i--)
       {
          Node_Rectangular n = node_rectangles.get(i);
          if (n.contains(p)) 
          {  
        	  return n;
          }
       }
       return null;
    }

    public void draw(Graphics2D g2)
    {
       for (Node_Rectangular n : node_rectangles)
       {	   
          n.draw02(g2); //draw(g2);
       }   
       
       ArrayList<Edge_transition>    edges = getEdges();
       
       for (Edge_transition e: edges)  
       {
    	   e.draw(g2);
       }

    }
    
    public Node_Rectangular nodeAt(int x, int y) 
    {
   	
        for (int i = 0;  i< node_rectangles.size() ; i++)
        {
            Node_Rectangular node_obj = node_rectangles.get(i); 
            int xmin = node_obj.getX1() ;
   		 	int ymin = node_obj.getY1() ;
   		 	int xmax = xmin + node_obj.getWidth();
   		 	int ymax = ymin + node_obj.getHeight();
   		 
   		 	if(x >= xmin && x <= xmax && y >= ymin && y <= ymax  )
   		 	{
   		 		System.out.println("Node rectangle detected.. "); 
   		 		return node_obj;
   		 	}
        }
        
        return null;
    }

    public ArrayList<Node_Rectangular> selectedNodes() 
    {
        ArrayList<Node_Rectangular>   selected = new ArrayList<Node_Rectangular>();
        for (Node_Rectangular n:  node_rectangles)
        {	
           if (n.isSelected()) 
            {
            	selected.add(n);
            }
       }  
       return selected;
    }
    
    
    public String find_selected()
    {
        int position = 0;
        
        for (Node_Rectangular n:  node_rectangles)
        {    
            if (n.isSelected())
            {     
                  String id = n.getID();
                  return(id);  
            } 
            position = position + 1;
        }          
        
        return(null);
    }
    
    public ArrayList<Node_Rectangular> getNodes() 
    { 
    	return node_rectangles; 
    }
    
    // Find and return the Node with the given label
    public Node_Rectangular nodeNamed(String id) 
    {
        for (Node_Rectangular n:  node_rectangles)
            if (n.getID().equals(id))  return n;

        return null;  // If we don't find one
    }
    
    
    public void RectangularEdge( Node_Rectangular n1, Node_Rectangular n2) 
    {
    	  addEdge(n1, n2 );
    	   
    	  ArrayList<Edge_transition>    edges = getEdges();
    	   
    }
   
    
    // Add an edge to the graph from the given start Node to the end Node
   /* public void addEdge(Node start, Node end) 
    {
        // First make the edge
        EdgeObj anEdge = new EdgeObj(start, end);

        // Now tell the nodes about the edge
        start.addIncidentEdge(anEdge);
        end.addIncidentEdge(anEdge);
    }*/
    
    public void pipoaddEdge(Node_Rectangular start, Node_Rectangular end) 
    {
        
        Edge_transition anEdge = new Edge_transition(start, end);

        // Now tell the nodes about the edge
        start.addIncidentEdge(anEdge);
        end.addIncidentEdge(anEdge);
    }
    
    
    public boolean GetEdgeExists(Node_Rectangular start, Node_Rectangular end) 
    {

    	String new_start = start.getID();
    	String new_end = end.getID();
    	
    	ArrayList<Edge_transition>  edges = getEdges();
    	
    	if(edges.size() != 0)
    	{
	    	for (Edge_transition e:  edges) 
	    	{
	    		String exists_start  = e.getStartNode().getID();
	    		String exists_end  = e.getEndNode().getID();
	        	
	    		if( new_start.equals(exists_start)  && new_end.equals(exists_end) )        	
	    		{
	    			return true;
	    		}
	        
	    	}
    	}	
    	
        return false;
    }
    
    
    
    
    
}   