
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JSeparator;


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;



import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Frame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class SettingsStates extends JDialog 
{
	
	private JTextField champ_state_name, champ_state_expression;
	private boolean close_Jdialog = false;
	private Node node;
	private String state_name;
	private String state_expression;
	
	private ArrayList copy_node;
	
	private Color current;
	
	private Color state_newColor;
	private Color text_newColor;
	
	private Color current_state_Color;
	private Color current_text_Color;
	
	private JPanel panel_state_color;
	private JPanel panel_text_color;

	
	public SettingsStates(JFrame f, boolean modal, Node node) // , ArrayList xxxxcopy_node)  // passage par initialise et pas par le constructeur
	{
       super(f, modal);
       
       this.node = node;
       //this.copy_node = copy_node;
       
       panel_state_color = new JPanel();
       panel_text_color = new JPanel();
       
       // decalage par rapport au frame
       Point location = f.getLocation();
       setLocation(location.x + 250, location.y + 250);

       // fermer la fenetre
       addWindowListener(new java.awt.event.WindowAdapter() 
       {
           public void windowClosing(java.awt.event.WindowEvent e) 
           {
        	   dispose();
           }
       });
       
       init();
   
	}

	// envoi les datas
	public void initialise( Object [] o)
	{
		 copy_node = (ArrayList)o[0];
		 
		 //current_state_Color = (Color)o[1];
		 //current_text_Color  = (Color)o[2];
		 //System.out.println("Chargement  " + copy_node + " " + current_state_Color + " " + current_text_Color);
     	 //panel_state_color.setBackground(current_state_Color);
    	 //panel_text_color.setBackground( current_text_Color);
	}
 
	// retourne les datas
	public Object [] getDonnees()
	{
		Color defaultColor = panel_state_color.getBackground();
		
		//return new Object[]{ state_name, close_Jdialog , state_newColor,   text_newColor };
		return new Object[]{ state_name, state_expression , close_Jdialog , panel_state_color.getBackground(),   panel_text_color.getBackground() };

	}
  
	private void init()
	{
	  	setResizable( false );
	  	setBounds(100, 100, 600, 460);
		getContentPane().setLayout(null);
		
		current_state_Color =	node.getStateColor();
		current_text_Color  = node.getTextColor();
		 
	    System.out.println("Chargement des couleurs " + current_state_Color + " " + current_text_Color);
			 
     	panel_state_color.setBackground(current_state_Color);
    	panel_text_color.setBackground( current_text_Color);
		
		ImageIcon icon = null;
		icon = createImageIcon("resources/settings.png", "");
		setIconImage( icon.getImage() );
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Settings state parameters ");
		
		// chargement des paramétres
		state_name = node.getLabel(); 
		
		JLabel lbl_state_name = new JLabel("State name :");
		lbl_state_name.setBounds(27, 24, 100, 14);
		getContentPane().add(lbl_state_name);
		
		champ_state_name = new JTextField();
		champ_state_name.setBounds(120, 21, 120, 20);
		getContentPane().add(champ_state_name);
		champ_state_name.setColumns(10);
		champ_state_name.setText(state_name);
		
		/**/
		JCheckBox chckbx_state_name = new JCheckBox(" Visible :");
		chckbx_state_name.setBounds(250, 21, 97, 23);
		getContentPane().add(chckbx_state_name);
		chckbx_state_name.setSelected( node.getVisible_name()  );
		chckbx_state_name.addItemListener(new ItemListener() 
		{    
			public void itemStateChanged(ItemEvent arg0)
            {                 
               //label.setText("C++ Checkbox: "  + (e.getStateChange()==1?"checked":"unchecked"));
				// check state
				if (chckbx_state_name.isSelected())
				{
					//chckbx_state_name.setSelected(false);
					node.setVisible_name(true);	
				} 
				else 
				{
					//chckbx_state_name.setSelected(true);
					node.setVisible_name(false);
				}
				
				//boolean ty = chckbx_state_name.getState();
				
				//node.setVisible_name( chckbx_state_name.getState() )
				
				// https://www.javaexamples.org/java/java.awt/getstate-in-checkbox.html
				
            }
         });    
		
		/*******************/
				
		state_expression = node.getExpression(); 
		JLabel lbl_state_expression = new JLabel("State equation :");
		lbl_state_expression.setBounds(27, 58, 100, 14);
		getContentPane().add(lbl_state_expression);
		
		champ_state_expression = new JTextField();
		champ_state_expression.setBounds(120, 55, 120, 20);
		getContentPane().add(champ_state_expression);
		champ_state_expression.setColumns(10);
		champ_state_expression.setText(state_expression);
		
		JCheckBox chckbx_state_expression = new JCheckBox(" Visible :");
		chckbx_state_expression.setBounds(250, 55, 97, 23);
		getContentPane().add(chckbx_state_expression);
		chckbx_state_expression.setSelected( node.getVisible_expression()  );
		chckbx_state_expression.addItemListener(new ItemListener() 
		{    
			public void itemStateChanged(ItemEvent arg0)
            {                 
               //label.setText("C++ Checkbox: "  + (e.getStateChange()==1?"checked":"unchecked"));
				// check state
				if (chckbx_state_expression.isSelected())
				{
					//chckbx_state_name.setSelected(false);
					node.setVisible_expression(true);
				} 
				else 
				{
					//chckbx_state_name.setSelected(true);
					node.setVisible_expression(false);
				}
            }
         });    

		
		
		/*******************/
		
		JLabel lbl_StateColor = new JLabel("State color  :");
		lbl_StateColor.setBounds(27, 90, 120, 14);
		getContentPane().add(lbl_StateColor);
		panel_state_color.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_state_color.setBackground(current_state_Color);
		panel_state_color.setBounds(120, 90, 60, 34);
		getContentPane().add(panel_state_color);
		
		panel_state_color.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				state_newColor = JColorChooser.showDialog(null, "Choose a color", current);
				panel_state_color.setBackground(state_newColor);
			}
		});
		
		JLabel lbl_TextColor = new JLabel("Text color :");
		lbl_TextColor.setBounds(27, 130, 100, 14);
		getContentPane().add(lbl_TextColor);
		panel_text_color.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_text_color.setBackground( current_text_Color);
		panel_text_color.setBounds(120, 130, 60, 34);
		getContentPane().add(panel_text_color);
		
		panel_text_color.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				text_newColor = JColorChooser.showDialog(null, "Choose a color", current);
				panel_text_color.setBackground(text_newColor);
			}
		});
		
		/*******************/
		
		JSeparator separator = new JSeparator();
		separator.setBounds(1, 384, 580, 2);
		getContentPane().add(separator);
	
		/*******************/
		
		JButton btn_ok = new JButton("OK");
		btn_ok.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				state_name = champ_state_name.getText();
				state_expression = champ_state_expression.getText();
				
				
				int len = state_name.length();
	            if(len == 0)    
	            {    
	               JOptionPane.showMessageDialog(null,  "Le champ est vide, le state n'a pas été mis à jour !", "Attention", JOptionPane.ERROR_MESSAGE);
	            }  
	            else
	            {
	            	// check si le nom du state existe dèjà !
	            	boolean flag_doublon = false;
	            	 
	            	for(int i = 0; i < copy_node.size(); i++)
	                {
	            		 Node obj_node = (Node) copy_node.get(i);
	            		 obj_node.getLabel().toLowerCase();
	            		 //String existing_state_name = copy_node.get(i).getLabel().toLowerCase();
	            		 String existing_state_name =  obj_node.getLabel().toLowerCase();
	            		 String new_state_name = state_name.toLowerCase();
		                 if(existing_state_name.equals( new_state_name ) )
	                     {
	                         JOptionPane.showMessageDialog(null,  "Deux états be peuvent avoir le même nom \nCe nom existe déjà !.", "Attention", JOptionPane.ERROR_MESSAGE);
	                         flag_doublon = true;
	                         break;
	                     }
	                }
	            		            	 
	            	if( flag_doublon == false)
	                {    
	     				close_Jdialog = true;
	                	System.out.println("Btn OK  \n");
		 				dispose();
	                }
	             }
			}
		});
		btn_ok.setBounds(135, 391, 89, 23);
		getContentPane().add(btn_ok);
	
		/*******************/
		
		JButton btn_cancel = new JButton("Cancel");
		btn_cancel.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				close_Jdialog = false;
				System.out.println("Btn Cancel  \n");
				dispose();
			}
		});
		btn_cancel.setBounds(359, 391, 89, 23);
		getContentPane().add(btn_cancel);
		
		/*******************/
	}
    
	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;
		}
	}
  
}