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

package com.hypixel.hytale.builtin.adventure.camera.system;

import com.hypixel.hytale.component.system.EcsEvent;
import com.hypixel.hytale.server.core.asset.type.gameplay.CameraEffectsConfig;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatValue;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.server.core.asset.type.camera.CameraEffect;
import com.hypixel.hytale.server.core.modules.entitystats.asset.DefaultEntityStatTypes;
import com.hypixel.hytale.server.core.modules.entity.damage.Damage;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.modules.entity.damage.DamageModule;
import com.hypixel.hytale.component.SystemGroup;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.modules.entity.damage.DamageEventSystem;

public class CameraEffectSystem extends DamageEventSystem
{
    @Nonnull
    private static final ComponentType<EntityStore, PlayerRef> PLAYER_REF_COMPONENT_TYPE;
    private static final ComponentType<EntityStore, EntityStatMap> ENTITY_STAT_MAP_COMPONENT_TYPE;
    @Nonnull
    private static final Query<EntityStore> QUERY;
    
    @Nullable
    @Override
    public SystemGroup<EntityStore> getGroup() {
        return DamageModule.get().getInspectDamageGroup();
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return CameraEffectSystem.QUERY;
    }
    
    @Override
    public void handle(final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer, @Nonnull final Damage damage) {
        final EntityStatMap entityStatMapComponent = archetypeChunk.getComponent(index, CameraEffectSystem.ENTITY_STAT_MAP_COMPONENT_TYPE);
        assert entityStatMapComponent != null;
        final EntityStatValue healthStat = entityStatMapComponent.get(DefaultEntityStatTypes.getHealth());
        if (healthStat == null) {
            return;
        }
        final float health = healthStat.getMax() - healthStat.getMin();
        if (health <= 0.0f) {
            return;
        }
        final PlayerRef playerRefComponent = archetypeChunk.getComponent(index, CameraEffectSystem.PLAYER_REF_COMPONENT_TYPE);
        assert playerRefComponent != null;
        final World world = commandBuffer.getExternalData().getWorld();
        final CameraEffectsConfig cameraEffectsConfig = world.getGameplayConfig().getCameraEffectsConfig();
        final Damage.CameraEffect effect = damage.getIfPresentMetaObject(Damage.CAMERA_EFFECT);
        final int effectIndex = (effect != null) ? effect.getEffectIndex() : cameraEffectsConfig.getCameraEffectIndex(damage.getDamageCauseIndex());
        if (effectIndex == Integer.MIN_VALUE) {
            return;
        }
        final CameraEffect cameraEffect = CameraEffect.getAssetMap().getAsset(effectIndex);
        if (cameraEffect == null) {
            return;
        }
        final float intensity = MathUtil.clamp(damage.getAmount() / health, 0.0f, 1.0f);
        playerRefComponent.getPacketHandler().writeNoCache(cameraEffect.createCameraShakePacket(intensity));
    }
    
    static {
        PLAYER_REF_COMPONENT_TYPE = PlayerRef.getComponentType();
        ENTITY_STAT_MAP_COMPONENT_TYPE = EntityStatMap.getComponentType();
        QUERY = Query.and(CameraEffectSystem.PLAYER_REF_COMPONENT_TYPE, CameraEffectSystem.ENTITY_STAT_MAP_COMPONENT_TYPE);
    }
}
