import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.JFileChooser;

import jsdai.SMachining_schema.ECartesian_point;
import jsdai.SMachining_schema.SMachining_schema;
import jsdai.lang.A_double;
import jsdai.lang.SdaiException;
import jsdai.lang.SdaiModel;
import jsdai.lang.SdaiRepository;
import jsdai.lang.SdaiSession;


public class CartesianPoint extends Main_Interface implements ActionListener {
		
	private String txt;
	private static int num_coordinates = 0; 
	private static double X,Y,Z;
	private static String output = null;
    private Vector coordenadas = new Vector();
	private int j = 1;
    
	Window help = new Help_Interface();
	Window add = new Add_coordinate();
	
	public void init1(){
		adicionar_coordenada.addActionListener(this);
		salvar_arquivo.addActionListener(this);
		gerar_step.addActionListener(this);
		abrir_localmente.addActionListener(this);
		novo.addActionListener(this);
		ok.addActionListener(this);
		help_coordinates.addActionListener(this);
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		Object source = e.getSource();
		if(gerar_step.equals(source)|ok.equals(source)){
					
			coordenada_x.getText();
			coordenada_y.getText();
			coordenada_z.getText();
	     	
		    X = Double.parseDouble(coordenada_x.getText());
			Y = Double.parseDouble(coordenada_y.getText());
			Z = Double.parseDouble(coordenada_z.getText());
		
		
			imprimeCartesianPoint();
			txt_step.setEditable(false);
					   
		    coordenadas.clear();		    			  
		    j = 1;
			num_coordinates = 0;
							
		}
		if(adicionar_coordenada.equals(source)){
			
			coordenada_x.getText();
			coordenada_y.getText();
			coordenada_z.getText();
	     	
		    X = Double.parseDouble(coordenada_x.getText());
			Y = Double.parseDouble(coordenada_y.getText());
			Z = Double.parseDouble(coordenada_z.getText());
			
			coordenadas.addElement(X);
			coordenadas.addElement(Y);
			coordenadas.addElement(Z);
						
			num_coordinates ++;
			add.setVisible(true);
		}
		if(salvar_arquivo.equals(source)){
			savefile();									
		}
		if(abrir_localmente.equals(source)){
			openfile();
			txt_step.setEditable(false);
		}
		if(novo.equals(source)){
			txt_step.setEditable(true);
			txt_step.setText("");
		}
		if(help_coordinates.equals(source)){
			help.setVisible(true);
		}
		
	}	
	
	public void imprimeCartesianPoint(){
		
		int indice_vetor = 0;
		String txtcompleto = "";
		while(j <= num_coordinates){
			
			txt = "#"+j+"=CARTESIAN_POINT('Robo',"+coordenadas.get(indice_vetor)+","+coordenadas.get(indice_vetor + 1)+","+coordenadas.get(indice_vetor + 2)+"));";
			txtcompleto = txtcompleto + txt +"\n";
			
			j++;
			indice_vetor = indice_vetor  + 3;
		}
		txtcompleto = "DATA;"+"\n"+txtcompleto+"ENDSEC;"+"\n"+"END-ISO-10303-21;";
		txt_step.setText(txtcompleto);
		
	}
	
	public void openfile() {	
		
		final JFileChooser chooseText = new JFileChooser();
		int returnVal = chooseText.showOpenDialog(CartesianPoint.this);
		
		if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooseText.getSelectedFile();
            //This is where a real application would open the file.            
            if(true){
            getFileString(file);
            txt_step.setText(output);                 	
            }
		}
   	} 
	
	public void savefile(){
		String step_data;
		step_data = txt_step.getText();
		JFileChooser savestp = new JFileChooser();
		if(savestp.showSaveDialog(CartesianPoint.this) != JFileChooser.APPROVE_OPTION)
            return;        
         File arquivo = savestp.getSelectedFile();
         if(arquivo == null)
             return;
         FileWriter writer = null;
         try {
             writer = new FileWriter(arquivo);
             writer.write(step_data);
         }catch(IOException e){
        	 
         }
         finally {
             if(writer != null){
                try{
                    writer.close();
                } 
                catch (IOException x){
                       
             }
             }
          }
             
	}                	
	public static String getFileString(File file){
		try {
			FileReader reader = new FileReader(file);
			BufferedReader leitor = new BufferedReader(reader);

			Scanner s = new Scanner(leitor);
			String arquivo = "";

			while (s.hasNextLine()) {
				arquivo += s.nextLine();
				arquivo += "\n";				
			}			
			output = arquivo;
		}
		catch (Exception e) {			
		}
		return output;
	}
	
	public CartesianPoint(){		
		init1();
	}	
}
