package voi.ui;

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.URL;
import javax.swing.border.*;
import java.awt.event.*;
import java.sql.*;
import java.lang.System;
import voi.votable.*;
import voi.routines.*;

/**
This class creates a Tool Bar which contains icon to process the query results
The various tools are
1) Save as Vo-Table
2) Save as Ascii and so on
**/
public class TdfToolBar extends JToolBar implements ActionListener 
{

	private GuiController theApp;
	private TdfIcon saveAsVoTableIcon, saveAsAsciiIcon, voPlotIcon;
	private boolean queryFlags [];

	private final Border EDGE = BorderFactory.createRaisedBevelBorder ();	// Button Border
	private final int FORM = 0;
	private final int SQL = 1; 
	
	private int mode = FORM;	
	/**
	Constructs that sets the layout to BoxLayout and sets the floatable to false
	**/	
	public TdfToolBar (GuiController theApp)
	{
		this.theApp = theApp;
		setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
		setBorder (new TitledBorder (new EtchedBorder (),"Tools"));
		setFloatable (false);
	}	
	
	/**
	This function creates all the icons required for the Tool Bar
	**/
	public void createIcons ()
	{
		// save as VOTable icon
		saveAsVoTableIcon = new TdfIcon ("save.gif", "Save in VoTable Format", EDGE);
		addSeparator ();
		add (saveAsVoTableIcon);
		saveAsVoTableIcon.addActionListener (this);
		
		// add the save as Ascii icon
		addSeparator ();
		saveAsAsciiIcon = new TdfIcon ("Save24.gif", "Save in Ascii Format", EDGE);
		add (saveAsAsciiIcon);
		saveAsAsciiIcon.addActionListener (this);
				
		
		//add the VOPlot Icon
		addSeparator ();
		voPlotIcon = new TdfIcon ("voplotlogo1.gif", "Launch VO-Plot", EDGE);
		add (voPlotIcon);
		voPlotIcon.addActionListener (this);
	}
	
	/**
	 This module gets the QueryFlags and the ResultSet from TdfInterface and then creates a object called MetaData.
	 This object along with the ResultSet is passed to the CreateVoTable Class
	**/
	public void writeVoTable (int mode)
	{
		File file = null;
		FileOutputStream fout = null;
		
		file = getSaveAsFile("xml");
		
		if (file != null)
		{
			try
			{
				fout = new FileOutputStream (file.getName()+".xml");
			}
			catch (FileNotFoundException e) 
			{
				System.out.println (" Exception being caught ");
			}
			
			VoMetaData metaData = new VoMetaData ();	
			
		// create the VoTableWriter Object by pasing
		// 1) OutputStream
		// 2) ResultSet
		// 3) VOTableTable
		// 4) queryFlags
			if (mode == FORM)
			{
				ResultSet rs = theApp.getTdfInterface ().getResults ();    // This gets the ResultsSet
				queryFlags = theApp.getTdfInterface ().getQueryFlags (); // get the QueryFlags
				VoTableWriter votableWriter = new VoTableWriter ( fout,
									  	  rs,
										  metaData.createMetaData (queryFlags),
										  queryFlags);
				votableWriter.writeToVoTable ();
			}
			else
			{
				ResultSet rs = theApp.getSqlQueryInterface ().getResults ();
				queryFlags = new ConstructVoFlags (rs).getQueryFlags ();
				VoTableWriter voTableWriter = new VoTableWriter ( fout,
									  	  rs,
										  metaData.createMetaData (queryFlags),
										  queryFlags);
				voTableWriter.writeToVoTable ();
				
			}
		
		}
		
								    
	}
	
	public void writeAsAscii (int mode)
	{
		// code for writing file to Ascii
		File file = null;
		FileOutputStream fout = null;
		
		file = getSaveAsFile("txt");
		
		if (file != null)
		{
			try
			{
				fout = new FileOutputStream (file.getName()+".txt");
			}
			catch (FileNotFoundException e) 
			{
				System.out.println (" Exception being caught ");
			}
		
			
			if (mode == FORM)
			{
				ResultSet rs = theApp.getTdfInterface ().getResults ();    	// This gets the ResultsSet
				queryFlags = theApp.getTdfInterface ().getQueryFlags (); // get the QueryFlags
				AsciiWriter asciiWriter = new AsciiWriter (fout, rs, queryFlags);		// create a class called AsciiWriter and pass 
				asciiWriter.writeSqlData ();							// 1) FileOutputStream fout
			}											// 2) ResultSet rs
			else 
			{
				ResultSet rs = theApp.getSqlQueryInterface ().getResults ();    		// This gets the ResultsSet
				AsciiWriter asciiWriter = new AsciiWriter (fout, rs);	// create a class called AsciiWriter and pass 
				asciiWriter.writeSqlData ();						// 1) FileOutputStream fout				
			}
		}
										
	}

	public File getSaveAsFile (String fileFilter)
	{
		
		boolean returnValue;
		String currentDir = System.getProperty("user.dir");
		JFileChooser voTableChooser = new  JFileChooser (currentDir);
		voTableChooser.addChoosableFileFilter(new MyFilter(fileFilter));
		File file = null;
		
		int returnVal = voTableChooser.showSaveDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) 
		{
			file = voTableChooser.getSelectedFile();
			return file;
		} 
		else 
		{
			System.out.println (" save cancelled ");
			returnValue = false;
		}
		return null;
	}
	
	
	public void launchVoPlot ()
	{
		
		new VOPlotLauncher ();
		
	}
	
	
	/**
	Keep the icons enabled/diabled as and when required 
	*/
	public void enableAll (boolean enableFlag)
	{
		saveAsVoTableIcon.setEnabled (enableFlag);
		saveAsAsciiIcon.setEnabled (enableFlag);
		voPlotIcon.setEnabled (enableFlag);
		// enable the VOPlot Icon
	}
		
		
	public void actionPerformed (ActionEvent ae)
	{	
		Object source = ae.getSource ();
		mode = theApp.getMode ();
		
		/**
		This module creates a VOTable of the data.
		**/ 
		if (source == saveAsVoTableIcon)
		{
			writeVoTable (mode);
		}
		
		if (source == saveAsAsciiIcon)
		{
			writeAsAscii (mode);	
		}
		
		
		if (source == voPlotIcon)
		{
			launchVoPlot ();
		
		}
		
	}	
	

}
