package com.zivilon.cinder_loe.command;

import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;

import com.zivilon.cinder_loe.character.CharacterRoleAPI;
import com.zivilon.cinder_loe.util.Utilities;

import java.util.UUID;

public class CommandCinderCharacter extends CommandBase {

    @Override
    public String getCommandName() {
        return "cinder_character";
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "/cinder_character <set/get/remove> <character_name> [player_name]";
    }

    @Override
    public int getRequiredPermissionLevel() {
        return 4;
    }

    @Override
    public void processCommand(ICommandSender sender, String[] args) {
        if(args.length < 2) {
            sender.addChatMessage(new ChatComponentText("Incorrect arguments. Usage: " + getCommandUsage(sender)));
            return;
        }

        String action = args[0];
        String character = args[1];

        if (action.equals("set")) {
            if(args.length < 3) {
                // Set character UUID to player UUID
                sender.addChatMessage(new ChatComponentText("Incorrect arguments. Please specify player: " + getCommandUsage(sender)));
            } else {
                String player = args[2];
                EntityPlayerMP player_entity = MinecraftServer.getServer().getConfigurationManager().func_152612_a(player);
                if (player_entity == null) {
                    sender.addChatMessage(new ChatComponentText("Invalid player name"));
                } else {
                    sender.addChatMessage(new ChatComponentText("Setting player " + player_entity.getCommandSenderName() + " as " + character));
                    CharacterRoleAPI.setCharacterRoleUUID(character, player_entity.getUniqueID());
                }
            }
        } else if (action.equals("remove")) {
            UUID uuid = CharacterRoleAPI.getCharacterRoleUUID(character);
            if (uuid == null) {
                sender.addChatMessage(new ChatComponentText("Invalid character name"));
                return;
            }

            sender.addChatMessage(new ChatComponentText("Cleared player for character " + character));
            CharacterRoleAPI.removeCharacterRole(character);
        } else if (action.equals("get")) {
            UUID uuid = CharacterRoleAPI.getCharacterRoleUUID(character);
            if (uuid == null) {
                sender.addChatMessage(new ChatComponentText("Character " + character + " is unclaimed"));
                return;
            }
            String player_name = Utilities.getPlayerByUUID(uuid).getCommandSenderName();
            if (player_name != null) {
                sender.addChatMessage(new ChatComponentText("Character " + character + " is currently set to " + player_name));
                return;
            }
            sender.addChatMessage(new ChatComponentText("Character " + character + " is currently set to offline player " + uuid.toString()));
        } else {
            sender.addChatMessage(new ChatComponentText("Incorrect arguments. Usage: " + getCommandUsage(sender)));
            return;
        }
    }
}
