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

package com.hypixel.hytale.component;

import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.query.Query;

public class ComponentType<ECS_TYPE, T extends Component<ECS_TYPE>> implements Comparable<ComponentType<ECS_TYPE, ?>>, Query<ECS_TYPE>
{
    @Nonnull
    public static final ComponentType[] EMPTY_ARRAY;
    private ComponentRegistry<ECS_TYPE> registry;
    private Class<? super T> tClass;
    private int index;
    private boolean invalid;
    
    public ComponentType() {
        this.invalid = true;
    }
    
    void init(@Nonnull final ComponentRegistry<ECS_TYPE> registry, @Nonnull final Class<? super T> tClass, final int index) {
        this.registry = registry;
        this.tClass = tClass;
        this.index = index;
        this.invalid = false;
    }
    
    @Nonnull
    public ComponentRegistry<ECS_TYPE> getRegistry() {
        return this.registry;
    }
    
    @Nonnull
    public Class<? super T> getTypeClass() {
        return this.tClass;
    }
    
    public int getIndex() {
        return this.index;
    }
    
    void invalidate() {
        this.invalid = true;
    }
    
    boolean isValid() {
        return !this.invalid;
    }
    
    @Override
    public boolean test(@Nonnull final Archetype<ECS_TYPE> archetype) {
        return archetype.contains(this);
    }
    
    @Override
    public boolean requiresComponentType(final ComponentType<ECS_TYPE, ?> componentType) {
        return this.equals(componentType);
    }
    
    @Override
    public void validateRegistry(@Nonnull final ComponentRegistry<ECS_TYPE> registry) {
        if (!this.registry.equals(registry)) {
            throw new IllegalArgumentException("ComponentType is for a different registry! " + String.valueOf(this));
        }
    }
    
    @Override
    public void validate() {
        if (this.invalid) {
            throw new IllegalStateException("ComponentType is invalid!");
        }
    }
    
    @Override
    public int compareTo(@Nonnull final ComponentType<ECS_TYPE, ?> o) {
        return Integer.compare(this.index, o.index);
    }
    
    @Override
    public boolean equals(@Nullable final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        final ComponentType<?, ?> that = (ComponentType<?, ?>)o;
        return this.index == that.index && this.registry.equals(that.registry);
    }
    
    @Override
    public int hashCode() {
        int result = this.registry.hashCode();
        result = 31 * result + this.index;
        return result;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "ComponentType{registry=" + String.valueOf(this.registry.getClass()) + "@" + this.registry.hashCode() + ", typeClass=" + String.valueOf(this.tClass) + ", index=" + this.index;
    }
    
    static {
        EMPTY_ARRAY = new ComponentType[0];
    }
}
