package org.apache.tools.ant.taskdefs.optional.vss;

import org.apache.tools.ant.*;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;

import java.io.File;

public class MSVSSADD extends MSVSS {

    private String m_localPath = null;
    private boolean m_Recursive = false;
    private boolean m_Writable = false;
    private String m_Version = null;
    private String m_Date = null;
    private String m_Label = null;
	private String m_fileToAdd = null;
	private String m_vssFolder = null;
	private String m_ssDir = null;
	private String m_vssPath = null;

    /**
     * Executes the task.
     * <p>
     * Builds a command line to execute ss and then calls Exec's run method
     * to execute the command line.
     */
    public void execute() throws BuildException {
		MSVSSChangeProject vsscp = (MSVSSChangeProject)project.createTask("vsscp");
		vsscp.setSsdir(getSsdir());
		vsscp.setLogin(getLogin());
		vsscp.setVsspath(getVSSPath());
		vsscp.setLocalpath(new Path(project,getLocalPath()));
		vsscp.execute();
        //ss Add local files [-B] [-C] [-D-] [-H] [-I-] [-K] [-N] [-O] [-R] [-W] [-Y] [-?]
		//\\Ocsfile\poetic\document\ss Add buildTest.txt -GLD:\vbhatia_dev\auto_build\poetic_app\src\com\ocs -P$/product_development/version_1/technical_design/backend/com/ocs
		Commandline commandLine = new Commandline();
		int result = 0;
		setSsdir(getSsdir());
		commandLine.setExecutable(getSSCommand());		
	    commandLine.createArgument().setValue(COMMAND_ADD);
	    //commandLine.createArgument().setValue(getVsspath());
		commandLine.createArgument().setValue(getFileToAdd());
	    //getLocalpathCommand(commandLine);
		//commandLine.createArgument().setValue(CHECKIN_PROJECT + getVsspath());
		commandLine.createArgument().setValue("-C-");  // ignore comments
	    commandLine.createArgument().setValue("-I-");  // ignore all errors
	    getLoginCommand(commandLine);
 
	    result = run(commandLine);
	    if ( result != 0 ) {
	        String msg = "Failed executing: " + commandLine.toString();
	        throw new BuildException(msg, location);
	    }
    }
	
	public void setFileToAdd(String fileToAdd) {
		m_fileToAdd = fileToAdd;
	}
	
	public String getFileToAdd() {
		return m_fileToAdd;
	}
	
	public void setLocalPath(String localPath) {
		this.m_localPath = localPath;	
	}
	
	public String getLocalPath() {
		return m_localPath;	
	}
	
	/**
     * set and get the SSDir attribute
     */
	 public void setSSDir(String ssDir) {
	 	this.m_ssDir = ssDir;
	 }
	 
	 public String getSsdir() {
	 	return m_ssDir;
	 }
	 
	 public void setVSSPath(String vssPath) {
	 	this.m_vssPath = vssPath;
	 }
	
	public String getVSSPath() {
		return this.m_vssPath;
	}

    /**
     * Set the local path.
     */
    public void setLocalpath(Path localPath) {
        m_localPath = localPath.toString();
    }

    /**
     * Builds and returns the -GL flag command if required
     * <p>
     * The localpath is created if it didn't exist
     */
    public void getLocalpathCommand(Commandline cmd) {
        if (m_localPath == null) {
            return;
        } else {
            // make sure m_LocalDir exists, create it if it doesn't
            File dir = project.resolveFile(m_localPath);
            if (!dir.exists()) {
                boolean done = dir.mkdirs();
                if (done == false) {
                    String msg = "Directory " + m_localPath + " creation was not " +
                        "succesful for an unknown reason";
                    throw new BuildException(msg, location);
                }
                project.log("Created dir: " + dir.getAbsolutePath());
            }

            cmd.createArgument().setValue(FLAG_OVERRIDE_WORKING_DIR + m_localPath);
        }
    }

    /**
     * Set behaviour recursive or non-recursive
     */
    public void setRecursive(boolean recursive) {
        m_Recursive = recursive;
    }

    /**
     * @return the 'recursive' command if the attribute was 'true', otherwise an empty string
     */
    public void getRecursiveCommand(Commandline cmd) {
        if ( !m_Recursive ) {
            return;
        } else {
            cmd.createArgument().setValue(FLAG_RECURSION);
        }
    }

    /**
     * Set behaviour, used in get command to make files that are 'got' writable
     */
    public final void setWritable(boolean argWritable) {
        m_Writable = argWritable;
    }

    /**
     * @return the 'make writable' command if the attribute was 'true', otherwise an empty string
     */
    public void getWritableCommand(Commandline cmd) {
        if ( !m_Writable ) {
            return;
        } else {
            cmd.createArgument().setValue(FLAG_WRITABLE);
        }
    }

    /**
     * Set the stored version string
     * <p>
     * Note we assume that if the supplied string has the value "null" that something
     * went wrong and that the string value got populated from a null object. This
     * happens if a ant variable is used e.g. version="${ver_server}" when ver_server
     * has not been defined to ant!
     */
    public void setVersion(String version) {
        if (version.equals("") || version.equals("null") ) {
            m_Version = null;
        } else {
            m_Version = version;
        }
    }

    /**
     * Set the stored date string
     * <p>
     * Note we assume that if the supplied string has the value "null" that something
     * went wrong and that the string value got populated from a null object. This
     * happens if a ant variable is used e.g. date="${date}" when date
     * has not been defined to ant!
     */
    public void setDate(String date) {
        if (date.equals("") || date.equals("null") ) {
            m_Date = null;
        } else {
            m_Date = date;
        }
    }

    /**
     * Set the labeled version to operate on in SourceSafe
     * <p>
     * Note we assume that if the supplied string has the value "null" that something
     * went wrong and that the string value got populated from a null object. This
     * happens if a ant variable is used e.g. label="${label_server}" when label_server
     * has not been defined to ant!
     */
    public void setLabel(String label) {
        if ( label.equals("") || label.equals("null") ) {
            m_Label = null;
        } else {
            m_Label = label;
        }
    }

    /**
     * Simple order of priority. Returns the first specified of version, date, label
     * If none of these was specified returns ""
     */
    public void getVersionCommand(Commandline cmd) {

        if ( m_Version != null) {
            cmd.createArgument().setValue(FLAG_VERSION + m_Version);
        } else if ( m_Date != null) {
            cmd.createArgument().setValue(FLAG_VERSION_DATE + m_Date);
        } else if (m_Label != null) {
            cmd.createArgument().setValue(FLAG_VERSION_LABEL + m_Label);
        }
    }
}