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

package com.hypixel.hytale.server.core.permissions.commands.op;

import java.util.Set;
import java.util.UUID;
import com.hypixel.hytale.server.core.modules.singleplayer.SingleplayerModule;
import com.hypixel.hytale.server.core.Constants;
import com.hypixel.hytale.server.core.permissions.PermissionsModule;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class OpSelfCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_OP_ADDED;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_OP_REMOVED;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_NON_VANILLA_PERMISSIONS;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_SINGLEPLAYER_OWNER_REQ;
    
    public OpSelfCommand() {
        super("self", "server.commands.op.self.desc");
    }
    
    @Override
    protected boolean canGeneratePermission() {
        return false;
    }
    
    @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) {
        if (PermissionsModule.get().areProvidersTampered()) {
            playerRef.sendMessage(OpSelfCommand.MESSAGE_COMMANDS_NON_VANILLA_PERMISSIONS);
            return;
        }
        if (Constants.SINGLEPLAYER && !SingleplayerModule.isOwner(playerRef)) {
            playerRef.sendMessage(OpSelfCommand.MESSAGE_COMMANDS_SINGLEPLAYER_OWNER_REQ);
            return;
        }
        if (!Constants.SINGLEPLAYER && !Constants.ALLOWS_SELF_OP_COMMAND) {
            playerRef.sendMessage(Message.translation("server.commands.op.self.multiplayerTip").param("uuidCommand", "uuid").param("permissionFile", "permissions.json").param("launchArg", "--allow-op"));
            return;
        }
        final UUID uuid = playerRef.getUuid();
        final PermissionsModule perms = PermissionsModule.get();
        final String opGroup = "OP";
        final Set<String> groups = perms.getGroupsForUser(uuid);
        if (groups.contains("OP")) {
            perms.removeUserFromGroup(uuid, "OP");
            context.sendMessage(OpSelfCommand.MESSAGE_COMMANDS_OP_REMOVED);
        }
        else {
            perms.addUserToGroup(uuid, "OP");
            context.sendMessage(OpSelfCommand.MESSAGE_COMMANDS_OP_ADDED);
        }
    }
    
    static {
        MESSAGE_COMMANDS_OP_ADDED = Message.translation("server.commands.op.self.added");
        MESSAGE_COMMANDS_OP_REMOVED = Message.translation("server.commands.op.self.removed");
        MESSAGE_COMMANDS_NON_VANILLA_PERMISSIONS = Message.translation("server.commands.op.self.nonVanillaPermissions");
        MESSAGE_COMMANDS_SINGLEPLAYER_OWNER_REQ = Message.translation("server.commands.op.self.singleplayerOwnerReq");
    }
}
