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

package com.hypixel.hytale.math.range;

import com.hypixel.hytale.codec.schema.config.IntegerSchema;
import com.hypixel.hytale.codec.schema.config.ArraySchema;
import com.hypixel.hytale.codec.schema.config.Schema;
import com.hypixel.hytale.codec.schema.SchemaContext;
import com.hypixel.hytale.codec.validation.ValidationResults;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import com.hypixel.hytale.codec.validation.Validator;

public class IntRangeBoundValidator implements Validator<IntRange>
{
    private final Integer min;
    private final Integer max;
    private final boolean inclusive;
    private final boolean lowerBound;
    
    @Nonnull
    public static IntRangeBoundValidator lowerBound(final Integer min, final Integer max, final boolean inclusive) {
        return new IntRangeBoundValidator(min, max, inclusive, true);
    }
    
    @Nonnull
    public static IntRangeBoundValidator upperBound(final Integer min, final Integer max, final boolean inclusive) {
        return new IntRangeBoundValidator(min, max, inclusive, false);
    }
    
    private IntRangeBoundValidator(final Integer min, final Integer max, final boolean inclusive, final boolean lowerBound) {
        this.min = min;
        this.max = max;
        this.inclusive = inclusive;
        this.lowerBound = lowerBound;
    }
    
    @Override
    public void accept(@Nullable final IntRange intRange, @Nonnull final ValidationResults results) {
        if (intRange == null) {
            return;
        }
        if (this.lowerBound) {
            this.validateBound(intRange.getInclusiveMin(), "Min bound", results);
        }
        else {
            this.validateBound(intRange.getInclusiveMax(), "Max bound", results);
        }
    }
    
    private void validateBound(final int value, final String boundName, @Nonnull final ValidationResults results) {
        if (this.min != null) {
            if (this.inclusive) {
                if (value < this.min) {
                    results.fail(boundName + " must be greater than or equal to " + this.min);
                }
            }
            else if (value <= this.min) {
                results.fail(boundName + " must be greater than " + this.min);
            }
        }
        if (this.max != null) {
            if (this.inclusive) {
                if (value > this.max) {
                    results.fail(boundName + " must be less than or equal to " + this.max);
                }
            }
            else if (value >= this.max) {
                results.fail(boundName + " must be less than " + this.max);
            }
        }
    }
    
    @Override
    public void updateSchema(final SchemaContext context, final Schema target) {
        if (!(target instanceof ArraySchema)) {
            throw new IllegalArgumentException();
        }
        final ArraySchema arraySchema = (ArraySchema)target;
        final Schema[] items = (Schema[])arraySchema.getItems();
        if (items == null) {
            throw new IllegalArgumentException();
        }
        if (this.lowerBound) {
            if (!(items[0] instanceof IntegerSchema)) {
                throw new IllegalArgumentException();
            }
            this.updateSchemaBound((IntegerSchema)items[0]);
        }
        else {
            if (!(items[1] instanceof IntegerSchema)) {
                throw new IllegalArgumentException();
            }
            this.updateSchemaBound((IntegerSchema)items[1]);
        }
    }
    
    private void updateSchemaBound(@Nonnull final IntegerSchema integerSchema) {
        if (this.min != null) {
            if (this.inclusive) {
                integerSchema.setMinimum(this.min);
            }
            else {
                integerSchema.setExclusiveMinimum(this.min);
            }
        }
        if (this.max != null) {
            if (this.inclusive) {
                integerSchema.setMaximum(this.max);
            }
            else {
                integerSchema.setExclusiveMaximum(this.max);
            }
        }
    }
}
