package com.agileboot.domain.collaboration.record.enumtype;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;

/**
 * @author codex
 */
@Getter
public enum SettlementStatusEnum {

    NONE("NONE", "无结款项"),

    SETTLED("SETTLED", "已结"),

    UNSETTLED("UNSETTLED", "未结"),

    PARTIAL("PARTIAL", "未结清");

    private final String value;
    private final String label;

    SettlementStatusEnum(String value, String label) {
        this.value = value;
        this.label = label;
    }

    public static String aggregate(Collection<String> statuses) {
        if (statuses == null || statuses.isEmpty()) {
            return NONE.value;
        }
        List<String> effectiveStatuses = statuses.stream()
            .filter(SettlementStatusEnum::isEffectiveStatus)
            .collect(Collectors.toList());
        if (effectiveStatuses.isEmpty()) {
            return NONE.value;
        }
        if (isSameStatus(effectiveStatuses, SETTLED)) {
            return SETTLED.value;
        }
        if (isSameStatus(effectiveStatuses, UNSETTLED)) {
            return UNSETTLED.value;
        }
        return PARTIAL.value;
    }

    private static boolean isEffectiveStatus(String status) {
        return status != null && !NONE.value.equals(status);
    }

    private static boolean isSameStatus(List<String> statuses, SettlementStatusEnum expected) {
        return statuses.stream().allMatch(status -> expected.value.equals(status));
    }

}
