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

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

import com.hypixel.hytale.builtin.adventure.objectives.task.ObjectiveTask;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;
import javax.annotation.Nullable;
import java.util.Iterator;
import com.hypixel.hytale.builtin.adventure.objectives.ObjectiveDataStore;
import java.util.Set;
import java.util.UUID;
import com.hypixel.hytale.builtin.adventure.objectives.ObjectivePlugin;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.builtin.adventure.objectives.Objective;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractCommandCollection;

public class ObjectiveCompleteCommand extends AbstractCommandCollection
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_OBJECTIVE_OBJECTIVE_NOT_FOUND;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_OBJECTIVE_NO_TASK_FOR_INDEX;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_OBJECTIVE_TASK_ALREADY_COMPLETED;
    
    public ObjectiveCompleteCommand() {
        super("complete", "server.commands.objective.complete");
        this.addSubCommand(new CompleteTaskCommand());
        this.addSubCommand(new CompleteTaskSetCommand());
        this.addSubCommand(new CompleteObjectiveCommand());
    }
    
    @Nullable
    private static Objective getObjectiveFromId(@Nonnull final Ref<EntityStore> participantRef, @Nonnull final String objectiveId, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final Player playerComponent = componentAccessor.getComponent(participantRef, Player.getComponentType());
        if (playerComponent == null) {
            return null;
        }
        final Set<UUID> activeObjectiveUUIDs = playerComponent.getPlayerConfigData().getActiveObjectiveUUIDs();
        final ObjectiveDataStore objectiveDataStore = ObjectivePlugin.get().getObjectiveDataStore();
        for (final UUID objectiveUUID : activeObjectiveUUIDs) {
            final Objective objective = objectiveDataStore.getObjective(objectiveUUID);
            if (objective == null) {
                continue;
            }
            if (objective.getObjectiveId().equals(objectiveId)) {
                return objective;
            }
        }
        return null;
    }
    
    static {
        MESSAGE_COMMANDS_OBJECTIVE_OBJECTIVE_NOT_FOUND = Message.translation("server.commands.objective.objectiveNotFound");
        MESSAGE_COMMANDS_OBJECTIVE_NO_TASK_FOR_INDEX = Message.translation("server.commands.objective.noTaskForIndex");
        MESSAGE_COMMANDS_OBJECTIVE_TASK_ALREADY_COMPLETED = Message.translation("server.commands.objective.taskAlreadyCompleted");
    }
    
    public static class CompleteTaskCommand extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<String> objectiveArg;
        @Nonnull
        private final RequiredArg<Integer> taskIndexArg;
        
        public CompleteTaskCommand() {
            super("task", "server.commands.objective.complete.task");
            this.objectiveArg = this.withRequiredArg("objectiveId", "server.commands.objective.complete.task.arg.objectiveId.desc", ArgTypes.STRING);
            this.taskIndexArg = this.withRequiredArg("taskIndex", "server.commands.objective.complete.task.arg.taskIndex.desc", ArgTypes.INTEGER);
        }
        
        @Override
        protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
            final String objectiveId = this.objectiveArg.get(context);
            final int taskIndex = this.taskIndexArg.get(context);
            final Objective objective = ObjectiveCompleteCommand.getObjectiveFromId(ref, objectiveId, store);
            if (objective == null) {
                context.sendMessage(ObjectiveCompleteCommand.MESSAGE_COMMANDS_OBJECTIVE_OBJECTIVE_NOT_FOUND);
                return;
            }
            final ObjectiveTask[] tasks = objective.getCurrentTasks();
            if (taskIndex >= tasks.length) {
                context.sendMessage(ObjectiveCompleteCommand.MESSAGE_COMMANDS_OBJECTIVE_NO_TASK_FOR_INDEX);
                return;
            }
            if (tasks[taskIndex].isComplete()) {
                context.sendMessage(ObjectiveCompleteCommand.MESSAGE_COMMANDS_OBJECTIVE_TASK_ALREADY_COMPLETED);
                return;
            }
            tasks[taskIndex].complete(objective, store);
            objective.checkTaskSetCompletion(store);
        }
    }
    
    public static class CompleteTaskSetCommand extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<String> objectiveArg;
        
        public CompleteTaskSetCommand() {
            super("taskSet", "server.commands.objective.complete.taskSet");
            this.objectiveArg = this.withRequiredArg("objectiveId", "server.commands.objective.complete.taskSet.arg.objectiveId.desc", ArgTypes.STRING);
        }
        
        @Override
        protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
            final String objectiveId = this.objectiveArg.get(context);
            final Objective objective = ObjectiveCompleteCommand.getObjectiveFromId(ref, objectiveId, store);
            if (objective == null) {
                context.sendMessage(ObjectiveCompleteCommand.MESSAGE_COMMANDS_OBJECTIVE_OBJECTIVE_NOT_FOUND);
                return;
            }
            final ObjectiveTask[] tasks = objective.getCurrentTasks();
            if (tasks == null || tasks.length == 0) {
                context.sendMessage(ObjectiveCompleteCommand.MESSAGE_COMMANDS_OBJECTIVE_NO_TASK_FOR_INDEX);
                return;
            }
            for (final ObjectiveTask task : tasks) {
                task.complete(objective, store);
            }
            objective.checkTaskSetCompletion(store);
        }
    }
    
    public static class CompleteObjectiveCommand extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<String> objectiveArg;
        
        public CompleteObjectiveCommand() {
            super("objective", "server.commands.objective.complete.objective");
            this.objectiveArg = this.withRequiredArg("objectiveId", "server.commands.objective.complete.objective.arg.objectiveId.desc", ArgTypes.STRING);
        }
        
        @Override
        protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
            final String objectiveId = this.objectiveArg.get(context);
            final Objective objective = ObjectiveCompleteCommand.getObjectiveFromId(ref, objectiveId, store);
            if (objective == null) {
                context.sendMessage(ObjectiveCompleteCommand.MESSAGE_COMMANDS_OBJECTIVE_OBJECTIVE_NOT_FOUND);
                return;
            }
            final ObjectiveTask[] tasks = objective.getCurrentTasks();
            if (tasks == null) {
                context.sendMessage(ObjectiveCompleteCommand.MESSAGE_COMMANDS_OBJECTIVE_NO_TASK_FOR_INDEX);
                return;
            }
            for (final ObjectiveTask task : tasks) {
                task.completeTransactionRecords();
            }
            objective.complete(store);
        }
    }
}
