
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 java.awt.event.ActionListener;
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 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;

public class titi extends JDialog //implements ActionListener

{

	private Color current;
	private JTextField text_start_state;
	private JTextField txt_end_state;
	private JTextField text_name;
	private JTextField text_equation;
	
	private JTextField champ_setx;
	private JTextField champ_sety;
	
	private boolean status_shift_text_auto;
	private boolean status_shift_text_manual;
	private int shift_text_X;  
	private int shift_text_Y;
	
	private boolean OK_close_Jdialog = false;

	Integer lEntier = new Integer(0);
	String leNom;
  
	private EdgeObj edge;

	private static final Image iPrintManager = Toolkit.getDefaultToolkit().getImage( "ressources/arrow.png" );
	
  public titi(JFrame f, boolean modal, EdgeObj  edge) 
  {
       super(f, modal);
       
       
       this.edge = edge;
       
       
       // 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();
           }
       });
       
       //jbInit();
       init();
   
  }

  // envoi les datas
  public void initialise( Object [] o)
  {
      lEntier = (Integer)o[0];
      leNom = (String)o[1];
      //jTextField1.setText(lEntier.toString());
      //jTextField2.setText(leNom);
  }
 
  // retourne les datas
  public Object [] getDonnees()
  {
      return new Object[]{OK_close_Jdialog, lEntier , leNom};

  }

//  public void show_dialog()
//  {
//       setVisible(true);
//  }
  
  
  private void jbInit() //throws Exception 
  {
	  
	  	setBounds(100, 100, 600, 460);
		getContentPane().setLayout(null);
		//{
		//}
	
		JPanel panel = new JPanel();
		
		//setPreferredSize( new Dimension( 640, 480 ) );
		setPreferredSize( new Dimension( 600, 460 ) );
		
		setResizable( false );
	
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Debug Edit tansistion Properties");
	
		panel.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				Color newColor = JColorChooser.showDialog(null, "Choose a color", current);
				panel.setBackground(newColor);
			}
		});
	
	
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(44, 324, 60, 34);
		getContentPane().add(panel);
	
		JLabel lbl_comments = new JLabel("Comments");
		lbl_comments.setBounds(44, 35, 72, 14);
		getContentPane().add(lbl_comments);

	  
		JLabel lbl_start_state = new JLabel("Start state :");
		lbl_start_state.setBounds(44, 254, 72, 14);
		getContentPane().add(lbl_start_state);
	
		JLabel lbl_end_state = new JLabel("end state :");
		lbl_end_state.setBounds(44, 285, 72, 14);
		getContentPane().add(lbl_end_state);
	
		text_start_state = new JTextField();
		text_start_state.setEditable(false);
		text_start_state.setBounds(113, 251, 86, 20);
		getContentPane().add(text_start_state);
		text_start_state.setColumns(10);
	
		txt_end_state = new JTextField();
		txt_end_state.setEditable(false);
		txt_end_state.setColumns(10);
		txt_end_state.setBounds(113, 282, 86, 20);
		getContentPane().add(txt_end_state);
	
		JSeparator separator = new JSeparator();
		separator.setBounds(1, 384, 600, 2);
		getContentPane().add(separator);
	
		/*****/
		JButton btn_ok = new JButton("OK");
		btn_ok.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				System.out.println("Btn OK  \n");
				OK_close_Jdialog = true;
				//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) 
			{
				System.out.println("Btn Cancel  \n");
				OK_close_Jdialog = false;
				//dispose();
			}
		});
		btn_cancel.setBounds(359, 391, 89, 23);
		getContentPane().add(btn_cancel);
	
		/*****/

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(44, 58, 201, 171);
		getContentPane().add(scrollPane);
	
		JTextArea textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		textArea.setRows(20);
		textArea.setColumns(40);
		
		JLabel lbl_state_name = new JLabel("Name");
		lbl_state_name.setBounds(292, 64, 65, 14);
		getContentPane().add(lbl_state_name);
		
		JLabel lbl_equation = new JLabel("Equation :");
		lbl_equation.setBounds(292, 97, 65, 14);
		getContentPane().add(lbl_equation);
		
		text_name = new JTextField();
		text_name.setBounds(388, 61, 120, 20);
		getContentPane().add(text_name);
		text_name.setColumns(10);
		
		text_name.setText("Blabla");
		
		text_equation = new JTextField();
		text_equation.setBounds(388, 94, 120, 20);
		getContentPane().add(text_equation);
		text_equation.setColumns(10);
		
		JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
		chckbxNewCheckBox.setBounds(43, 153, 97, 23);
		getContentPane().add(chckbxNewCheckBox);
		
		JCheckBox chckbxNewCheckBox_1 = new JCheckBox("New check box");
		chckbxNewCheckBox_1.setBounds(43, 179, 97, 23);
		getContentPane().add(chckbxNewCheckBox_1);
		
  }
  
  private void init()
  {
	  	setResizable( false );
	  	setBounds(100, 100, 600, 460);
		getContentPane().setLayout(null);
		//{
		//}
		
		
		int shift_text_X = edge.get_shift_text_X(); 
		int shift_text_Y = edge.get_shift_text_Y();
		
		status_shift_text_auto = edge.get_shift_mode_auto();
		status_shift_text_manual = edge.get_shift_mode_manual();
		
		
		ImageIcon icon = null;
		icon = createImageIcon("resources/settings.png", "");
		setIconImage( icon.getImage() );
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Settings transistion text ");
	
		JLabel lbl_comments = new JLabel("Shift position");
		lbl_comments.setBounds(44, 35, 72, 14);
		getContentPane().add(lbl_comments);
	
		/*******************/
		
		JLabel lbl_state_name = new JLabel("X:");
		lbl_state_name.setBounds(44, 64, 24, 14);
		getContentPane().add(lbl_state_name);
		
		JLabel lbl_equation = new JLabel("Y:");
		lbl_equation.setBounds(44, 97, 24, 14);
		getContentPane().add(lbl_equation);

		champ_setx = new JTextField();
		champ_setx.setBounds(68, 60, 150, 20);
		getContentPane().add(champ_setx);
		champ_setx.setColumns(10);
		// setting
		champ_setx.setText( String.valueOf( shift_text_X ) );
		
		champ_sety = new JTextField();
		champ_sety.setBounds(68, 93, 150, 20);
		getContentPane().add(champ_sety);
		champ_sety.setColumns(10);
		// setting
		champ_sety.setText( String.valueOf( shift_text_Y ) );
		
		/*******************/
		
		JRadioButton RadioButton_auto = new JRadioButton("Auto set text");
		RadioButton_auto.setBounds(44, 165, 120, 23);
		getContentPane().add(RadioButton_auto);
		// setting
		RadioButton_auto.setSelected( status_shift_text_auto );
		
		JRadioButton RadioButton_manual = new JRadioButton("Manual set text");
		RadioButton_manual.setBounds(44, 201, 120, 23);
		getContentPane().add(RadioButton_manual);
		// setting
		RadioButton_manual.setSelected( status_shift_text_manual );
		
		ButtonGroup group = new ButtonGroup();
        group.add(RadioButton_auto);
	    group.add(RadioButton_manual);

//	    group.setSelected((ButtonModel) RadioButton_manual, true);
	    
		/*******************/
	    
	    //RadioListener myListener = new RadioListener();
	    //RadioButton_auto.addActionListener(myListener);
	    //RadioButton_manual.addActionListener(myListener);
	
	    RadioListener GroupListener = new RadioListener();
	    RadioButton_auto.addActionListener(GroupListener);
	    RadioButton_manual.addActionListener(GroupListener);
	    
		/*
	    group.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				
				if (e.getActionCommand().equals("Check")) 
				{
			    	            System.out.println("Selected Radio Button: " +group.getSelection().getActionCommand());
	    	        }
				
			}
		});
	    
	   */
	
		/*******************/
		
		/*JCheckBox chk_box_set_text = new JCheckBox("Set text");
		chk_box_set_text.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				
				boolean isSelected = chk_box_set_text.isSelected();
				if(isSelected)
				{
					System.out.println("Manual settings");
				}
				else
				{
					System.out.println("Auto Settings");
				}
			}
		});
		chk_box_set_text.setBounds(38, 127, 97, 23);
		getContentPane().add(chk_box_set_text);
		*/
		
		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) 
			{
				/*****/
				edge.set_shift_text_X( Integer.valueOf( champ_setx.getText() ) ); 
				edge.set_shift_text_Y( Integer.valueOf( champ_sety.getText() ) ); 
				edge.set_shift_mode_auto( status_shift_text_auto );
				edge.set_shift_mode_manual( status_shift_text_manual );

				boolean  a = status_shift_text_auto; 
				boolean  b = status_shift_text_manual;

				
				System.out.println("Enregistre les paramétres \n " + champ_setx.getText() + " " + champ_sety.getText() + " " + status_shift_text_auto + " " + status_shift_text_manual);
				/*****/
				
				OK_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) 
			{
				OK_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;
  	}
  }
 
  /*
  class RadioListener implements ActionListener
  {
	    public void actionPerformed(ActionEvent e) 
	    {
	    	if (e.getActionCommand().equals("Auto set text")) 
			{
		    	            System.out.println("un"); 
			}
	    	
	    	if (e.getActionCommand().equals("Manual set text")) 
			{
		    	            System.out.println("deux"); 
			}
	    	
	    }
	}
*/
  
  class RadioListener implements ActionListener 
  {
      public void actionPerformed(ActionEvent e) 
      {
    	  if (e.getActionCommand().equals("Auto set text")) 
			{
   	            System.out.println("Auto mode selected"); 
   	        	status_shift_text_auto = true;
   	        	status_shift_text_manual = false;
			}
	    	
	    	if (e.getActionCommand().equals("Manual set text")) 
			{
	            System.out.println("Manual mode selected"); 
	        	status_shift_text_auto = false;
	        	status_shift_text_manual = true;
			}

      }
  }
  
  
  
}