import java.awt.Color;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Text 
{

	private int x, y;
	private String chaine;
	private Color color;
	private boolean selected; 
	private int nlines;
	private int width;
	private int height;
	private Point2D             location;
	
    
    public Text() 
    { 
    	this("", new Point2D.Double(0,0)); 
    }
    
    public Text(String aLabel) 
    { 
    	this(aLabel, new Point2D.Double(0,0) {}); 
  	}

    public Text(Point2D aPoint)
    { 
    	this("", aPoint);
   	}
    
    public Text(double x, double y) 
    { 
    	this("", new Point2D.Double(x,y)); 
   	}
    
    public Text(String aLabel, Point2D aPoint) 
    {
        chaine = aLabel;
        location = aPoint;
      
        selected = false;
    }

    
    public Text(String str, int x, int y, Color c) 
    {
        chaine = str;
        this.x = x;
        this.y = y;
        this.color = c;
        selected = false;
    	nlines  = getLineCount(chaine);
    }

/*	
	public Text(int x, int y, String chaine, Color c)
	{
		this.x = x;
		this.y = y;
		this.chaine = chaine;
		this.color = c;
		this.selected = false;
		
		nlines  = getLineCount(chaine);
	}
*/	
	public  int getLineCount(String text)
	{
		return text.split("[\n|\r]").length;
	}
	 
	public int countLines(String str) 
	{
		if(str == null || str.isEmpty())
		{
		   return 0;
		}
		
		int lines = 1;
		int pos = 0;
		
		while ((pos = str.indexOf("\n", pos) + 1) != 0) 
		{
		    lines++;
		}
		
		return lines;
	 } 
	
	 public void setX(int x)
	 {
		 this.x = x;
	 }
	
	 public int getX()
	 {
		 return x;
	 } 

	 public void setY(int y)
	 {
		 this.y = y;
	 }
	 
	 public int getY()
	 {
		 return y;
	 }
	
	
	 public void setText(String s)
	 {
		 chaine = s;
	 } 
	 
	 public String getText()
	 {
		 return chaine;
	 } 
	 
	 public void setColor(Color c)
	 {
		 color = c;
	 }
	
	 public Color getColor()
	 {
		 return color;
	 }
	
	 public void setSelected(boolean select)
	 {
		 selected = select;
	 }

	 public boolean getSelected()
	 {
		 return selected;
	 }
	
	 public boolean isSelected() 
	 { 
		 return selected; 
	 }
	
	 public void toggleSelected() 
	 { 
		 selected = !selected; 
	 }
	
	public void setWidth(int val)
	{
		width = val;
	}
	
	public int getWidth()
	{
		return width;
	}
	
	public void setHeight(int val)
	{
		height = val;
	}
	
	public int getHeight()
	{
		return height;
	}
	
	public void setUnSelected(boolean state) { selected = state; }
	
    public void saveTo(PrintWriter aFile) 
    {
    	
    	String sauve_str = "";
    	
    	if( nlines != 1)
    	{
    		 String[] temp;
             String delimiter = "\\n";
             temp = chaine.split(delimiter);
             for(int i =0; i < temp.length ; i++)
             System.out.println(temp[i]);
             // String[] array = new String[] { "a", "b", "c" };
             // String joined2 = String.join(",", array);
             String joined = String.join("-", temp);
             sauve_str = joined;
             
    	}
    	else
    	{
    		sauve_str = chaine;
    	}
    	
       
        aFile.println(sauve_str);
        aFile.println(getX());
        aFile.println(getY());
        aFile.println(selected);

    }
    
 // Load a node from given file.  Note that incident edges are not connected
    public static Text loadFrom(BufferedReader aFile) throws IOException 
    {
        Text   aText = new Text();
        String str_build = "";

        String s = aFile.readLine();
        
      //  aaa-bbb-ccc-dddsdsqsd-sqsqfddfsfd---toto

      // chaque tiret correspond à un retour à la ligne
       
      // donc 3 tiret signifie un saut de 3 lignes
        
        if( s.contains("-") == true)
        {
        	
        
             String[] tempArray;
      
             String delimiter = "-";
             tempArray = s.split(delimiter);
        	
//        	String[] pieces_of_string = s.split("*");
        	
        	
        	
        	for(int i = 0; i < tempArray.length; i++ )
        	{
        		
        		if(i < tempArray.length -1 )
        		{ 
        			str_build = str_build + tempArray[i] + "\n"; // pas de \r
        		
        		}
        		else
        		{ 
        			str_build = str_build + tempArray[i] ;
        		
        		}	
        	}
        }
        
        else
        {
        	str_build = s;
        }
        
        
        aText.setText( str_build );
        aText.setX( Integer.parseInt(aFile.readLine()) );
        aText.setY( Integer.parseInt(aFile.readLine()) );
        aText.setSelected(Boolean.valueOf(aFile.readLine()).booleanValue());
      
        return aText;
    }
	
}
