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

package com.hypixel.hytale.codec.validation.validator;

import com.hypixel.hytale.codec.schema.config.IntegerSchema;
import com.hypixel.hytale.codec.schema.config.NumberSchema;
import java.util.logging.Level;
import com.hypixel.hytale.codec.schema.config.Schema;
import com.hypixel.hytale.codec.schema.SchemaContext;
import javax.annotation.Nonnull;
import com.hypixel.hytale.codec.validation.ValidationResults;
import javax.annotation.Nullable;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.codec.validation.Validator;

public class RangeValidator<T extends Comparable<T>> implements Validator<T>
{
    private static final HytaleLogger LOGGER;
    private final T min;
    private final T max;
    private final boolean inclusive;
    
    public RangeValidator(final T min, final T max, final boolean inclusive) {
        this.min = min;
        this.max = max;
        this.inclusive = inclusive;
    }
    
    @Override
    public void accept(@Nullable final T t, @Nonnull final ValidationResults results) {
        if (t == null) {
            return;
        }
        if (this.min != null) {
            final int compare = t.compareTo(this.min);
            if (this.inclusive) {
                if (compare < 0) {
                    results.fail("Must be greater than or equal to " + String.valueOf(this.min));
                }
            }
            else if (compare < 0 || compare == 0) {
                results.fail("Must be greater than " + String.valueOf(this.min));
            }
        }
        if (this.max != null) {
            final int compare = t.compareTo(this.max);
            if (this.inclusive) {
                if (compare > 0) {
                    results.fail("Must be less than or equal to " + String.valueOf(this.max));
                }
            }
            else if (compare > 0 || compare == 0) {
                results.fail("Must be less than " + String.valueOf(this.max));
            }
        }
    }
    
    @Override
    public void updateSchema(final SchemaContext context, final Schema target) {
        if (this.min != null && !(this.min instanceof Number)) {
            RangeValidator.LOGGER.at(Level.WARNING).log("Can't handle: min is not a Number: %s, %s", this.min, this.min.getClass());
            return;
        }
        if (this.max != null && !(this.max instanceof Number)) {
            RangeValidator.LOGGER.at(Level.WARNING).log("Can't handle: max is not a Number: %s, %s", this.max, this.max.getClass());
            return;
        }
        if (!(target instanceof NumberSchema) && !(target instanceof IntegerSchema)) {
            boolean failed = true;
            if (target.getAnyOf() != null) {
                for (final Schema schema : target.getAnyOf()) {
                    if (schema instanceof NumberSchema || schema instanceof IntegerSchema) {
                        this.updateSchema(schema);
                        failed = false;
                    }
                }
            }
            if (failed) {
                RangeValidator.LOGGER.at(Level.WARNING).log("Can't handle: %s as a range: %s", target.getHytale().getType(), target);
            }
            return;
        }
        this.updateSchema(target);
    }
    
    private void updateSchema(final Schema target) {
        if (target instanceof final IntegerSchema i) {
            if (this.min != null) {
                final Number v = (Number)this.min;
                if (this.inclusive) {
                    i.setMinimum(v.intValue());
                }
                else {
                    i.setExclusiveMinimum(v.intValue());
                }
            }
            if (this.max != null) {
                final Number v = (Number)this.max;
                if (this.inclusive) {
                    i.setMaximum(v.intValue());
                }
                else {
                    i.setExclusiveMaximum(v.intValue());
                }
            }
        }
        else {
            final NumberSchema j = (NumberSchema)target;
            if (this.min != null) {
                final Number v2 = (Number)this.min;
                if (this.inclusive) {
                    j.setMinimum(v2.doubleValue());
                }
                else {
                    j.setExclusiveMinimum(v2.doubleValue());
                }
            }
            if (this.max != null) {
                final Number v2 = (Number)this.max;
                if (this.inclusive) {
                    j.setMaximum(v2.doubleValue());
                }
                else {
                    j.setExclusiveMaximum(v2.doubleValue());
                }
            }
        }
    }
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
    }
}
