

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.ArrayList;

public class StateObj 
{

	public static int    		RADIUS = 50;
    private String              label;
    private Point2D             location;
    private ArrayList<EdgeObj>  incidentEdges;
    private boolean             selected;
    private boolean				loopback_status;
    private boolean				reset_status;
    
    private Color				state_color;
    private Color				text_color;
    
    
    
    // static pour le chargement d'un fichier
    private  LoopbackObj 		loop_obj;
    
    
    public StateObj() 
    { 
    	this("", new Point2D.Double(0,0)); 
    	
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;
    	
    	
    }
    
    public StateObj(String aLabel) 
    { 
    	this(aLabel, new Point2D.Double(0,0) {}); 
    	
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;
    	

  	}

    public StateObj(Point2D aPoint)
    { 
    	this("", aPoint);
    	
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;
    	

   	}
    
    public StateObj(double x, double y) 
    { 
    	this("", new Point2D.Double(x,y)); 
    	
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;


    	
   	}
    
    public StateObj(String aLabel, Point2D aPoint) 
    {
        label = aLabel;
        location = aPoint;
        incidentEdges = new ArrayList<EdgeObj>();
        selected = false;
        loopback_status = false;
        reset_status = false;
        
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;
    
    	
        int radius = 50;
        int x1  = (int) (location.getX() - RADIUS);
        int y1  = (int) (location.getY() - RADIUS);
        int x2  = (int) (location.getX() + RADIUS);
        int y2 =  (int) (location.getY() + RADIUS);
        
        
        loop_obj = new LoopbackObj( label, (int)location.getX(), (int)location.getY(), x1, y1,  x2,  y2);
        
       
        
    }

    public String getLabel() { return label; }
    public Point2D getLocation() { return location; }
    public void setLabel(String newLabel) { label = newLabel; }
    public void setLocation(Point2D aPoint) { location = aPoint; }
    public void setLocation(double x, double y) { location = new Point2D.Double(x, y); }
    public boolean isSelected() { return selected; }
    
    public void setSelected(boolean state) { selected = state; }
    
    public void setUnSelected(boolean state) { selected = state; }
    
    
    public LoopbackObj getLoopBack()
    {
    	return loop_obj;
    }
    
    public void toggleSelected() { selected = !selected; }

    
    public void LoopToggleSelected() 
    { 
    	loopback_status = !loopback_status; 
   	}
   
    public void Set_loopback_status(boolean state)
    {
    	loopback_status = state;
    }

    public boolean Get_loopback_status()
    {
    	return loopback_status;
    }
    

    public void Set_reset_status(boolean state)
    {
    	reset_status = state;
    }

    public boolean Get_reset_status()
    {
    	return reset_status;
    }
    
    
    public ArrayList<EdgeObj> incidentEdges() { return incidentEdges; }
    public void addIncidentEdge(EdgeObj e) { incidentEdges.add(e); }
    
    
    public LoopbackObj getloopback() 
    { 
    	return loop_obj; 
    }
    
    public void setLoop(LoopbackObj obj) 
    { 
    	loop_obj = obj;
   	}
    

    // Return a list of all neighbouring Nodes
    public ArrayList<Node> neighbours() 
    {
        ArrayList<Node> result = new ArrayList<Node>();

        //for (EdgeObj e: incidentEdges)
        //    result.add(e.otherEndFrom(this));

        return null; //result;
    }

    public String toString() 
    {
        return(label + "(" + location.getX() + "," + location.getY() + ")");
    }
    
    public void draw(Graphics g) 
    {
    	if (selected)
            g.setColor(Color.RED);
    	else
            //g.setColor(Color.blue);
            g.setColor(getStateColor());
    	               
        g.drawOval((int)location.getX() - RADIUS, (int)location.getY() - RADIUS, RADIUS*2, RADIUS*2);
        
        // reset sur le state
        if(	reset_status == true)
        {
        	  int rayon = RADIUS - 5;	
        	
        	  g.drawOval((int)location.getX() - rayon, (int)location.getY() - rayon, rayon*2, rayon*2);
        }
        
        g.setColor(getTextColor());
        Font myFont = new Font("Arial", 1, 14);
        g.setFont(myFont);
        g.drawString(label, (int)location.getX() + RADIUS, (int)location.getY() - RADIUS);
    }

    public void saveTo(PrintWriter aFile) 
    {
        aFile.println(label);
        aFile.println((int)location.getX());
        aFile.println((int)location.getY());
        aFile.println(selected);
        aFile.println(loopback_status);
        
		aFile.println((int) loop_obj.startPt.x );
		aFile.println((int) loop_obj.startPt.y );
		aFile.println((int) loop_obj.endPt.x );
		aFile.println((int) loop_obj.endPt.y );
			        	
		aFile.println((int) loop_obj.startCtrlPt.x );
		aFile.println((int) loop_obj.startCtrlPt.y );
		aFile.println((int) loop_obj.endCtrlPt.x );
		aFile.println((int) loop_obj.endCtrlPt.y );
        	
        aFile.println(reset_status);
        aFile.println( format_couleur( state_color ) );
        aFile.println( format_couleur( text_color ) );

    }
    
    public String format_couleur(Color c)
    {
    	  int r = c.getRed();
          int g = c.getGreen();
          int b = c.getBlue();
          String RGBColor = Integer.toString(r) + "-" +  Integer.toString(g) + "-" +  Integer.toString(b); 
    	  // chaine R-G-B
          return RGBColor;
    }
    
    public static Node loadFrom(BufferedReader aFile) throws IOException 
    {
        Node   aNode = new Node();
        
        //Node(String aLabel, Point2D aPoint) 
        
       // LoopbackObj loop = new LoopbackObj();

        aNode.setLabel(aFile.readLine());
        aNode.setLocation(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine()));
        aNode.setSelected(Boolean.valueOf(aFile.readLine()).booleanValue());
        
        aNode.Set_loopback_status(Boolean.valueOf(aFile.readLine()).booleanValue());
       
        //loop.setStartPt(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine()) );
        
        //loop.setEndPt(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine()) );
        
        //loop.setStartCtrlPt(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine() ));
        
        //loop.setEndCtrlPt(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine() ));
        
        Point pt_setStartPt = new Point(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine()) );
        Point pt_setEndPt = new Point(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine()) );
        Point pt_setStartCtrlPt = new Point(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine()) );
        Point pt_setEndCtrlPt = new Point(Integer.parseInt(aFile.readLine()), Integer.parseInt(aFile.readLine()) );
 /*               
        int p1x = Integer.parseInt(aFile.readLine());
        int p1y = Integer.parseInt(aFile.readLine());
        
        int p2x = Integer.parseInt(aFile.readLine());
        int p2y = Integer.parseInt(aFile.readLine());

        int p3x = Integer.parseInt(aFile.readLine());
        int p3y = Integer.parseInt(aFile.readLine());

        int p4x = Integer.parseInt(aFile.readLine());
        int p4y = Integer.parseInt(aFile.readLine());

        System.out.println("&&&&&&&  " + p1x + " " + p1y + " " +  p2x + " " + p2y + " " + p3x + " " + p3y + " " + p4x + " " + p4y);
        
        int x1  = (int) (aNode.getLocation().getX() - RADIUS);
        int y1  = (int) (aNode.getLocation().getY() - RADIUS);
        int x2  = (int) (aNode.getLocation().getX() + RADIUS);
        int y2 =  (int) (aNode.getLocation().getY() + RADIUS);
  */  
        //loop_obj = new LoopbackObj( aNode.getLabel() , (int) (aNode.getLocation().getX()), (int) (aNode.getLocation().getY()) , x1, y1,  x2,  y2);
        
        
        //loop_obj = new LoopbackObj( aNode.getLabel() , pt_setStartPt, pt_setEndPt, pt_setStartCtrlPt,  pt_setEndCtrlPt );
        
        
        
        aNode.Set_reset_status(Boolean.valueOf(aFile.readLine()).booleanValue());
        
        Color tmpcolor = null;
        String[] tokens = null;
        
        // pour la couleur du state
        tokens = aFile.readLine().split("-");
        tmpcolor =  new Color( Integer.parseInt(tokens[0]) , Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]));
        aNode.setStateColor(tmpcolor);
        
        // pour la couleur du texte
        tokens = aFile.readLine().split("-");
        tmpcolor =  new Color( Integer.parseInt(tokens[0]) , Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]));
        aNode.setTextColor(tmpcolor);
        
        return aNode;
    }

    private Color Color(double d, double d0, double d1) 
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    
    public void updateCoordLoopBack(int xcentre, int ycentre)
    {
    	//loop_obj.x0 = val;
    	
    	loop_obj.calculationLoopbackObj(xcentre, ycentre);
    }
    
    
 /*   
    public void mise_a_jour_CoordLoopBack(int xcentre, int ycentre)
    {
    	
    	//loop_obj.calculationLoopbackObj(xcentre, ycentre);
    	loop_obj.traitement(xcentre, ycentre);
    }
   */ 
    
    public void recalculation()
    {
    	
    	loop_obj.recalculation();
    }
    
    
    public void setStateColor(Color c)
	{
		state_color = c;
	}
    
	public Color getStateColor()
	{
		return state_color;
	}
	
	public void setTextColor(Color c)
	{
		text_color = c;
	}
	    
	public Color getTextColor()
	{
		return text_color;
	}
		
}
