package org.soraworld.itemsaver.common;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Himmelt
 */
public class CommandSaver extends CommandBase {
    @Override
    public String getName() {
        return "itemsaver";
    }

    @Override
    public String getUsage(ICommandSender sender) {
        return "isv give|add|set|remove|open";
    }

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

    @Override
    public List<String> getAliases() {
        return Arrays.asList("saver", "isv");
    }

    @Override
    public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
        if (args.length > 0) {
            switch (args[0]) {
                case "give":
                    if (args.length >= 3) {
                        EntityPlayerMP target = getPlayer(server, sender, args[1]);
                        String type = args[2];
                        String name = null;
                        if (args.length >= 4) {
                            name = args[3];
                        }
                        ItemTypeData data = CommonProxy.getTypeData(server, type);
                        int amount = args.length >= 5 ? Integer.parseInt(args[4]) : -1;
                        data.give(target, name, amount);
                        CommandBase.notifyCommandListener(sender, this, "commands.give.success", new TextComponentString(" [" + type + (name == null ? "" : "-" + name) + "] "), 1, target.getName());
                    } else {
                        sender.sendMessage(new TextComponentString("/isv give <player> type [item] [amount]"));
                    }
                    break;
                case "add":
                    if (args.length == 3) {
                        if (sender instanceof EntityPlayerMP) {
                            ItemStack stack = ((EntityPlayerMP) sender).getHeldItem(EnumHand.MAIN_HAND);
                            if (CommonProxy.addItem(server, args[1], args[2], stack)) {
                                sender.sendMessage(new TextComponentString("物品已添加!"));
                            } else {
                                sender.sendMessage(new TextComponentString("相应名称物品已存在，请更换名称或使用 set 覆盖 !"));
                            }
                        } else {
                            sender.sendMessage(new TextComponentString("此命令只能有游戏内玩家执行 !"));
                        }
                    } else {
                        sender.sendMessage(new TextComponentString("/isv add type name"));
                    }
                    break;
                case "set":
                    if (args.length == 3) {
                        if (sender instanceof EntityPlayerMP) {
                            ItemStack stack = ((EntityPlayerMP) sender).getHeldItem(EnumHand.MAIN_HAND);
                            CommonProxy.setItem(server, args[1], args[2], stack);
                            sender.sendMessage(new TextComponentString("物品已设置!"));
                        } else {
                            sender.sendMessage(new TextComponentString("此命令只能有游戏内玩家执行 !"));
                        }
                    } else {
                        sender.sendMessage(new TextComponentString("/isv set type name"));
                    }
                    break;
                case "remove":
                    break;
                case "open":
                    if (sender instanceof EntityPlayerMP) {
                        CommonProxy.openMenu((EntityPlayerMP) sender);
                    } else {
                        sender.sendMessage(new TextComponentString("此命令只能有游戏内玩家执行 !"));
                    }
                    break;
                default:
                    sender.sendMessage(new TextComponentString(getUsage(sender)));
            }
        } else {
            sender.sendMessage(new TextComponentString(getUsage(sender)));
        }
    }

    @Override
    public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
        return sender.canUseCommand(2, "gamemode");
    }

    @Override
    public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) {
        if ("give".equals(args[0])) {
            switch (args.length) {
                case 2:
                    return CommandBase.getListOfStringsMatchingLastWord(args, server.getOnlinePlayerNames());
                case 3:
                    return CommonProxy.getListOfTypes(server, args[args.length - 1]);
                case 4:
                    return CommonProxy.getListOfNames(server, args[2], args[args.length - 1]);
                default:
            }
        }
        return new ArrayList<>();
    }
}

@@ -2104,20 +2104,22 @@
 player%3E 
+%3C
 type
+%3E
  %5Bitem%5D 
@@ -3028,25 +3028,29 @@
 isv add 
+%3C
 type
- 
+%3E %3C
 name
+%3E
 %22));%0A   
@@ -3762,17 +3762,21 @@
 set 
+%3C
 type
- 
+%3E %3C
 name
+%3E
 %22));
@@ -3852,24 +3852,736 @@
 e %22remove%22:%0A
+                    if (args.length %3E= 2) %7B%0A                        String type = args%5B1%5D;%0A                        if (args.length %3E= 3) %7B%0A                            CommonProxy.getTypeData(server, type).remove(args%5B2%5D);%0A                            sender.sendMessage(new TextComponentString(%22%E5%B7%B2%E7%A7%BB%E9%99%A4%E5%88%86%E7%B1%BB - %22 + type + %22 %E4%B8%8B%E7%9A%84%E7%89%A9%E5%93%81 - %22 + args%5B2%5D));%0A                        %7D else %7B%0A                            CommonProxy.getTypeData(server, type).clear();%0A                            sender.sendMessage(new TextComponentString(%22%E5%B7%B2%E7%A7%BB%E9%99%A4%E5%88%86%E7%B1%BB - %22 + type));%0A                        %7D%0A                    %7D else %7B%0A                        sender.sendMessage(new TextComponentString(%22/isv remove %3Ctype%3E %5Bname%5D%22));%0A                    %7D%0A
             

add remove