package org.xcolab.view.pages.proposals.view.action;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.xcolab.client.members.pojo.Member;
import org.xcolab.client.proposals.ProposalMemberRatingClient;
import org.xcolab.util.http.ServiceRequestUtils;
import org.xcolab.util.http.caching.CacheName;
import org.xcolab.view.errors.AccessDeniedPage;
import org.xcolab.view.pages.proposals.exceptions.ProposalsAuthorizationException;
import org.xcolab.view.pages.proposals.utils.context.ProposalContext;
import org.xcolab.view.util.entity.analytics.AnalyticsUtil;
import org.xcolab.view.util.googleanalytics.GoogleAnalyticsEventType;
import org.xcolab.view.util.googleanalytics.GoogleAnalyticsUtils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/contests/{contestYear}/{contestUrlName}/c/{proposalUrlString}/{proposalId}")
public class SupportProposalActionController {

    private static final String SUPPORT_ANALYTICS_KEY = "SUPPORTED_ENTRIES";
    private static final String SUPPORT_ANALYTICS_CATEGORY = "User";
    private static final String SUPPORT_ANALYTICS_ACTION = "Support contest entry";
    private static final String SUPPORT_ANALYTICS_LABEL = "";

    @PostMapping("supportProposalAction")
    public String handleAction(HttpServletRequest request, HttpServletResponse response,
            Model model, Member currentMember, ProposalContext proposalContext,
            @RequestParam(required = false) String forwardToTab)
            throws ProposalsAuthorizationException, IOException {

        if (!proposalContext.getPermissions().getCanSupportProposal()) {
            return new AccessDeniedPage(currentMember).toViewName(response);
        }
        long memberId = currentMember.getUserId();
        long proposalId = proposalContext.getProposal().getProposalId();
        ProposalMemberRatingClient proposalMemberRatingClient =
                proposalContext.getClients().getProposalMemberRatingClient();
        if (proposalMemberRatingClient.isMemberProposalSupporter(proposalId, memberId)) {
            proposalMemberRatingClient.deleteProposalSupporter(proposalId, memberId);
        } else {
            proposalMemberRatingClient.addProposalSupporter(proposalId, memberId);
            int supportedCount = proposalMemberRatingClient.getProposalSupportersCount(memberId);
            if (supportedCount > 0) {
                int analyticsValue = AnalyticsUtil.getAnalyticsValueForCount(supportedCount);
                AnalyticsUtil
                        .publishEvent(request, memberId, SUPPORT_ANALYTICS_KEY + analyticsValue,
                                SUPPORT_ANALYTICS_CATEGORY, SUPPORT_ANALYTICS_ACTION,
                                SUPPORT_ANALYTICS_LABEL, analyticsValue);
				GoogleAnalyticsUtils.pushEventAsync(GoogleAnalyticsEventType.CONTEST_ENTRY_SUPPORT);
            }
        }
        ServiceRequestUtils.clearCache(CacheName.MISC_REQUEST);
        String proposalLinkUrl =
                proposalContext.getProposal().getProposalLinkUrl(proposalContext.getContest());
        if (!StringUtils.isBlank(forwardToTab)) {
            proposalLinkUrl += "/tab/" + forwardToTab;
        }
        return "redirect:" + proposalLinkUrl;
    }

}

@@ -179,16 +179,75 @@
 .Model;%0A
+import org.springframework.web.bind.annotation.GetMapping;%0A
 import o
@@ -423,16 +423,63 @@
 Param;%0A%0A
+import org.xcolab.client.contest.pojo.Contest;%0A
 import o
@@ -579,16 +579,120 @@
 Client;%0A
+import org.xcolab.client.proposals.pojo.Proposal;%0Aimport org.xcolab.commons.servlet.flash.AlertMessage;%0A
 import o
@@ -3784,10 +3784,604 @@
 %0A    %7D%0A%0A
+    @GetMapping(%22supportProposalAction%22)%0A    public String handleInvalidGetRequest(HttpServletRequest request,%0A            HttpServletResponse response, Model model, ProposalContext proposalContext,%0A            Member member) %7B%0A%0A        AlertMessage.warning(%0A                %22Your support hasn't been recorded, please make sure to click the button only once.%22)%0A                .flash(request);%0A        final Contest contest = proposalContext.getContest();%0A        final Proposal proposal = proposalContext.getProposal();%0A        return %22redirect:%22 + proposal.getProposalLinkUrl(contest);%0A    %7D%0A
 %7D%0A

Add get equivalent for support