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

package com.hypixel.hytale.server.core.modules.entity.stamina;

import com.hypixel.hytale.server.core.asset.type.gameplay.GameplayConfig;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatValue;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatsModule;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.server.core.entity.movement.MovementStatesSystems;
import com.hypixel.hytale.component.dependency.Order;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.ResourceType;
import com.hypixel.hytale.server.core.entity.movement.MovementStatesComponent;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;

public class StaminaSystems
{
    public static class SprintStaminaEffectSystem extends EntityTickingSystem<EntityStore>
    {
        private final ComponentType<EntityStore, Player> playerComponentType;
        private final ComponentType<EntityStore, EntityStatMap> entityStatMapComponentType;
        private final ComponentType<EntityStore, MovementStatesComponent> movementStatesComponentType;
        private final ResourceType<EntityStore, SprintStaminaRegenDelay> sprintRegenDelayResourceType;
        private final Query<EntityStore> query;
        private final Set<Dependency<EntityStore>> dependencies;
        
        public SprintStaminaEffectSystem() {
            this.playerComponentType = Player.getComponentType();
            this.entityStatMapComponentType = EntityStatMap.getComponentType();
            this.movementStatesComponentType = MovementStatesComponent.getComponentType();
            this.sprintRegenDelayResourceType = SprintStaminaRegenDelay.getResourceType();
            this.query = (Query<EntityStore>)Query.and(this.playerComponentType, this.entityStatMapComponentType, this.movementStatesComponentType);
            this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.BEFORE, MovementStatesSystems.TickingSystem.class), new SystemDependency(Order.BEFORE, EntityStatsModule.PlayerRegenerateStatsSystem.class));
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nonnull
        @Override
        public Set<Dependency<EntityStore>> getDependencies() {
            return this.dependencies;
        }
        
        @Override
        public void tick(final float dt, final int systemIndex, @Nonnull final Store<EntityStore> store) {
            if (!this.updateResource(store)) {
                return;
            }
            super.tick(dt, systemIndex, store);
        }
        
        @Override
        public void tick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final MovementStatesComponent movementStates = archetypeChunk.getComponent(index, this.movementStatesComponentType);
            if (!movementStates.getMovementStates().sprinting && movementStates.getSentMovementStates().sprinting) {
                final SprintStaminaRegenDelay regenDelay = store.getResource(this.sprintRegenDelayResourceType);
                final EntityStatMap statMap = archetypeChunk.getComponent(index, this.entityStatMapComponentType);
                final EntityStatValue statValue = statMap.get(regenDelay.getIndex());
                if (statValue != null && statValue.get() <= regenDelay.getValue()) {
                    return;
                }
                statMap.setStatValue(regenDelay.getIndex(), regenDelay.getValue());
            }
        }
        
        protected boolean updateResource(@Nonnull final Store<EntityStore> store) {
            final SprintStaminaRegenDelay resource = store.getResource(this.sprintRegenDelayResourceType);
            if (resource.validate()) {
                return resource.hasDelay();
            }
            final GameplayConfig gameplayConfig = store.getExternalData().getWorld().getGameplayConfig();
            final StaminaGameplayConfig staminaConfig = gameplayConfig.getPluginConfig().get((Class<? extends StaminaGameplayConfig>)StaminaGameplayConfig.class);
            if (staminaConfig == null || staminaConfig.getSprintRegenDelay().getIndex() == Integer.MIN_VALUE) {
                resource.markEmpty();
                return false;
            }
            final StaminaGameplayConfig.SprintRegenDelayConfig regenDelay = staminaConfig.getSprintRegenDelay();
            resource.update(regenDelay.getIndex(), regenDelay.getValue());
            return resource.hasDelay();
        }
    }
}
