package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

public class Prompt extends Task{
    private String property = null;
    private String type = null;
    private String mode = null;
    private Vector list = new Vector();

    public void execute() throws BuildException{
        if(property==null)throw new BuildException("property must be set.", location);
        String value = project.getProperty(property);

        Prompter p;
        if(mode!=null&&mode.equals("gui")){
            p = new SwingPrompt();
        }else{
            p = new CommandLinePrompt();
        }
        value = p.prompt();

        if(value!=null)
            project.setProperty( property, value);


    }
    public void setProperty(String property){
        this.property = property;
    }
    public void setType(String type){
        this.type = type;
    }
    public void setMode(String mode){
        this.mode = mode;
    }
    public Value createValue(){
        Value value = new Value();
        list.addElement(value);
        return value;
    }
    public class Value{
        private String value = null;
        private String label = null;
        public void setLabel(String label){
            this.label=label;
        }
        public void setValue(String value){
            this.value=value;
        }
        public String getLabel(){
            return label;
        }
        public String getValue(){
            return value;
        }
        public String toString(){
            if(label!=null)
                return label;
            return value;
        }
    }
    private interface Prompter{
        public String prompt() throws BuildException;
    }
    private class SwingPrompt implements Prompter{
        public String prompt() throws BuildException{
            String value = null;
            if(type==null||type.equals("plain")){
                value = (String)JOptionPane.showInputDialog(
                    null,
                    "Set value for " + property,
                    "Ant",
                    3,
                    null,
                    null,
                    value
                );
            }else if(type.equals("file")){
                JFileChooser chooser = new JFileChooser();
                chooser.showDialog(null, "Select file");
                chooser.setFileSelectionMode(chooser.FILES_AND_DIRECTORIES);
                if(chooser.getCurrentDirectory()!=null)
                    value = chooser.getCurrentDirectory().getName();
                if(chooser.getSelectedFile()!=null)
                    value = chooser.getSelectedFile().getName();
            }else if(type.equals("list")){
                Object[] o = list.toArray(new Object[list.size()]);
                Value v = (Value)JOptionPane.showInputDialog(
                    null,
                    "Set value for " + property,
                    "Ant",
                    3,
                    null,
                    o,
                    value
                );
                if(v!=null)
                    value = v.getValue();
            }
            return value;
        }
    }
    private class CommandLinePrompt implements Prompter{
        public String prompt() throws BuildException{
            String value = null;
            if(type==null||type.equals("plain")||type.equals("file")){
                    System.out.print("Enter value for " + property + ": ");
                    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                    try{
                        value = in.readLine();
                    }catch(Exception e){
                        throw new BuildException(e);
                    }
            }else if(type.equals("list")){
                int i=0;
                for(Enumeration e = list.elements();e.hasMoreElements();){
                    System.out.println(++i+": "+ e.nextElement());
                }
                System.out.println("Enter the number of the desired value: ");
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                try{
                    value = in.readLine();
                }catch(Exception e){
                    throw new BuildException(e);
                }
                try{
                    value=((Value)list.elementAt(Integer.parseInt(value)-1)).getValue();
                }catch(Exception e){
                    //e.printStackTrace();
                }
            }
            return value;
        }
    }
}
