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

package com.hypixel.hytale.component.dependency;

import com.hypixel.hytale.component.ComponentRegistry;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.system.ISystem;

public class SystemDependency<ECS_TYPE, T extends ISystem<ECS_TYPE>> extends Dependency<ECS_TYPE>
{
    @Nonnull
    private final Class<T> systemClass;
    
    public SystemDependency(@Nonnull final Order order, @Nonnull final Class<T> systemClass) {
        this(order, systemClass, OrderPriority.NORMAL);
    }
    
    public SystemDependency(@Nonnull final Order order, @Nonnull final Class<T> systemClass, final int priority) {
        super(order, priority);
        this.systemClass = systemClass;
    }
    
    public SystemDependency(@Nonnull final Order order, @Nonnull final Class<T> systemClass, @Nonnull final OrderPriority priority) {
        super(order, priority);
        this.systemClass = systemClass;
    }
    
    @Nonnull
    public Class<T> getSystemClass() {
        return this.systemClass;
    }
    
    @Override
    public void validate(@Nonnull final ComponentRegistry<ECS_TYPE> registry) {
        if (!registry.hasSystemClass(this.systemClass)) {
            throw new IllegalArgumentException("SystemType dependency isn't registered: " + String.valueOf(this.systemClass));
        }
    }
    
    @Override
    public void resolveGraphEdge(@Nonnull final ComponentRegistry<ECS_TYPE> registry, @Nonnull final ISystem<ECS_TYPE> thisSystem, @Nonnull final DependencyGraph<ECS_TYPE> graph) {
        switch (this.order) {
            case BEFORE: {
                for (final ISystem<ECS_TYPE> system : graph.getSystems()) {
                    if (this.systemClass.equals(system.getClass())) {
                        graph.addEdge(thisSystem, system, -this.priority);
                    }
                }
                break;
            }
            case AFTER: {
                for (final ISystem<ECS_TYPE> system : graph.getSystems()) {
                    if (this.systemClass.equals(system.getClass())) {
                        graph.addEdge(system, thisSystem, this.priority);
                    }
                }
                break;
            }
        }
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "SystemDependency{systemClass=" + String.valueOf(this.systemClass) + "} " + super.toString();
    }
}
