package net.congstar.jira.plugins.scrumpoker.data;

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import net.congstar.jira.plugins.scrumpoker.action.ConfigurePlanningPokerAction;

import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.util.ErrorCollection;
import java.util.Map;
import java.text.NumberFormat;

public class DefaultStoryPointSupport implements StoryPointFieldSupport {

    private final IssueManager issueManager;

    private final PluginSettingsFactory pluginSettingsFactory;

    private final CustomFieldManager customFieldManager;

    private final IssueService issueService;

    private final JiraAuthenticationContext context;

    public DefaultStoryPointSupport(JiraAuthenticationContext context, IssueService issueService, IssueManager issueManager, PluginSettingsFactory pluginSettingsFactory, CustomFieldManager customFieldManager) {
        this.issueManager = issueManager;
        this.pluginSettingsFactory = pluginSettingsFactory;
        this.customFieldManager = customFieldManager;
        this.context = context;

        this.issueService = issueService;
    }

    @Override
    public void save(String issueKey, Double newValue) {
        ApplicationUser appuser = context.getUser();
        IssueService.IssueResult issueResult = issueService.getIssue(appuser, issueKey);
        MutableIssue issue = issueResult.getIssue();

        IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
        CustomField customField = findStoryPointField();
        issueInputParameters.addCustomFieldValue(customField.getId(), NumberFormat.getInstance().format(newValue));
        IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(appuser, issue.getId(), issueInputParameters);

        if (updateValidationResult.isValid()) {
            IssueService.IssueResult updateResult = issueService.update(appuser, updateValidationResult);
            if (!updateResult.isValid()) {
                System.out.println("**** Issue update FAILED!");
                ErrorCollection errors = updateResult.getErrorCollection();
                if (errors.hasAnyErrors()) {
                    Map<String, String> messages = errors.getErrors();
                    for (Map.Entry<String, String> message : messages.entrySet()) {
                        System.out.println(message.getKey() + " : " + message.getValue());
                    }
                }
            }
        } else {
            System.out.println("**** Update validation FAILED!");
            ErrorCollection errors = updateValidationResult.getErrorCollection();
            if (errors.hasAnyErrors()) {
                Map<String, String> messages = errors.getErrors();
                for (Map.Entry<String, String> message : messages.entrySet()) {
                    System.out.println(message.getKey() + " : " + message.getValue());
                }
            }
        }
    }

    @Override
    public Double getValue(String issueKey) {
        ApplicationUser appuser = context.getUser();
        IssueService.IssueResult issueResult = issueService.getIssue(appuser, issueKey);
        if (!issueResult.isValid()) {
            System.out.println("**** Problem finding issue");
            return null;
        }

        MutableIssue issue = issueResult.getIssue();

        return (Double) issue.getCustomFieldValue(findStoryPointField());
    }

    private CustomField findStoryPointField() {
        PluginSettings settings = pluginSettingsFactory.createGlobalSettings();
        String field = (String) settings.get(ConfigurePlanningPokerAction.STORY_POINT_FIELD_NAME);
        String storyPointFieldName = field != null ? field : ConfigurePlanningPokerAction.DEFAULT_FIELD_FOR_STORY_POINTS;

        for (CustomField customField : customFieldManager.getCustomFieldObjects()) {
            if (customField.getNameKey().equalsIgnoreCase(storyPointFieldName)) {
                return customField;
            }
        }

        return null;
    }

}

@@ -56,98 +56,136 @@
 ort 
-com.atlassian.jira.issue.CustomFieldManager
+java.text.NumberFormat;%0Aimport java.util.Map
 ;%0A
+%0A
 import 
-com.atlassian.jira.issue.IssueManager;
+net.congstar.jira.plugins.scrumpoker.action.ConfigurePlanningPokerAction;%0A
 %0Aimp
@@ -211,25 +211,28 @@
 ira.
+bc.
 issue.
-MutableIssu
+IssueServic
 e;%0Ai
@@ -266,15 +266,8 @@
 sue.
-fields.
 Cust
@@ -273,16 +273,23 @@
 tomField
+Manager
 ;%0Aimport
@@ -307,195 +307,86 @@
 ian.
-sal.api.pluginsettings.PluginSettings;%0Aimport com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;%0Aimport net.congstar.jira.plugins.scrumpoker.action.ConfigurePlanningPokerAction;%0A
+jira.issue.IssueInputParameters;%0Aimport com.atlassian.jira.issue.IssueManager;
 %0Aimp
@@ -408,36 +408,33 @@
 an.jira.
-bc.
 issue.
-IssueServic
+MutableIssu
 e;%0Aimpor
@@ -458,74 +458,32 @@
 ira.
-user.ApplicationUser;%0Aimport com.atlassian.crowd.embedded.api.User
+issue.fields.CustomField
 ;%0Aim
@@ -572,34 +572,28 @@
 ira.
-issue.IssueInputParamet
+user.ApplicationUs
 er
-s
 ;%0Aim
@@ -649,52 +649,126 @@
 ort 
-java.util.Map;%0Aimport java.text.NumberFormat
+com.atlassian.sal.api.pluginsettings.PluginSettings;%0Aimport com.atlassian.sal.api.pluginsettings.PluginSettingsFactory
 ;%0A%0Ap
@@ -845,54 +845,8 @@
  %7B%0A%0A
-    private final IssueManager issueManager;%0A%0A
     
@@ -967,54 +967,8 @@
 r;%0A%0A
-    private final IssueService issueService;%0A%0A
     
@@ -1011,24 +1011,74 @@
 ext context;
+%0A    %0A    private final IssueService issueService;
 %0A%0A    public
@@ -1282,50 +1282,8 @@
 ) %7B%0A
-        this.issueManager = issueManager;%0A
     
@@ -1424,17 +1424,16 @@
 ontext;%0A
-%0A
         

remove unused issue manager