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

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

import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.component.ArchetypeChunk;
import java.util.UUID;
import java.util.Set;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.component.CommandBuffer;
import javax.annotation.Nullable;
import com.hypixel.hytale.math.vector.Vector3d;
import java.util.List;
import com.hypixel.hytale.protocol.packets.worldmap.ContextMenuItem;
import com.hypixel.hytale.protocol.packets.worldmap.MapMarker;
import com.hypixel.hytale.server.core.util.PositionUtil;
import com.hypixel.hytale.math.vector.Transform;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.Ref;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import com.hypixel.hytale.builtin.adventure.objectives.transaction.TransactionRecord;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.builtin.adventure.objectives.Objective;
import com.hypixel.hytale.builtin.adventure.objectives.config.task.ReachLocationTaskAsset;
import com.hypixel.hytale.builtin.adventure.objectives.config.task.ObjectiveTaskAsset;
import com.hypixel.hytale.builtin.adventure.objectives.markers.reachlocation.ReachLocationMarker;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.codec.builder.BuilderCodec;

public class ReachLocationTask extends ObjectiveTask
{
    public static final BuilderCodec<ReachLocationTask> CODEC;
    @Nonnull
    public static String MARKER_ICON;
    private static final ComponentType<EntityStore, TransformComponent> TRANSFORM_COMPONENT_TYPE;
    private static final ComponentType<EntityStore, ReachLocationMarker> REACH_LOCATION_MARKER_COMPONENT_TYPE;
    private boolean completed;
    private boolean markerLoaded;
    
    public ReachLocationTask(@Nonnull final ObjectiveTaskAsset asset, final int taskSetIndex, final int taskIndex) {
        super(asset, taskSetIndex, taskIndex);
    }
    
    protected ReachLocationTask() {
    }
    
    @Override
    public boolean checkCompletion() {
        return this.completed;
    }
    
    @Nonnull
    @Override
    public ReachLocationTaskAsset getAsset() {
        return (ReachLocationTaskAsset)this.asset;
    }
    
    @Nonnull
    private String getMarkerId(@Nonnull final Objective objective) {
        return String.format("ReachLocation_%s_%d", objective.getObjectiveUUID(), this.taskIndex);
    }
    
    @Nullable
    @Override
    protected TransactionRecord[] setup0(@Nonnull final Objective objective, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        if (!this.markers.isEmpty()) {
            return null;
        }
        final String targetLocationId = this.getAsset().getTargetLocationId();
        final List<Ref<EntityStore>> reachLocationMarkerEntities = new ObjectArrayList<Ref<EntityStore>>();
        store.forEachChunk(ReachLocationTask.REACH_LOCATION_MARKER_COMPONENT_TYPE, (archetypeChunk, componentStoreCommandBuffer) -> {
            for (int index = 0; index < archetypeChunk.size(); ++index) {
                final ReachLocationMarker reachLocationMarkerComponent = archetypeChunk.getComponent(index, ReachLocationTask.REACH_LOCATION_MARKER_COMPONENT_TYPE);
                if (reachLocationMarkerComponent != null) {
                    if (!(!reachLocationMarkerComponent.getMarkerId().equals(targetLocationId))) {
                        reachLocationMarkerEntities.add(archetypeChunk.getReferenceTo(index));
                    }
                }
            }
            return;
        });
        if (!reachLocationMarkerEntities.isEmpty()) {
            final Vector3d currentLocation = objective.getPosition(store);
            final Ref<EntityStore> closestMarker = reachLocationMarkerEntities.getFirst();
            final TransformComponent closestMarkerTransformComponent = store.getComponent(closestMarker, ReachLocationTask.TRANSFORM_COMPONENT_TYPE);
            assert closestMarkerTransformComponent != null;
            final ReachLocationMarker closestMarkerReachComponent = store.getComponent(closestMarker, ReachLocationTask.REACH_LOCATION_MARKER_COMPONENT_TYPE);
            assert closestMarkerReachComponent != null;
            Vector3d closestPosition = closestMarkerTransformComponent.getPosition();
            double shortestDistance = closestPosition.distanceSquaredTo(currentLocation);
            String closestLocationName = closestMarkerReachComponent.getLocationName();
            for (int i = 1; i < reachLocationMarkerEntities.size(); ++i) {
                final Ref<EntityStore> markerEntityReference = reachLocationMarkerEntities.get(i);
                final TransformComponent markerTransformComponent = store.getComponent(markerEntityReference, ReachLocationTask.TRANSFORM_COMPONENT_TYPE);
                assert markerTransformComponent != null;
                final ReachLocationMarker markerReachLocationComponent = store.getComponent(markerEntityReference, ReachLocationTask.REACH_LOCATION_MARKER_COMPONENT_TYPE);
                assert markerReachLocationComponent != null;
                final Vector3d pos = markerTransformComponent.getPosition();
                final double distance = pos.distanceSquaredTo(currentLocation);
                final String locationName = markerReachLocationComponent.getLocationName();
                if (distance < shortestDistance && locationName != null) {
                    shortestDistance = distance;
                    closestPosition = pos;
                    closestLocationName = locationName;
                }
            }
            if (closestLocationName != null) {
                this.addMarker(new MapMarker(this.getMarkerId(objective), closestLocationName, ReachLocationTask.MARKER_ICON, PositionUtil.toTransformPacket(new Transform(closestPosition)), null));
                this.markerLoaded = true;
                return null;
            }
        }
        return null;
    }
    
    public void setupMarker(@Nonnull final Objective objective, @Nonnull final ReachLocationMarker locationMarkerEntity, @Nonnull final Vector3d position, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        if (this.markerLoaded) {
            return;
        }
        final String markerId = locationMarkerEntity.getMarkerId();
        if (!markerId.equals(this.getAsset().getTargetLocationId())) {
            return;
        }
        final String locationName = locationMarkerEntity.getLocationName();
        if (locationName == null) {
            return;
        }
        this.addMarker(new MapMarker(this.getMarkerId(objective), locationName, ReachLocationTask.MARKER_ICON, PositionUtil.toTransformPacket(new Transform(position)), null));
        this.markerLoaded = true;
    }
    
    @Nonnull
    @Override
    public com.hypixel.hytale.protocol.ObjectiveTask toPacket(@Nonnull final Objective objective) {
        final com.hypixel.hytale.protocol.ObjectiveTask packet = new com.hypixel.hytale.protocol.ObjectiveTask();
        packet.taskDescriptionKey = this.asset.getDescriptionKey(objective.getObjectiveId(), this.taskSetIndex, this.taskIndex);
        packet.currentCompletion = (this.completed ? 1 : 0);
        packet.completionNeeded = 1;
        return packet;
    }
    
    public void onPlayerReachLocationMarker(@Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final String locationMarkerId, @Nonnull final Objective objective) {
        if (!locationMarkerId.equals(this.getAsset().getTargetLocationId())) {
            return;
        }
        final UUIDComponent uuidComponent = store.getComponent(ref, UUIDComponent.getComponentType());
        assert uuidComponent != null;
        if (!objective.getActivePlayerUUIDs().contains(uuidComponent.getUuid())) {
            return;
        }
        if (!this.areTaskConditionsFulfilled(store, ref, null)) {
            return;
        }
        this.completed = true;
        objective.markDirty();
        this.sendUpdateObjectiveTaskPacket(objective);
        this.consumeTaskConditions(store, ref, objective.getActivePlayerUUIDs());
        this.complete(objective, store);
        objective.checkTaskSetCompletion(store);
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "ReachLocationTask{completed=" + this.completed + "} " + super.toString();
    }
    
    static {
        CODEC = BuilderCodec.builder(ReachLocationTask.class, ReachLocationTask::new, ReachLocationTask.BASE_CODEC).append(new KeyedCodec<Boolean>("Completed", Codec.BOOLEAN), (reachLocationTask, aBoolean) -> reachLocationTask.completed = aBoolean, reachLocationTask -> reachLocationTask.completed).add().append(new KeyedCodec("MarkerLoaded", Codec.BOOLEAN), (reachLocationTask, transactionRecord) -> reachLocationTask.markerLoaded = transactionRecord, reachLocationTask -> reachLocationTask.markerLoaded).add().build();
        ReachLocationTask.MARKER_ICON = "Home.png";
        TRANSFORM_COMPONENT_TYPE = TransformComponent.getComponentType();
        REACH_LOCATION_MARKER_COMPONENT_TYPE = ReachLocationMarker.getComponentType();
    }
}
