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

import org.xcolab.client.proposals.pojo.Proposal;
import org.xcolab.client.proposals.pojo.proposals.ProposalRibbon;

import java.util.Comparator;

public enum ProposalSortColumn {
    NAME(Comparator.comparing(o -> o.getName().toLowerCase())),
    AUTHOR(Comparator.comparing(o -> o.getAuthorName().toLowerCase())),
    SUPPORTERS((o1, o2) -> (int) (o1.getSupportersCount() - o2.getSupportersCount())),
    VOTES((o1, o2) -> {
        try {
            return (int) (o1.getVotesCountFromCache() - o2.getVotesCountFromCache());
        }
        catch (Exception e) {
            return (int) (o1.getProposalId() - o2.getProposalId());
        }
    }, SUPPORTERS),
    COMMENTS((o1, o2) -> (int) (o1.getCommentsCount() - o2.getCommentsCount())),
    JUDGESTATUS(
            (o1, o2) -> (o1.getJudgeStatus().getStatusValue() - o2.getJudgeStatus().getStatusValue())),
    OVERALLSTATUS(
            (o1, o2) -> (o1.getOverallStatus().getStatusValue() - o2.getOverallStatus().getStatusValue())),
    SCREENINGSTATUS(
            (o1, o2) -> (o1.getScreeningStatus().getStatusValue() - o2.getScreeningStatus().getStatusValue())),
    IAFSTATUS(
            (o1, o2) -> (o1.getImpactAssessmentStatus().getStatusValue() - o2.getImpactAssessmentStatus().getStatusValue())),
    MODIFIED(Comparator.comparing(Proposal::getLastModifiedDate)),
    CONTRIBUTORS((o1, o2) -> {
        if (o1.isOpen()) {
            return o2.isOpen() ? 0 : -1;
        } else {
            return o2.isOpen() ? 1 : 0;
        }
    }),
    RIBBONS((o1, o2) -> {
        final ProposalRibbon ribbon1 = o1.getRibbonWrapper();
        final ProposalRibbon ribbon2 = o2.getRibbonWrapper();

        int sortOrderDiff = ribbon1.getSortOrder() - ribbon2.getSortOrder();
        if (sortOrderDiff != 0) {
            return sortOrderDiff;
        }

        return ribbon1.getRibbon() - ribbon2.getRibbon();
    });
    
    private final Comparator<Proposal> proposalsComparator;

    ProposalSortColumn(Comparator<Proposal> comparator) {
        proposalsComparator = comparator;
    }

    ProposalSortColumn(Comparator<Proposal> comparator, ProposalSortColumn secondarySortColumn) {
        proposalsComparator = comparator.thenComparing(secondarySortColumn.getComparator());
    }

    public Comparator<Proposal> getComparator() {
        return proposalsComparator;
    }

}

@@ -469,43 +469,8 @@
 ) -%3E
- %7B%0A        try %7B%0A            return
  (in
@@ -535,145 +535,8 @@
 e())
-;%0A        %7D%0A        catch (Exception e) %7B%0A            return (int) (o1.getProposalId() - o2.getProposalId());%0A        %7D%0A    %7D, SUPPORTERS
 ),%0A 

Remove unnecessary try-catch from voting sort column