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

import com.atlassian.jira.issue.IssueFieldConstants;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.RendererManager;
import com.atlassian.jira.issue.fields.layout.field.FieldLayout;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutManager;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.velocity.htmlsafe.HtmlSafe;
import net.congstar.jira.plugins.scrumpoker.data.ScrumPokerStorage;
import net.congstar.jira.plugins.scrumpoker.model.ScrumPokerCard;
import net.congstar.jira.plugins.scrumpoker.model.ScrumPokerCards;
import net.congstar.jira.plugins.scrumpoker.model.ScrumPokerSession;

import java.util.Map;

/**
 * Start a new Scrum poker session or refresh a currently displayed Scrum poker session.
 */
public class StartScrumPokerAction extends ScrumPokerAction {

    private static final long serialVersionUID = 1L;
    private static final String PARAM_ACTION = "action";

    private final IssueManager issueManager;
    private final ScrumPokerStorage scrumPokerStorage;
    private final UserManager userManager;
    private final RendererManager rendererManager;
    private final FieldLayoutManager fieldLayoutManager;

    private String issueKey;
    private final Map<String, ScrumPokerCard> cardDeck = ScrumPokerCards.asMap();
    private ScrumPokerSession pokerSession;

    public StartScrumPokerAction(IssueManager issueManager, UserManager userManager, RendererManager rendererManager,
                                 ScrumPokerStorage scrumPokerStorage, FieldLayoutManager fieldLayoutManager) {
        this.issueManager = issueManager;
        this.rendererManager = rendererManager;
        this.scrumPokerStorage = scrumPokerStorage;
        this.userManager = userManager;
        this.fieldLayoutManager = fieldLayoutManager;
    }

    @Override
    protected String doExecute() throws Exception {
        String action = getHttpRequest().getParameter(PARAM_ACTION);
        issueKey = getHttpRequest().getParameter(PARAM_ISSUE_KEY);

        MutableIssue issue = issueManager.getIssueObject(issueKey);
        if (issue == null) {
            addErrorMessage("Issue Key" + issueKey + " not found.");
            return "error";
        }

        pokerSession = scrumPokerStorage.sessionForIssue(issueKey, currentUserKey());

        if (action != null && action.equals("update")) {
            return "update";
        } else {
            return "start";
        }
    }

    public MutableIssue getIssue() {
        return issueManager.getIssueObject(issueKey);
    }

    @HtmlSafe
    public String getIssueDescription() {
        MutableIssue issue = issueManager.getIssueObject(issueKey);
        FieldLayout fieldLayout = fieldLayoutManager.getFieldLayout(issue);
        FieldLayoutItem fieldLayoutItem = fieldLayout.getFieldLayoutItem(IssueFieldConstants.DESCRIPTION);
        String rendererType = (fieldLayoutItem != null) ? fieldLayoutItem.getRendererType() : null;
        return rendererManager.getRenderedContent(rendererType, issue.getDescription(), issue.getIssueRenderContext());
    }

    public Map<String, ScrumPokerCard> getCardDeck() {
        return cardDeck;
    }

    public String getChosenCard() {
        return pokerSession.getCards().get(getLoggedInUser().getKey());
    }

    public ScrumPokerCard[] getCards() {
        return ScrumPokerCards.pokerDeck;
    }

    public ScrumPokerSession getPokerSession() {
        return pokerSession;
    }

    public String getUsername(String key) {
        return userManager.getUserByKey(key).getDisplayName();
    }

}

@@ -820,31 +820,8 @@
 n;%0A%0A
-import java.util.Map;%0A%0A
 /**%0A
@@ -1372,90 +1372,8 @@
 ey;%0A
-    private final Map%3CString, ScrumPokerCard%3E cardDeck = ScrumPokerCards.asMap();%0A
     
@@ -2087,17 +2087,16 @@
 E_KEY);%0A
-%0A
         
@@ -2287,25 +2287,24 @@
 ;%0A        %7D%0A
-%0A
         poke
@@ -2377,25 +2377,24 @@
 Key());%0A
-%0A
         
 if (acti
@@ -2389,140 +2389,60 @@
     
-if (action != null && action.equals(%22update%22)) %7B%0A            return %22update%22;%0A        %7D else %7B%0A            return %22start%22;%0A        %7D
+return %22update%22.equals(action) ? %22update%22 : %22start%22;
 %0A   
@@ -2950,16 +2950,16 @@
 : null;%0A
+
         
@@ -3081,95 +3081,8 @@
  %7D%0A%0A
-    public Map%3CString, ScrumPokerCard%3E getCardDeck() %7B%0A        return cardDeck;%0A    %7D%0A%0A
     

simplify refreshing session data and remove unused code