package net.minecraft.gametest.framework;

import java.util.function.Consumer;
import com.google.common.collect.Lists;
import javax.annotation.Nullable;
import java.util.Collection;

public class MultipleTestTracker {
    private final Collection<GameTestInfo> tests;
    @Nullable
    private Collection<GameTestListener> listeners;
    
    public MultipleTestTracker() {
        this.tests = Lists.newArrayList();
        this.listeners = Lists.newArrayList();
    }
    
    public MultipleTestTracker(final Collection<GameTestInfo> collection) {
        this.tests = Lists.newArrayList();
        this.listeners = Lists.newArrayList();
        this.tests.addAll(collection);
    }
    
    public void addTestToTrack(final GameTestInfo kg) {
        this.tests.add(kg);
        this.listeners.forEach(kg::addListener);
    }
    
    public void addListener(final GameTestListener kh) {
        this.listeners.add(kh);
        this.tests.forEach(kg -> kg.addListener(kh));
    }
    
    public void addFailureListener(final Consumer<GameTestInfo> consumer) {
        this.addListener(new GameTestListener() {
            @Override
            public void testStructureLoaded(final GameTestInfo kg) {
            }
            
            @Override
            public void testFailed(final GameTestInfo kg) {
                consumer.accept(kg);
            }
        });
    }
    
    public int getFailedRequiredCount() {
        return (int)this.tests.stream().filter(GameTestInfo::hasFailed).filter(GameTestInfo::isRequired).count();
    }
    
    public int getFailedOptionalCount() {
        return (int)this.tests.stream().filter(GameTestInfo::hasFailed).filter(GameTestInfo::isOptional).count();
    }
    
    public int getDoneCount() {
        return (int)this.tests.stream().filter(GameTestInfo::isDone).count();
    }
    
    public boolean hasFailedRequired() {
        return this.getFailedRequiredCount() > 0;
    }
    
    public boolean hasFailedOptional() {
        return this.getFailedOptionalCount() > 0;
    }
    
    public int getTotalCount() {
        return this.tests.size();
    }
    
    public boolean isDone() {
        return this.getDoneCount() == this.getTotalCount();
    }
    
    public String getProgressBar() {
        final StringBuffer stringBuffer2 = new StringBuffer();
        stringBuffer2.append('[');
        final StringBuffer sb;
        this.tests.forEach(kg -> {
            if (!kg.hasStarted()) {
                sb.append(' ');
            }
            else if (kg.hasSucceeded()) {
                sb.append('+');
            }
            else if (kg.hasFailed()) {
                sb.append(kg.isRequired() ? 'X' : 'x');
            }
            else {
                sb.append('_');
            }
            return;
        });
        stringBuffer2.append(']');
        return stringBuffer2.toString();
    }
    
    @Override
    public String toString() {
        return this.getProgressBar();
    }
}
