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 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.draw(g2);
       }   
       
       ArrayList<Edge_transition>    edges = getEdges();
       
       for (Edge_transition e: edges)  
       {
    	   e.draw(g2);
       }
       
     

    }
    
    public Node_Rectangular nodeAt(double x, double 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;
    }

    
}   