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

package com.hypixel.hytale.builtin.adventure.objectives.systems;

import com.hypixel.hytale.component.RemoveReason;
import java.util.Map;
import com.hypixel.hytale.server.core.entity.entities.player.data.PlayerWorldData;
import com.hypixel.hytale.server.core.universe.world.World;
import java.util.Iterator;
import com.hypixel.hytale.server.core.entity.entities.player.data.PlayerConfigData;
import java.util.Set;
import com.hypixel.hytale.builtin.adventure.objectives.config.gameplayconfig.ObjectiveGameplayConfig;
import java.util.UUID;
import com.hypixel.hytale.builtin.adventure.objectives.ObjectivePlugin;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.entity.entities.Player;
import javax.annotation.Nonnull;
import com.hypixel.hytale.builtin.adventure.objectives.components.ObjectiveHistoryComponent;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.RefSystem;

public class ObjectivePlayerSetupSystem extends RefSystem<EntityStore>
{
    @Nonnull
    private final ComponentType<EntityStore, ObjectiveHistoryComponent> objectiveHistoryComponentType;
    @Nonnull
    private final ComponentType<EntityStore, Player> playerComponentType;
    @Nonnull
    private final ComponentType<EntityStore, UUIDComponent> uuidComponentType;
    @Nonnull
    private final Query<EntityStore> query;
    
    public ObjectivePlayerSetupSystem(@Nonnull final ComponentType<EntityStore, ObjectiveHistoryComponent> objectiveHistoryComponentType, @Nonnull final ComponentType<EntityStore, Player> playerComponentType) {
        this.uuidComponentType = UUIDComponent.getComponentType();
        this.objectiveHistoryComponentType = objectiveHistoryComponentType;
        this.playerComponentType = playerComponentType;
        this.query = (Query<EntityStore>)Query.and(playerComponentType, this.uuidComponentType);
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
    
    @Override
    public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        commandBuffer.ensureComponent(ref, this.objectiveHistoryComponentType);
        final Player playerComponent = store.getComponent(ref, this.playerComponentType);
        assert playerComponent != null;
        final UUIDComponent uuidComponent = store.getComponent(ref, this.uuidComponentType);
        assert uuidComponent != null;
        final ObjectivePlugin objectiveModule = ObjectivePlugin.get();
        final UUID playerUuid = uuidComponent.getUuid();
        final PlayerConfigData playerConfigData = playerComponent.getPlayerConfigData();
        final Set<UUID> activeObjectiveUUIDs = playerConfigData.getActiveObjectiveUUIDs();
        if (activeObjectiveUUIDs != null) {
            for (final UUID objectiveUUID : activeObjectiveUUIDs) {
                objectiveModule.addPlayerToExistingObjective(store, playerUuid, objectiveUUID);
            }
        }
        final World world = store.getExternalData().getWorld();
        final String worldName = world.getName();
        final PlayerWorldData perWorldData = playerConfigData.getPerWorldData(worldName);
        if (perWorldData.isFirstSpawn()) {
            final ObjectiveGameplayConfig config = ObjectiveGameplayConfig.get(world.getGameplayConfig());
            final Map<String, String> starterObjectiveLinePerWorld = (config != null) ? config.getStarterObjectiveLinePerWorld() : null;
            if (starterObjectiveLinePerWorld != null) {
                final String objectiveLineId = starterObjectiveLinePerWorld.get(worldName);
                if (objectiveLineId != null) {
                    objectiveModule.startObjectiveLine(store, objectiveLineId, Set.of(playerUuid), world.getWorldConfig().getUuid(), null);
                }
            }
        }
    }
    
    @Override
    public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
    }
}
