import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class Toolbar extends JToolBar
{
	
	private JButton search_btn ;
	private JButton rectangle_btn ;
	private JButton text_btn ;
	private JButton line_btn ;
	private JButton circle_btn;
	private JButton delete_btn ;
	private JButton square_btn;
	private JButton connection_btn;
	private JButton properties_btn;
	private JButton decision_btn;
	private JButton polygon_btn;
	private JButton segment_btn;
	
	//private  Icon search_img;
	//private  Icon rectangle_img;
	//private  Icon text_img;
	//private  Icon line_img;
	//private  Icon circle_img;
	//private  Icon delete_img;
	
	private String action_selected = "";
	
    public Toolbar()
    {
        
        ImageIcon icon = null;
       
        icon = createImageIcon("resources/search.png", "");
        search_btn = new JButton("",icon);
        search_btn.setToolTipText("Search");
        add(search_btn);
        
        icon = createImageIcon("resources/rectangle.png", "");
        rectangle_btn = new JButton("",icon);
        rectangle_btn.setToolTipText("Draw rectangle");
        add(rectangle_btn);
      
        icon = createImageIcon("resources/text.png", "");
        text_btn = new JButton("",icon);
        text_btn.setToolTipText("Draw text");
        add(text_btn);
        
        icon = createImageIcon("resources/arrow.png", "");
        line_btn = new JButton("",icon);
        line_btn.setToolTipText("Draw line");
        add(line_btn);
      
        icon = createImageIcon("resources/circle.png", "");
        circle_btn = new JButton("",icon);
        circle_btn.setToolTipText("Draw circle");
        add(circle_btn);
        
        icon = createImageIcon("resources/remove.png", "");
        delete_btn = new JButton("",icon);
        delete_btn.setToolTipText("Delete");
        add(delete_btn);
        
        icon = createImageIcon("resources/rounded-square.png", "");
        square_btn = new JButton("",icon);
        square_btn.setToolTipText("Draw box");
        add(square_btn);

        icon = createImageIcon("resources/connection.png", "");
        connection_btn = new JButton("",icon);
        connection_btn.setToolTipText("Draw connection");
        add(connection_btn);
        
        
        icon = createImageIcon("resources/properties.png", "");
        properties_btn = new JButton("",icon);
        properties_btn.setToolTipText("Properties...");
        add(properties_btn);
        
        icon = createImageIcon("resources/decision.png", "");
        decision_btn = new JButton("",icon);
        decision_btn.setToolTipText("Decision...");
        add(decision_btn);
        
        icon = createImageIcon("resources/polygon.png", "");
        polygon_btn = new JButton("",icon);
        polygon_btn.setToolTipText("Polygon...");
        add(polygon_btn);
        
        icon = createImageIcon("resources/line.png", "");
        segment_btn = new JButton("",icon);
        segment_btn.setToolTipText("Line...");
        add(segment_btn);
        
        search_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button Search...."); 
            	
            	java.awt.Container c = getParent();  
    	    	 while (!(c instanceof javax.swing.JFrame) && (c!=null)) 
    	    	 {  
    	    	         c = c.getParent();  
    	    	 }  
    	    	 if (c!=null) 
    	    	 { 
    	    	    JFrame frame_parent =(javax.swing.JFrame) c;  
                	DialogPreferences dialog_windows = new DialogPreferences(frame_parent, true ) ; 
    	    	 	dialog_windows.setVisible(true);
    	    	 }
	    	 
	    	 }
            
        });
 
        
        text_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	set_action_selected("TEXT");
            	
                // boite de dialogue pour une nouvelle saisie
            	TextDialog dialog = new TextDialog("");
             	dialog.setModal(true);
             	dialog.setVisible(true);
             	
           	    // récupére
             	String saisie = dialog.getText();
             	//System.out.println(saisie);
             	
             	// stockage temporaire dans la classe transfert
             	split02.transfert_set(saisie);
            }
        });
        
        
        rectangle_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button Rectangle...."); 
            	set_action_selected("RECTANGLE");
            }
        });
        
        line_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button line...."); 
            	set_action_selected("LINE");
            }
        });
        
        circle_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button circle...."); 
            	set_action_selected("CIRCLE");
            }
        });
        
        delete_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button delete...."); 
            	set_action_selected("DELETE");
            }
        });
 
        square_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button square...."); 
            	set_action_selected("STATE");
            }
        });
        
        connection_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button connection...."); 
            	set_action_selected("CONNECTION");
            }
        });
        
        
        properties_btn.addActionListener(new ActionListener() 
        {
        	public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button properties...."); 
            	
            	java.awt.Container c = getParent();  
    	    	 while (!(c instanceof javax.swing.JFrame) && (c!=null)) 
    	    	 {  
    	    	         c = c.getParent();  
    	    	 }  
    	    	 if (c!=null) 
    	    	 { 
    	    	    JFrame frame_parent =(javax.swing.JFrame) c;  
                	pipo aaa = new pipo();
    	    	    aaa.setVisible(true);
    	    	    
    	    	    aaa.addChangeListener(new  ChangeListener()
    	            {
    	               public void stateChanged(ChangeEvent event)
    	               {
    	                  repaint();
    	               }
    	            });
    	    	    
    	    	    
    	    	    
    	    	    //Demo demo= new Demo();
    	    	    //demo.getField().getDocument().addDocumentListener( new MonDocListener(demo));
    	    	    
    	    	    
                	//DialogPreferences dialog_windows = new DialogPreferences(frame_parent, true ) ; 
    	    	 	//dialog_windows.setVisible(true);
    	    	 }
	    	 
	    	 }
            
        });
        
        decision_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button decision...."); 
            	set_action_selected("DECISION");
            }
        });
        
        polygon_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button polygon...."); 
            	set_action_selected("POLYGON");
            }
        });
        
        
        segment_btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
            	System.out.println("Click button segment...."); 
            	set_action_selected("SEGMENT");
            }
        });
    }
    
    public ImageIcon createImageIcon(String path,  String description) 
    {
    	URL imgURL = getClass().getResource(path);

    	if (imgURL != null)
    	{
    			return new ImageIcon(imgURL, description);
    	} 
    	else 
    	{
    		System.err.println("Couldn't find file: " + path);
    		return null;
    	}
    }
    
    public void set_action_selected(String value)
    {
    	action_selected = value;
    }
    
    public String get_action_selected()
    {
    	return action_selected;
    }
  
}