package org.xcolab.view.pages.profile.beans;

import org.xcolab.client.contest.ContestClientUtil;
import org.xcolab.client.contest.pojo.phases.ContestPhase;
import org.xcolab.client.contest.pojo.phases.ContestPhaseRibbonType;
import org.xcolab.client.proposals.ProposalClientUtil;
import org.xcolab.client.proposals.ProposalPhaseClientUtil;
import org.xcolab.client.proposals.pojo.Proposal;
import org.xcolab.client.proposals.pojo.phases.ProposalContestPhaseAttribute;
import org.xcolab.util.enums.contest.ProposalContestPhaseAttributeKeys;
import org.xcolab.view.pages.profile.entity.Badge;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

public class BadgeBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private final List<Badge> badges = new ArrayList<>();

    public BadgeBean(long userId) {
        populateBadges(userId);
    }

    private void populateBadges(long userId) {
        for (Proposal proposal : ProposalClientUtil.getMemberProposals(userId)) {
            getRibbonAttribute(proposal)
                    .ifPresent(ribbonAttribute -> {
                        final ContestPhaseRibbonType ribbonType = ContestClientUtil
                                .getContestPhaseRibbonType(ribbonAttribute.getNumericValue());

                        if (ribbonType.getRibbon() > 0) {
                            final ContestPhase phase = ContestClientUtil.getContestPhase(
                                    ribbonAttribute.getContestPhaseId());
                            badges.add(new Badge(ribbonType, proposal, proposal.getName(),
                                    phase.getContest()));
                        }
                    });
        }
    }

    private Optional<ProposalContestPhaseAttribute> getRibbonAttribute(Proposal proposal) {
        List<Long> phasesForProposal = ProposalPhaseClientUtil.getContestPhasesForProposal(
                proposal.getProposalId());
        return phasesForProposal.stream()
                .map(getRibbonAttributeFunction(proposal.getProposalId()))
                .filter(Objects::nonNull)
                .max(getStartDateComparator());
    }

    private Comparator<ProposalContestPhaseAttribute> getStartDateComparator() {
        return Comparator.comparing(attribute -> ContestClientUtil
                .getContestPhase(attribute.getContestPhaseId()).getPhaseStartDateDt());
    }

    private Function<Long, ProposalContestPhaseAttribute> getRibbonAttributeFunction(
            Long proposalId) {
        return phaseId -> ProposalPhaseClientUtil
                .getProposalContestPhaseAttribute(proposalId, phaseId,
                        ProposalContestPhaseAttributeKeys.RIBBON);
    }

    public List<Badge> getBadges() {
        return badges;
    }

    @Override
    public String toString() {
        return badges.toString();
    }
}

@@ -901,22 +901,16 @@
 private 
-final 
 List%3CBad
@@ -923,28 +923,8 @@
 dges
- = new ArrayList%3C%3E()
 ;%0A%0A 
@@ -966,24 +966,28 @@
         
-populate
+badges = get
 Badges(u
@@ -1017,21 +1017,23 @@
 ate 
-void populate
+List%3CBadge%3E get
 Badg
@@ -1042,32 +1042,86 @@
 (long userId) %7B%0A
+        final List%3CBadge%3E badges = new ArrayList%3C%3E();%0A
         for (Pro
@@ -1182,24 +1182,111 @@
 (userId)) %7B%0A
+            final Optional%3CProposalContestPhaseAttribute%3E ribbonAttributeOpt =%0A        
             
@@ -1313,16 +1313,17 @@
 roposal)
+;
 %0A       
@@ -1331,56 +1331,142 @@
     
-        .ifPresent(ribbonAttribute -%3E %7B%0A        
+if (ribbonAttributeOpt.isPresent()) %7B%0A                final ProposalContestPhaseAttribute ribbonAttribute = ribbonAttributeOpt.get();%0A
     
@@ -1557,32 +1557,24 @@
             
-  
-      
 .getContestP
@@ -1637,32 +1637,24 @@
             
-      
-  
 if (ribbonTy
@@ -1671,32 +1671,24 @@
 on() %3E 0) %7B%0A
-        
             
@@ -1785,24 +1785,16 @@
         
-  
-      
 ribbonAt
@@ -1819,32 +1819,24 @@
 PhaseId());%0A
-        
             
@@ -1934,24 +1934,16 @@
         
-        
 phase.ge
@@ -1972,24 +1972,16 @@
         
-   
-     
 %7D%0A      
@@ -1978,32 +1978,34 @@
   %7D%0A            
+%7D%0A
         %7D);%0A    
@@ -1997,18 +1997,16 @@
        %7D
-);
 %0A       
@@ -2002,25 +2002,38 @@
   %7D%0A        
-%7D
+return badges;
 %0A    %7D%0A%0A    

Improve readbility of getBadges