package pw.yumc.Residence.commandsub;

import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.ResidenceManager;

import cn.citycraft.PluginHelper.commands.BaseCommand;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.text.Language;

public class CommandRemove extends BaseCommand {
    ResidenceMain plugin;

    public CommandRemove(final ResidenceMain plugin) {
        super("delete");
        this.plugin = plugin;
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        Player player = null;
        final boolean resadmin = (command != null);
        final ResidenceManager rmanager = plugin.getResidenceManager();
        final Map<String, String> deleteConfirm = plugin.getDeleteConfirm();
        final Language language = plugin.getLanguage();
        if (sender instanceof Player) {
            player = (Player) sender;
            final String area = rmanager.getNameByLoc(player.getLocation());
            if (area != null) {
                final ClaimedResidence res = rmanager.getByName(area);
                if (res.getParent() != null) {
                    final String[] split = area.split("\\.");
                    final String words = split[split.length - 1];
                    if (!deleteConfirm.containsKey(player.getName()) || !area.equalsIgnoreCase(deleteConfirm.get(player.getName()))) {
                        player.sendMessage(ChatColor.RED + language.getPhrase("DeleteSubzoneConfirm", ChatColor.YELLOW + words + ChatColor.RED));
                        deleteConfirm.put(player.getName(), area);
                    } else {
                        rmanager.removeResidence(player, area, resadmin);
                    }
                } else {
                    if (!deleteConfirm.containsKey(player.getName()) || !area.equalsIgnoreCase(deleteConfirm.get(player.getName()))) {
                        player.sendMessage(ChatColor.RED + language.getPhrase("DeleteConfirm", ChatColor.YELLOW + area + ChatColor.RED));
                        deleteConfirm.put(player.getName(), area);
                    } else {
                        rmanager.removeResidence(player, area, resadmin);
                    }
                }
                return;
            }
        }
        if (args.length != 1) {
            return;
        }
        if (player != null) {
            if (!deleteConfirm.containsKey(player.getName()) || !args[0].equalsIgnoreCase(deleteConfirm.get(player.getName()))) {
                String words = null;
                if (rmanager.getByName(args[0]) != null) {
                    final ClaimedResidence res = rmanager.getByName(args[0]);
                    if (res.getParent() != null) {
                        final String[] split = args[0].split("\\.");
                        words = split[split.length - 1];
                    }
                }
                if (words == null) {
                    player.sendMessage(ChatColor.RED + language.getPhrase("DeleteConfirm", ChatColor.YELLOW + args[0] + ChatColor.RED));
                } else {
                    player.sendMessage(ChatColor.RED + language.getPhrase("DeleteSubzoneConfirm", ChatColor.YELLOW + words + ChatColor.RED));
                }
                deleteConfirm.put(player.getName(), args[0]);
            } else {
                rmanager.removeResidence(player, args[0], resadmin);
            }
        } else if (!deleteConfirm.containsKey("Console") || !args[0].equalsIgnoreCase(deleteConfirm.get("Console"))) {
            String words = null;
            if (rmanager.getByName(args[0]) != null) {
                final ClaimedResidence res = rmanager.getByName(args[1]);
                if (res.getParent() != null) {
                    final String[] split = args[0].split("\\.");
                    words = split[split.length - 1];
                }
            }
            if (words == null) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + language.getPhrase("DeleteConfirm", ChatColor.YELLOW + args[0] + ChatColor.RED));
            } else {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + language.getPhrase("DeleteSubzoneConfirm", ChatColor.YELLOW + words + ChatColor.RED));
            }
            deleteConfirm.put("Console", args[0]);
        } else {
            rmanager.removeResidence(args[0]);
        }
    }

}
