package org.greenstone.gatherer.download;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.io.*;
import java.util.ArrayList;
import org.greenstone.gatherer.cdm.Argument;
import org.greenstone.gatherer.Configuration;
import org.greenstone.gatherer.DebugStream;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.LocalGreenstone;
import org.greenstone.gatherer.gui.*;
import org.greenstone.gatherer.util.Utility;

public class ServerInfoDialog extends JDialog
{    
    static private Dimension SIZE = new Dimension(500, 300);
    private String url;
    private String mode;
    private JPanel info_list_pane;
    private JScrollPane info_scroll_pane;
    private JDialog info_dialog;
    private Download download;
    private String proxy_url;
    
    public ServerInfoDialog(String url, String proxy_url, String mode, Download download) {
	super();
	this.url = url;
	this.mode = mode;
        this.download = download;
        this.proxy_url = proxy_url;
	this.info_dialog = this;
	
	this.setSize(SIZE);

 	JButton close_button = new GLIButton(Dictionary.get("General.Close"),Dictionary.get("General.Close_Tooltip"));
 	close_button.setEnabled(true); 
	close_button.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent event) {
		    info_dialog.dispose();
		}
	    });
	
	JPanel button_pane = new JPanel();
	button_pane.setLayout(new FlowLayout());
	button_pane.setBorder(BorderFactory.createEtchedBorder());
	button_pane.add(close_button);
	
	info_list_pane = new JPanel();
	info_list_pane.setLayout(new BoxLayout(info_list_pane, BoxLayout.Y_AXIS));
	info_list_pane.setBorder(BorderFactory.createEtchedBorder());
	getServerInfo();

	info_scroll_pane = new JScrollPane(info_list_pane);
	
	JPanel information_pane = new JPanel();
	information_pane.setLayout(new BorderLayout());
	information_pane.setBorder(BorderFactory.createEtchedBorder());
	information_pane.add(info_scroll_pane,BorderLayout.CENTER);
	
	JPanel main_pane = new JPanel();
	main_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
	main_pane.setLayout(new BorderLayout());
	main_pane.add(information_pane,BorderLayout.CENTER);
	main_pane.add(button_pane,BorderLayout.SOUTH);

	Container main_container = this.getContentPane();
	main_container.add(main_pane);
	
	// Show
	Dimension screen_size = Configuration.screen_size;
	setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
	setVisible(true);

    }

    private void getServerInfo()
    {
	ArrayList commands = new ArrayList();
	if (Utility.isWindows()) {
	    commands.add(Configuration.perl_path);
	    commands.add("-S");
	}
	commands.add(LocalGreenstone.getBinScriptDirectoryPath()+"downloadfrom.pl");
	commands.add("-info");
	commands.add("-download_mode");
	commands.add(mode);
	
       	ArrayList all_arg = download.getArguments(true,false);
	for (int i = 0; i < all_arg.size(); i++) {
	    Argument argument = (Argument) all_arg.get(i);
	    
	    if(argument.isAssigned()) {
		commands.add("-" + argument.getName());
		if(argument.getType() != Argument.FLAG) {
		    commands.add(argument.getValue());
		}
	    }   
	}
	
	String[] command = (String[]) commands.toArray(new String[0]);
	DebugStream.println("Getting server info: "+commands);
	try {
	    String [] env = null;
	    Process prcs = null; 
	    Runtime rt = Runtime.getRuntime();
	  
	    if (Utility.isWindows()) {
              	prcs = rt.exec(command);   
	    }
	    else {
		if (proxy_url != null && !proxy_url.equals("")) {          	
		    // Specify proxies as environment variables
		    // Need to manually specify GSDLHOME and GSDLOS also
		    env = new String[4];
                    proxy_url = proxy_url.replaceAll("http://","");
		    env[0] = "http_proxy=http://"+proxy_url;
		    env[1] = "ftp_proxy=ftp://"+proxy_url;
		    env[2] = "GSDLHOME=" + Configuration.gsdl_path;
		    env[3] = "GSDLOS=" + Gatherer.client_operating_system;
		    prcs = rt.exec(command, env); 
		} 
		else {
		    // Will inherit the GLI's environment, with GSDLHOME and GSDLOS set
		    prcs = rt.exec(command);
		}
	    }

	    InputStreamReader isr = new InputStreamReader(prcs.getErrorStream());
	    BufferedReader br = new BufferedReader(isr);
	    String line;
	    // Capture the standard error stream and seach for two particular occurances.
	    while ((line = br.readLine()) != null && !line.trim().equals("<<Finished>>")) {
	      
		JLabel a_label = new JLabel(line);
		a_label.setAlignmentX(Component.LEFT_ALIGNMENT);
		info_list_pane.add(a_label);
	    }
	    
	} catch (Exception ioe) {
	  
	}
	
    }

}
