/*
 * AskUser.java
 *
 * Created on March 16, 2001, 4:39 PM
 */

package com.sns.tools.ant.taskdefs;

import org.apache.tools.ant.taskdefs.Taskdef;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;

import java.util.Properties;
import java.util.Date;

/**
 * Asks the user for input.
 *
 *Usage:
 *<pre>
 *      <ask-user question="Who are you?"
 *                  default="Me"
 *                  property="user.name"
 *                  answerfile="answers.properties"
 *                  record="true"
 *      />
 *</pre>
 *
 * @author  <a href="mailto:John.D.Casey@mail.sprint.com">John Casey</a>
 * @version 1.0
 */
public class AskUser extends Taskdef {
    private String propertyName;
    private String question;
    private String defaultAnswer;
    private File answerFile;
    private boolean record;
    
    private Properties props;
    
    /**
     * Sets the prompt to be displayed.
     */
    public void setQuestion(String question){
        this.question = question;
    }
    
    /**
     * Sets the default value (if the user just presses Enter).
     */
    public void setDefault(String defaultAns){
        this.defaultAnswer = defaultAns;
    }
    
    /**
     * Sets the property to store the user's answer under.
     */
    public void setProperty(String property){
        this.propertyName = property;
    }
    
    /**
     * Sets the file to get default values from, if the default attribute
     * is not set. Also, if the record attribute is true, this file is
     * where the user's answers will be stored.
     */
    public void setAnswerfile(File f){
        this.answerFile = f;
    }
    
    /**
     * Sets the record flag.  This tells the task whether or not to trap
     * the user's input to the specified answer file.
     *
     * NOTE: This <b>must</b> be used in conjunction with the answerfile
     * attribute in order for recording to be turned on for this prompt.
     */
    public void setRecord(boolean record){
        this.record = record;
    }
    
    /**
     * Executes the task, prompting the user for input.
     */
    public void execute() throws BuildException{
        try{
            //initialize the answerfile manager, and register this task's
            //presence.
            AnswerFileManager mgr = AnswerFileManager.getInstance(this);
            if(answerFile != null){
                try{
                    props = mgr.getAnswerFile(answerFile);
                    if(defaultAnswer == null){defaultAnswer = props.getProperty(propertyName);}
                }
                catch(Exception ex){
                    log(ex.getMessage(), project.MSG_VERBOSE);
                }
            }
            
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String val = null;
            while(val == null){
                String ask = question + " ";
                if(defaultAnswer != null){ask += "[" + defaultAnswer + "]";}
                System.out.print(ask);
                System.out.flush();
                val = in.readLine();
                if((val != null) && (val.equals(""))){
                    if(defaultAnswer == null){
                        val = null;
                    }
                    else{
                        val = defaultAnswer;
                    }
                }
            }
            
            project.setProperty(propertyName, val);
            
        //if the task is set to record...
            if((answerFile != null) && (record)){
                try{
                //if we haven't initialized the properties object yet...
                    if(props == null){mgr.getAnswerFile(answerFile);}
                }
            //if for some reason the properties object couldn't be
            //retrieved...
                catch(Exception ex){
                    //don't send on to user, except in verbose mode.
                    log(ex.getMessage(), project.MSG_VERBOSE);
                    props = new Properties();
                    mgr.setAnswerFile(answerFile, props);
                }
                props.setProperty(propertyName, val);
            }
            mgr.release(this);
        }
        catch(Exception ex){
            throw new BuildException(ex);
        }
    }
            
}
