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 MSVSSLABEL extends MSVSS {

    private String m_LocalPath = null;
    private boolean m_Recursive = false;    
    private String m_Version = null;
    private String m_Date = null;
    private String m_Label = null;
	private String m_fileToLabel = null;
	private String m_vssFolder = 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 {
        //ss Label VSS items [-C] [-H] [-I-] [-Llabel] [-N] [-O] [-V] [-Y] [-?]
		//\\Ocsfile\poetic\document\ss Label $/product_development/version_1/technical_design/backend/com/ocs/buildTest.txt -Ltext -V22 -C- -I-Y
		Commandline commandLine = new Commandline();
		int result = 0;	
		commandLine.setExecutable(getSSCommand());		
	    commandLine.createArgument().setValue(COMMAND_LABEL);
		commandLine.createArgument().setValue(getVsspath()+"/"+m_fileToLabel);
		commandLine.createArgument().setValue("-L"+getLabel());
		getVersionCommand(commandLine);
		commandLine.createArgument().setValue("-C-");  // ignore comments
	    commandLine.createArgument().setValue("-I-Y");  // Yes to all Yes or No questions
	    getLoginCommand(commandLine);
	    result = run(commandLine);
	    if ( result != 0 ) {
	        String msg = "Failed executing: " + commandLine.toString();
	        throw new BuildException(msg, location);
	    }
    }
	
	public void setFileToLabel(String fileToLabel) {
		m_fileToLabel = fileToLabel;
	}
	

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


    /**
     * 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 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;
        }
    }
	
	public String getLabel() {
        return m_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);
        }
    }
}