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

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

import javax.annotation.Nullable;
import java.time.Instant;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.builtin.instances.removal.InstanceDataResource;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.system.tick.DelayedEntitySystem;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.protocol.packets.interface_.PortalDef;
import com.hypixel.hytale.protocol.packets.interface_.PortalState;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.protocol.packets.interface_.UpdatePortal;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.builtin.portals.resources.PortalWorld;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.RefSystem;

public final class PortalTrackerSystems
{
    private PortalTrackerSystems() {
    }
    
    public static class TrackerSystem extends RefSystem<EntityStore>
    {
        @Override
        public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final PortalWorld portalWorld = store.getResource(PortalWorld.getResourceType());
            if (!portalWorld.exists()) {
                return;
            }
            final World world = store.getExternalData().getWorld();
            final PlayerRef playerRef = commandBuffer.getComponent(ref, PlayerRef.getComponentType());
            if (portalWorld.getSeesUi().add(playerRef.getUuid())) {
                final UpdatePortal packet = portalWorld.createFullPacket(world);
                playerRef.getPacketHandler().write(packet);
            }
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final PortalWorld portalWorld = store.getResource(PortalWorld.getResourceType());
            if (!portalWorld.exists()) {
                return;
            }
            final PlayerRef playerRef = commandBuffer.getComponent(ref, PlayerRef.getComponentType());
            playerRef.getPacketHandler().write(new UpdatePortal(null, null));
            portalWorld.getSeesUi().remove(playerRef.getUuid());
        }
        
        @Override
        public Query<EntityStore> getQuery() {
            return (Query<EntityStore>)Query.and(Player.getComponentType(), PlayerRef.getComponentType());
        }
    }
    
    public static class UiTickingSystem extends DelayedEntitySystem<EntityStore>
    {
        public UiTickingSystem() {
            super(1.0f);
        }
        
        @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 PortalWorld portalWorld = store.getResource(PortalWorld.getResourceType());
            if (!portalWorld.exists()) {
                return;
            }
            final World world = store.getExternalData().getWorld();
            final Store<ChunkStore> chunkStore = world.getChunkStore().getStore();
            final InstanceDataResource instanceData = chunkStore.getResource(InstanceDataResource.getResourceType());
            final Instant timeout = instanceData.getTimeoutTimer();
            if (timeout == null) {
                return;
            }
            final PlayerRef playerRef = archetypeChunk.getComponent(index, PlayerRef.getComponentType());
            final UpdatePortal packet = portalWorld.getSeesUi().add(playerRef.getUuid()) ? portalWorld.createFullPacket(world) : portalWorld.createUpdatePacket(world);
            playerRef.getPacketHandler().write(packet);
        }
        
        @Nullable
        @Override
        public Query<EntityStore> getQuery() {
            return (Query<EntityStore>)Query.and(Player.getComponentType(), PlayerRef.getComponentType());
        }
    }
}
