package ee.elinyo.teamcity.plugins.ansible.agent;

import java.util.ArrayList;
import java.util.List;

import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.artifacts.ArtifactsWatcher;
import jetbrains.buildServer.agent.runner.BuildServiceAdapter;
import jetbrains.buildServer.agent.runner.ProcessListener;
import jetbrains.buildServer.agent.runner.ProgramCommandLine;
import jetbrains.buildServer.agent.runner.SimpleProgramCommandLine;
import jetbrains.buildServer.runner.CommandLineArgumentsUtil;

import org.jetbrains.annotations.NotNull;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;

import ee.elinyo.teamcity.plugins.ansible.common.AnsibleCommand;
import ee.elinyo.teamcity.plugins.ansible.common.AnsibleRunConfig;

public class AnsibleRunService extends BuildServiceAdapter {
    
    protected static final Logger LOG = Logger.getInstance(AnsibleRunService.class.getName());
    
    private ArtifactsWatcher artifactsWatcher;

    public AnsibleRunService(ArtifactsWatcher artifactsWatcher) {
        this.artifactsWatcher = artifactsWatcher;
    }

    @Override
    public ProgramCommandLine makeProgramCommandLine() throws RunBuildException {
        AnsibleRunConfig config = new AnsibleRunConfig(getRunnerParameters());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Going to run ansible with parameters: " + config.toString());
        }
        if (AnsibleCommand.CUSTOM_SCRIPT.equals(config.getCommandType())) {
            return makeCustomScriptCommand(config);
        }
        return makeExecutableCommand(config);
    }
    
    private ProgramCommandLine makeExecutableCommand(AnsibleRunConfig config)
            throws RunBuildException {
        String workingDir = getWorkingDirectory().getPath();
        StringBuilder args = new StringBuilder("");
        if (!StringUtil.isEmptyOrSpaces(config.getInventory())) {
            args.append("-i ").append(config.getInventory());
        }
        if (StringUtil.isEmptyOrSpaces(config.getPlaybook())) {
            throw new RunBuildException("Ansible playbook should be specified");
        }
        args.append(" ").append(config.getPlaybook());
        if (!StringUtil.isEmptyOrSpaces(config.getOptions())) {
            args.append(" ").append(config.getOptions());
        }
        return new SimpleProgramCommandLine(getEnvironmentVariables(),
                workingDir,
               config.getExecutable(), CommandLineArgumentsUtil.extractArguments(args.toString()));
    }

    private ProgramCommandLine makeCustomScriptCommand(AnsibleRunConfig config) throws RunBuildException {
       // String workingDir = getWorkingDirectory().getPath();
        LOG.warn("Not Implemented");
        throw new RunBuildException("Ansible customs script is not imlemented");
    }

    @NotNull
    @Override
    public List<ProcessListener> getListeners() {
        List<ProcessListener> listeners = new ArrayList<ProcessListener>(super.getListeners());
        listeners.add(new AnsibleOutputListener(getBuild(), getRunnerContext(), artifactsWatcher));
        return listeners;
    }

}

@@ -89,20 +89,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -2439,16 +2439,24 @@
 Line(get
+Provided
 Environm
@@ -2456,17 +2456,16 @@
 nvironme
-n
 tVariabl
@@ -2599,24 +2599,256 @@
 g()));%0A    %7D
+%0A    %0A    private Map%3CString, String%3E getProvidedEnvironmetVariables() %7B%0A        Map%3CString, String%3E vars = new HashMap%3CString, String%3E(getEnvironmentVariables());%0A        vars.put(%22ANSIBLE_NOCOLOR%22, %221%22);%0A        return vars;%0A    %7D
 %0A%0A    privat

fix TTY colors