
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;


import java.io.*;
import java.util.ArrayList;
import java.util.Random;

public class DecisionObj 
{

    private String              label;
    private String	            id;
    private Point2D             location;
    private Point             	p1;
    private Point             	p2;
    private Point             	p3;
    private Point             	p4;
   
    private boolean             selected;
    private int					w = 100;
    private	int					h = 100;	
    private int					xcentre;
    private int					ycentre;
    
    private ArrayList<Edge_transition>  incidentEdges;
    
    private Color				state_color;
    private Color				text_color;
    
    public DecisionObj() 
    { 
    	this("", new Point2D.Double(0,0)); 
    	
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;
    }
    
    public DecisionObj(String aLabel) 
    { 
    	this(aLabel, new Point2D.Double(0,0) {}); 
    	
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;

  	}

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

   	}
    
    public DecisionObj(double x, double y) 
    { 
    	this("", new Point2D.Double(x,y)); 
    	
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;
    	
   	}
    
    public DecisionObj(String aLabel, Point2D aPoint) 
    {
        label = aLabel;
        location = aPoint;
        
        incidentEdges = new ArrayList<Edge_transition>();
        
        selected = false;
        
    	this.state_color = Color.BLACK;
    	this.text_color = Color.BLACK;
    	
    	
    	//         P2
        //         *
        //       *   * 
        // P1  *       *  P3
        //       *   *
        //         *
        //         P4
        //
    	
    	xcentre = (int) aPoint.getX();
    	ycentre = (int) aPoint.getY();
    	
    	
    	p1 = new Point();
    	p2 = new Point();
    	p3 = new Point();
    	p4 = new Point();
    	
    	p1.setLocation( (int) (aPoint.getX() - (w/2))  , (int)aPoint.getY());
    	p2.setLocation( (int) (aPoint.getX() )  ,  (int) (aPoint.getY() - (h/2)) );
    	p3.setLocation( (int) (aPoint.getX() + (w/2))  , (int)aPoint.getY());
    	p4.setLocation( (int) (aPoint.getX() )  ,  (int) (aPoint.getY() + (h/2)) );
    	
    	id = generate_id();
    }
    
    
    public void addIncidentEdge(Edge_transition e) 
	 { 
		 incidentEdges.add(e); 
	 } 
	 
	 public ArrayList<Edge_transition> incidentEdges() 
	 { 
		 return incidentEdges; 
	 }
    
    public Point getP1()
    {
    	return p1;
    }
    
    public Point getP2()
    {
    	return p2;
    }
    
    public Point getP3()
    {
    	return p3;
    }
    
    public Point getP4()
    {
    	return p4;
    }
    
    public void setP1(Point pval)
    {
    	p1 = pval;
    }
    
    public void setP2(Point pval)
    {
    	p2 = pval;
    }
    
    public void setP3(Point pval)
    {
    	p3 = pval;
    }
    
    public void setP4(Point pval)
    {
    	p4 = pval;
    }
    
    public void setXcentre(int val)
    {
    	xcentre = val;
    }
    
    public void setYcentre(int val)
    {
    	ycentre = val;
    }
        
	public void setSelected(boolean select)
	{
		selected = select;
	}

	public void setUnSelected(boolean state) 
	{ 
	   	selected = state; 
  	}
	    
	public boolean getsetSelected()
	{
		return selected;
	}
		
	public void toggleSelected() 
	{ 
		selected = !selected; 
	}
		
	public boolean isSelected() 
	{ 
		return selected; 
	}
		
	public void set_state_color(Color c)
	{
		state_color = c;
	}
	    
	public Color get_state_color()
	{
		return state_color;
	}
		
	public void set_text_color(Color c)
	{
		text_color = c;
	}
		    
	public Color get_text_color()
	{
		return text_color;
	}
	
	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 void draw(Graphics g) 
	{
		if (selected)
    	{
            g.setColor(Color.RED);
    	}
    	else
    	{	
            g.setColor(get_state_color());
    	}
          
        //         P2
        //         *
        //       *   * 
        // P1  *       *  P3
        //       *   *
        //         *
        //         P4
        //
        g.drawLine( (int)p1.getX(), (int)p1.getY()  ,(int)p2.getX() , (int)p2.getY());
        g.drawLine( (int)p2.getX(), (int)p2.getY()  ,(int)p3.getX() , (int)p3.getY());
        g.drawLine( (int)p3.getX(), (int)p3.getY()  ,(int)p4.getX() , (int)p4.getY());
        g.drawLine( (int)p4.getX(), (int)p4.getY()  ,(int)p1.getX() , (int)p1.getY());
        
        //
        g.setFont(new Font("Arial", Font.PLAIN, 14));
        Font currentFont = g.getFont();
        
		String fn1 = currentFont.getName(); 
		int fstyle = currentFont.getStyle();
		int fsize = currentFont.getSize();
			
		
		FontMetrics fm=g.getFontMetrics( currentFont );
		
		int widthInPixels = fm.stringWidth(label);
		int heightInPixels = fm.getHeight();
	    int x = xcentre - (widthInPixels /2);
	    int y = ycentre + (heightInPixels / 2); 
	    g.drawString(label, x , y  );
	    
	    // pour le marquage de la reference
	    g.setFont(new Font("Arial", Font.PLAIN, 10));
	    g.drawString(id, p2.x + (w/4) + 10 , p2.y + (h/4) );
    	
	}
	 
	public String generate_id()
	{
		int max = 100;
		int min = 1;
		Random randomNum = new Random();
		int id = min + randomNum.nextInt(max);
		System.out.println("Random id " + id );
		String tmp = "DESC_"+ Integer.toString(id);
		return tmp;
	 }
	 
	public String getId()
	{
		return id;
	}
	
	
	
    /*
    public boolean contains(int x, int y)
    {
    	// triangle 1 : P1 P2 P3
    	boolean is_inside_triangle1 = isInside(p1.x , p1.y, p2.x , p2.y , p3.x , p3.y , x,  y); 
    	// triangle 1 : P1 P4 P3
    	boolean is_inside_triangle2 = isInside(p1.x , p1.y, p4.x , p4.y , p3.x , p3.y , x,  y); 
    	
    	if( is_inside_triangle1  == true || is_inside_triangle2 == true )
    	{
    		System.out.println("Decision inside");
        	return true;
    	}
    	else
    	{	
    		System.out.println("Decision outside");
    		return false;
    	}	
    }

    
    // le losange peut être decoupé horizontalement pour créer 2 rectangle
    // le point est dans le rectangle si l'aire totale du rectangle est égale à la zome des 3 autres

    
    
    /*
    public boolean contains(double x, double y)
    {
		return selected;
        //double dx = Math.abs(x - center.getX());
        //double dy = Math.abs(y - center.getY());
        //double d = dx / width + dy / height;
        //return d <= 0.5;
    }
    
    //boolean inside = Math.abs(mousePos.x) * radii.y + Math.abs(mousePos.y) * radii.x
   //         <= radii.x * radii.y;
    
  /*  var dx = Math.abs(coords[0] - middle[0]);
    var dy = Math.abs(coords[1] - middle[1]);
    if (dx / size[0] + dy / size[1] <= 1)
      alert("Inside diamond");
    else
      alert("Outside diamond");
	*/
    
    // https://math.stackexchange.com/questions/312403/how-do-i-determine-if-a-point-is-within-a-rhombus
    
    // https://www.geeksforgeeks.org/check-whether-a-given-point-lies-inside-a-triangle-or-not/  bien
    
	
      
	
}
