package de.schramke.bamboo.plugins.gradlew.tasks;

import com.atlassian.bamboo.build.Job;
import com.atlassian.bamboo.task.TaskConfigConstants;
import com.atlassian.bamboo.util.TextProviderUtils;
import com.atlassian.bamboo.v2.build.agent.capability.Requirement;
import com.atlassian.bamboo.v2.build.agent.capability.RequirementImpl;
import com.google.common.collect.Sets;
import com.opensymphony.xwork2.TextProvider;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;

import com.atlassian.bamboo.collections.ActionParametersMap;
import com.atlassian.bamboo.task.AbstractTaskConfigurator;
import com.atlassian.bamboo.task.TaskDefinition;
import com.atlassian.bamboo.utils.error.ErrorCollection;
import com.atlassian.util.concurrent.Nullable;

import java.util.Map;
import java.util.Set;

public class GradleTaskConfigurator extends AbstractTaskConfigurator {

    private static final Logger log = Logger.getLogger(GradleTaskConfigurator.class);

    // ------------------------------------------------------------------------------------------------------- Constants

    private static final Set<String> FIELDS_TO_COPY = Sets.newHashSet(
            GradleBuildTask.USE_WRAPPER,
            GradleBuildTask.LABEL,
            GradleBuildTask.TASK,
            GradleBuildTask.ENVIRONMENT,
            GradleBuildTask.LOG_LEVEL,
            GradleBuildTask.STACK_TRACE_OUTPUT,
            TaskConfigConstants.CFG_JDK_LABEL,
            TaskConfigConstants.CFG_WORKING_SUB_DIRECTORY
    );

    // ------------------------------------------------------------------------------------------------- Type Properties
    // ---------------------------------------------------------------------------------------------------- Dependencies
    // ---------------------------------------------------------------------------------------------------- Constructors
    // ----------------------------------------------------------------------------------------------- Interface Methods

    @Override
    @NotNull
    public Set<Requirement> calculateRequirements(@NotNull TaskDefinition taskDefinition, @NotNull Job buildable) {
        final String label = taskDefinition.getConfiguration().get(GradleBuildTask.LABEL);
        return Sets.<Requirement>newHashSet(new RequirementImpl(GradleBuildTask.GRADLE_CAPABILITY_PREFIX + "." + label, true, ".*"));
    }

    @Override
    public void validate(@NotNull final ActionParametersMap params, @NotNull final ErrorCollection errorCollection) {
        super.validate(params, errorCollection);

        TextProvider textprovider = TextProviderUtils.getTextProvider();

        final String gradleTaskValue = params.getString("gradleTask");
        if (StringUtils.isEmpty(gradleTaskValue)) {
            errorCollection.addError("gradleTask", textprovider.getText("gradle.task.error"));
        }
    }

    @Override
    public Map<String, String> generateTaskConfigMap(@NotNull final ActionParametersMap params, @Nullable final TaskDefinition previousTaskDefinition) {

        final Map<String, String> map = super.generateTaskConfigMap(params, previousTaskDefinition);
        taskConfiguratorHelper.populateTaskConfigMapWithActionParameters(map, params, FIELDS_TO_COPY);
        return map;

    }

    @Override
    public void populateContextForCreate(@NotNull final Map<String, Object> context) {
        super.populateContextForCreate(context);
        context.put("useWrapper", true);
        context.put("gradleTask", "build");
    }

    @Override
    public void populateContextForEdit(@NotNull final Map<String, Object> context, @NotNull final TaskDefinition taskDefinition) {
        super.populateContextForEdit(context, taskDefinition);
        taskConfiguratorHelper.populateContextWithConfiguration(context, taskDefinition, FIELDS_TO_COPY);
    }

    @Override
    public void populateContextForView(@NotNull final Map<String, Object> context, @NotNull final TaskDefinition taskDefinition) {
        super.populateContextForView(context, taskDefinition);
        taskConfiguratorHelper.populateContextWithConfiguration(context, taskDefinition, FIELDS_TO_COPY);
    }

    // -------------------------------------------------------------------------------------------------- Action Methods
    // -------------------------------------------------------------------------------------------------- Public Methods
    // -------------------------------------------------------------------------------------- Basic Accessors / Mutators
}

@@ -115,84 +115,307 @@
 boo.
-task.TaskConfigConstants;%0Aimport com.atlassian.bamboo.util.TextProviderUtils
+collections.ActionParametersMap;%0Aimport com.atlassian.bamboo.task.AbstractTaskConfigurator;%0Aimport com.atlassian.bamboo.task.TaskConfigConstants;%0Aimport com.atlassian.bamboo.task.TaskDefinition;%0Aimport com.atlassian.bamboo.utils.error.ErrorCollection;%0Aimport com.atlassian.bamboo.utils.i18n.I18nBean
 ;%0Aim
@@ -565,79 +565,81 @@
 com.
-google.common.collect.Sets;%0Aimport com.opensymphony.xwork2.TextProvider
+atlassian.util.concurrent.Nullable;%0Aimport com.google.common.collect.Sets
 ;%0Aim
@@ -759,282 +759,8 @@
 l;%0A%0A
-import com.atlassian.bamboo.collections.ActionParametersMap;%0Aimport com.atlassian.bamboo.task.AbstractTaskConfigurator;%0Aimport com.atlassian.bamboo.task.TaskDefinition;%0Aimport com.atlassian.bamboo.utils.error.ErrorCollection;%0Aimport com.atlassian.util.concurrent.Nullable;%0A%0A
 impo
@@ -2541,24 +2541,26 @@
 llection);%0A%0A
+//
         Text
@@ -2619,16 +2619,64 @@
 vider();
+%0A        I18nBean i18nBean = this.getI18nBean();
 %0A%0A      
@@ -2788,24 +2788,26 @@
 skValue)) %7B%0A
+//
             
@@ -2885,24 +2885,115 @@
 k.error%22));%0A
+            errorCollection.addError(%22gradleTask%22, i18nBean.getText(%22gradle.task.error%22));%0A
         %7D%0A  

use I18nBean instead of TeaxtProvider