package net.congstar.jira.plugins.scrumpoker;

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.plugin.webfragment.conditions.AbstractWebCondition;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.user.ApplicationUser;
import net.congstar.jira.plugins.scrumpoker.data.StoryPointFieldSupport;

/**
 * Only display the Scrum poker web element for issues that have the custom field to hold the story points.
 */
public class StoryTypeCondition extends AbstractWebCondition {

    private final CustomFieldManager customFieldManager;

    private final StoryPointFieldSupport storyPointFieldSupport;

    public StoryTypeCondition(CustomFieldManager customFieldManager, StoryPointFieldSupport storyPointFieldSupport) {
        this.customFieldManager = customFieldManager;
        this.storyPointFieldSupport = storyPointFieldSupport;
    }

    @Override
    public boolean shouldDisplay(ApplicationUser applicationUser, JiraHelper jiraHelper) {
        Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue");
        return currentIssue.isEditable() && hasStoryPointField(currentIssue);
    }

    private boolean hasStoryPointField(Issue currentIssue) {
        CustomField storyPointField = storyPointFieldSupport.findStoryPointField();
        return customFieldManager.getCustomFieldObjects(currentIssue).contains(storyPointField);
    }

}

@@ -510,16 +510,33 @@
 es that 
+are editable and 
 have the
@@ -570,15 +570,29 @@
 tory
+%0A *
  points
+ configured
 .%0A *

improve javadoc to reflect recent changes