// 
// 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.SystemType;
import com.hypixel.hytale.component.system.ISystem;

public class SystemTypeDependency<ECS_TYPE, T extends ISystem<ECS_TYPE>> extends Dependency<ECS_TYPE>
{
    @Nonnull
    private final SystemType<ECS_TYPE, T> systemType;
    
    public SystemTypeDependency(@Nonnull final Order order, @Nonnull final SystemType<ECS_TYPE, T> systemType) {
        this(order, systemType, OrderPriority.NORMAL);
    }
    
    public SystemTypeDependency(@Nonnull final Order order, @Nonnull final SystemType<ECS_TYPE, T> systemType, final int priority) {
        super(order, priority);
        this.systemType = systemType;
    }
    
    public SystemTypeDependency(@Nonnull final Order order, @Nonnull final SystemType<ECS_TYPE, T> systemType, @Nonnull final OrderPriority priority) {
        super(order, priority);
        this.systemType = systemType;
    }
    
    @Nonnull
    public SystemType<ECS_TYPE, T> getSystemType() {
        return this.systemType;
    }
    
    @Override
    public void validate(@Nonnull final ComponentRegistry<ECS_TYPE> registry) {
        if (!registry.hasSystemType(this.systemType)) {
            throw new IllegalArgumentException("SystemType dependency isn't registered: " + String.valueOf(this.systemType));
        }
    }
    
    @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.systemType.isType(system)) {
                        graph.addEdge(thisSystem, system, -this.priority);
                    }
                }
                break;
            }
            case AFTER: {
                for (final ISystem<ECS_TYPE> system : graph.getSystems()) {
                    if (this.systemType.isType(system)) {
                        graph.addEdge(system, thisSystem, this.priority);
                    }
                }
                break;
            }
        }
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "SystemTypeDependency{systemType=" + String.valueOf(this.systemType) + "} " + super.toString();
    }
}
