import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class Node_Rectangular 
{
	 private String              	name;
	 private Point2D             	location;
	 
	 private Rectangle2D 			bounds;
	 
	 
	 private int					x1;
	 private int					y1;
	 private int					w;
	 private int					h;
	 
	 
	 	 
	 private ArrayList<Edge_transition>  incidentEdges;
	 
	
	 private boolean             	selected;
	 private boolean			 	loopback_status;
	 private boolean			 	reset_status;
	 private Color					state_color;
	 private Color					text_color;
	 private LoopbackObj 			loop_obj;
	 
	 private int					x_default_name = 0;
	 private int					y_default_name = 0;
	 
	 private int					xcentre = 0;
	 private int					ycentre = 0;
	 
	 
	 public Node_Rectangular() 
	 { 
		 
	 }
	
	 public Node_Rectangular(String name, Point2D aPoint) 
	 {
		 this.name = name;
	     location = aPoint;
	     
	     x1 = (int) aPoint.getX();
	     y1 = (int) aPoint.getY();
	     w = 100;
	     h = 60;
	 
	     xcentre = x1 + (w /2);
	     ycentre = y1 + (h /2);
	     
	     incidentEdges = new ArrayList<Edge_transition>();
	     selected = false;
	     loopback_status = false;
	     reset_status = false;
    	 state_color = Color.BLACK;
	     text_color = Color.BLACK;
	    	
	        
	}
	 
	 public void addIncidentEdge(Edge_transition e) 
	 { 
		 incidentEdges.add(e); 
	 } 
	 
	 public ArrayList<Edge_transition> incidentEdges() 
	 { 
		 return incidentEdges; 
	 }
	 
	 public void setX1(int val)
	 {
		 x1 = val;
	 }  
		    
	 public int getX1()
	 {
		 return x1;
	 }
			    
	 public void setY1(int val)
	 {
		 y1 = val;
	 }  
		    
	 public int getY1()
	 {
		 return y1;
	 }
	 
	 
	 public void setWidth(int val)
	 {
		 w = val;
	 }  
		    
	 public int getWidth()
	 {
		 return w;
	 }
			    
	 public void setHeight(int val)
	 {
		 h = val;
	 }  
		    
	 public int getHeight()
	 {
		 return h;
	 }
	 
	 public boolean contains(Point2D p)
	 {
		 
		 Rectangle r = new Rectangle (x1,y1,w,h);    
		 
	      return r.contains(p);
	 }
	 
	 public void draw(Graphics2D g2)
	 {
	 
	//	 Font font = new Font("Verdana", Font.PLAIN, 12);  
	//	 FontMetrics metrics = new FontMetrics(font) {       	    };  
	//	 Rectangle2D rect_bounds = metrics.getStringBounds( name , null);  
	//	 int widthInPixels = (int) rect_bounds.getWidth(); 	
 	 //    int heightInPixels = (int) rect_bounds.getHeight(); 	
 	     
 	     // default
 	
	//   bien pour connaitre la taille de la font	 
	//	 https://stackoverflow.com/questions/368295/how-to-get-real-string-height-in-java
		 
		 Font currentFont = g2.getFont();
		 FontMetrics fm=g2.getFontMetrics( currentFont );
		 // int widthInPixels = fm.stringWidth(name);
		 //int heightInPixels = fm.getHeight();
 	     
		 
		 int ligne = name.split(System.getProperty("line.separator")).length;
    	 System.out.println("------>  Nb ligne " + ligne);
	    	 

    	 if(ligne == 1)
    	 {
    		 int widthInPixels = fm.stringWidth(name);
    		 int heightInPixels = fm.getHeight();
    		 
    		 if( widthInPixels + (2 * 30) < w ) 
    		 {
    		 
    			 int delta_x = (w - widthInPixels) /2;
    			 int delta_y = (h - heightInPixels) /2;
 	    	 
    			 // identique
    			 int xtest = xcentre - (widthInPixels /2);
    			 int x = x1 + delta_x;
 	    	 
    			 // identique
    			 int ytest = ycentre + (heightInPixels / 2); 
    			 int y = y1 + delta_y + heightInPixels;
 			 
 	    	 	 System.out.println(" Centre un " + xcentre + "  " + ycentre);
 	    	 
 	    	 	 g2.drawString(name, x, y);
 	    	 
 	    	 	 Rectangle2D top = new Rectangle2D.Double(x1, y1, w, h);
 	    	 	 g2.draw(top);
 	    	  	    	 
 	    	 	 g2.setColor(Color.RED);
 	    	 	 Rectangle2D enveloppe = new Rectangle2D.Double(x, y -heightInPixels , widthInPixels, heightInPixels);
 	    	 	 g2.draw(enveloppe);
 	    	 
 	    	 	 System.out.println("un \n");
    		 }
    		 else
    		 {
     	    	 
     	    	 int x = xcentre - (widthInPixels /2);
     	    	 int y = ycentre + (heightInPixels / 2); 
     	    	 
      	    	 x1 = x - 30;
      	    	 y1 = ycentre - 30;
      	    	 
      	    	 int new_w =  widthInPixels + 60;
      	    	 
      	    	  g2.drawString(name, x, y );
     	    	 
     	    	 Rectangle2D top = new Rectangle2D.Double(x1, y1, new_w, h);
     	    	 g2.draw(top);
     	    	 
     	 	     System.out.print(" Centre deux " + xcentre + "  " + ycentre);

     	 	     g2.setColor(Color.RED);
    	    	 Rectangle2D enveloppe = new Rectangle2D.Double(x, y -heightInPixels , widthInPixels, heightInPixels);
    	    	 g2.draw(enveloppe); 
    		 }
     	     
    	 }
    	 else
    	 {
    		  System.out.println("Not define ..");
    	 }
 	     
 	     //System.out.print("----> " + x1 + "  " + y1 + "  " + xcentre + "  " + ycentre);

    	 /*
    	 
 	     if(w > widthInPixels + 2 * 25 && h > heightInPixels + 2 * 15)
 	     {	 
 	    	 
 	    	 int delta_x = (w - widthInPixels) /2;
 	    	 int delta_y = (h - heightInPixels) /2;
 	    	 
 	    	 // identique
 	    	 int xtest = xcentre - (widthInPixels /2);
 	    	 int x = x1 + delta_x;
 	    	 
 	    	 // identique
 	    	 int ytest = ycentre + (heightInPixels / 2); 
 	    	 int y = y1 + delta_y + heightInPixels;
 			 
 	    	
 	    	//  System.out.print(" Position x et y : " + xtest + "  " + x + "  " + ytest + "  " + y );
 	    	 
 	 	     System.out.print(" Centre un " + xcentre + "  " + ycentre);

 	    	 
 	    	 
 	    	 g2.drawString(name, x, y);
 	    	 
 	    	 Rectangle2D top = new Rectangle2D.Double(x1, y1, w, h);
 	    	 g2.draw(top);
 	    	 
 	    	 
 	    	 g2.setColor(Color.RED);
 	    	 Rectangle2D enveloppe = new Rectangle2D.Double(x, y -heightInPixels , widthInPixels, heightInPixels);
 	    	 g2.draw(enveloppe);
 	    	 
 	    	 System.out.print("un \n");
 	     }
 	     
 	     if(w < widthInPixels + 2 * 25 && h > heightInPixels + 2 * 15)
 	     {
 	    	 
 	    	 ligne = name.split(System.getProperty("line.separator")).length;
 	    	 System.out.print(" Nb ligne \n" + ligne);
 	    	 
 	    	 int x = xcentre - (widthInPixels /2);
 	    	 int y = ycentre + (heightInPixels / 2); 
 	    	 
  	    	 x1 = x - 30;
  	    	 y1 = ycentre - 30;
  	    	 
  	    	 w =  widthInPixels + 60;
  	    	 //h =  60; //heightInPixels + 2 * 30;
  	    	 int h  = (ligne + ligne - 1) * g2.getFontMetrics().getHeight() + 2 * 30; 
	    	 
  	    	 
  	    	 for (String line : name.split("\n"))
     	        g2.drawString(line, x, y += g2.getFontMetrics().getHeight());
  	    	 
 	    	
  	    	 //g2.drawString(name, x, y);
 	    	 
// 	    	 int x = rect_milieu_x - (widthInPixels / 2);
// 	    	 int y = rect_milieu_y + (heightInPixels / 2 );
// 	    	 g2.drawString(name, x, y);

// 	    	x1 = x - 25;
// 	    	y1 = y - heightInPixels - 25;
// 	    	w = widthInPixels + 2 * 25;
// 	    	h =  heightInPixels + 2 * 25;
// 	    	 aaa
 	    	 
// 	    	 int delta_x = (w - widthInPixels) /2;
// 	    	 int delta_y = (h - widthInPixels) /2;
 	    	 
// 	    	 int x = x1 + delta_x;
// 	    	 int y = y1 + delta_y + heightInPixels;
 			 
 	    	 
 	    	 Rectangle2D top = new Rectangle2D.Double(x1, y1, w, h);
 	    	 g2.draw(top);
 	    	 
 	 	     System.out.print(" Centre deux " + xcentre + "  " + ycentre);

 	 	     g2.setColor(Color.RED);
	    	 Rectangle2D enveloppe = new Rectangle2D.Double(x, y -heightInPixels , widthInPixels, heightInPixels);
	    	 g2.draw(enveloppe);
 	 	     
 	     }
 	     */
 	    	 
	 }
	 
	 
	 public void setName(String val)
	 {
		 name = val;
	 }
	 
	 public String getName()
	 {
		 return name;
	 }
	
}
