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

package com.hypixel.hytale.builtin.ambience.systems;

import com.hypixel.hytale.builtin.ambience.resources.AmbienceResource;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import javax.annotation.Nullable;
import com.hypixel.hytale.protocol.packets.world.UpdateEnvironmentMusic;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.component.system.HolderSystem;
import com.hypixel.hytale.component.Archetype;
import com.hypixel.hytale.builtin.ambience.components.AmbienceTracker;
import com.hypixel.hytale.server.core.universe.PlayerRef;
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.query.Query;

public class ForcedMusicSystems
{
    private static final Query<EntityStore> TICK_QUERY;
    
    static {
        TICK_QUERY = Archetype.of(Player.getComponentType(), PlayerRef.getComponentType(), AmbienceTracker.getComponentType());
    }
    
    public static class PlayerAdded extends HolderSystem<EntityStore>
    {
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            holder.ensureComponent(AmbienceTracker.getComponentType());
        }
        
        @Override
        public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
            final AmbienceTracker ambienceTrackerComponent = holder.getComponent(AmbienceTracker.getComponentType());
            assert ambienceTrackerComponent != null;
            final PlayerRef playerRefComponent = holder.getComponent(PlayerRef.getComponentType());
            assert playerRefComponent != null;
            final UpdateEnvironmentMusic pooledPacket = ambienceTrackerComponent.getMusicPacket();
            pooledPacket.environmentIndex = 0;
            playerRefComponent.getPacketHandler().write(pooledPacket);
        }
        
        @Nullable
        @Override
        public Query<EntityStore> getQuery() {
            return (Query<EntityStore>)Query.and(PlayerRef.getComponentType(), AmbienceTracker.getComponentType());
        }
    }
    
    public static class Tick extends EntityTickingSystem<EntityStore>
    {
        @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 AmbienceResource ambienceResource = store.getResource(AmbienceResource.getResourceType());
            final AmbienceTracker tracker = archetypeChunk.getComponent(index, AmbienceTracker.getComponentType());
            final PlayerRef playerRef = archetypeChunk.getComponent(index, PlayerRef.getComponentType());
            final int have = tracker.getForcedMusicIndex();
            final int desired = ambienceResource.getForcedMusicIndex();
            if (have == desired) {
                return;
            }
            tracker.setForcedMusicIndex(desired);
            final UpdateEnvironmentMusic pooledPacket = tracker.getMusicPacket();
            pooledPacket.environmentIndex = desired;
            playerRef.getPacketHandler().write(pooledPacket);
        }
        
        @Nullable
        @Override
        public Query<EntityStore> getQuery() {
            return ForcedMusicSystems.TICK_QUERY;
        }
    }
}
