// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.command.system;

import java.util.Iterator;
import com.hypixel.hytale.server.core.command.system.exceptions.GeneralCommandException;
import java.util.Collection;
import java.util.Collections;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.Message;
import java.util.List;

public class ParseResult
{
    private boolean failed;
    @Nullable
    private List<Message> reasons;
    private final boolean throwExceptionWhenFailed;
    
    public ParseResult() {
        this(false);
    }
    
    public ParseResult(final boolean throwExceptionWhenFailed) {
        this.throwExceptionWhenFailed = throwExceptionWhenFailed;
    }
    
    public void fail(@Nonnull final Message reason, @Nullable final Message... otherMessages) {
        this.failed = true;
        if (this.reasons == null) {
            this.reasons = new ObjectArrayList<Message>();
        }
        this.reasons.add(reason);
        if (otherMessages != null) {
            Collections.addAll(this.reasons, otherMessages);
        }
        if (this.throwExceptionWhenFailed) {
            final StringBuilder builder = new StringBuilder(reason.getAnsiMessage());
            if (otherMessages != null) {
                for (final Message otherMessage : otherMessages) {
                    builder.append("\n").append(otherMessage.getAnsiMessage());
                }
            }
            throw new GeneralCommandException(Message.raw(builder.toString()));
        }
    }
    
    public void fail(@Nonnull final Message reason) {
        this.fail(reason, (Message[])null);
    }
    
    public boolean failed() {
        return this.failed;
    }
    
    public void sendMessages(@Nonnull final CommandSender sender) {
        if (this.reasons == null) {
            return;
        }
        for (final Message reason : this.reasons) {
            sender.sendMessage(reason);
        }
    }
}
