package pw.yumc.Residence.commandsub;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.Location;
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.permissions.PermissionGroup;
import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.CuboidArea;
import com.bekvon.bukkit.residence.protection.ResidenceManager;

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

public class CommandSelect extends BaseCommand {
    ResidenceMain plugin;

    public CommandSelect(final ResidenceMain plugin) {
        super("s");
        this.plugin = plugin;
        setMinimumArguments(1);
        setOnlyPlayerExecutable();
        setPermission("residence.select");
        setPossibleArguments("请使用/res select ? 查看帮助");
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        final Player player = (Player) sender;
        final boolean resadmin = (command != null);
        final ResidenceManager rmanager = plugin.getResidenceManager();
        final Language language = plugin.getLanguage();
        final PermissionGroup group = plugin.getPermissionManager().getGroup(player);
        final SelectionManager smanager = plugin.getSelectionManager();

        if (!group.selectCommandAccess() && !resadmin) {
            player.sendMessage(ChatColor.RED + language.getPhrase("SelectDiabled"));
            return;
        }
        if (!group.canCreateResidences() && group.getMaxSubzoneDepth() <= 0 && !resadmin) {
            player.sendMessage(ChatColor.RED + language.getPhrase("SelectDiabled"));
            return;
        }

        final String subcmd = args[0];
        switch (args.length) {
        case 1:
            switch (subcmd) {
            case "size":
            case "cost":
                if (smanager.hasPlacedBoth(player.getName())) {
                    try {
                        smanager.showSelectionInfo(player);
                        return;
                    } catch (final Exception ex) {
                        Logger.getLogger(ResidenceMain.class.getName()).log(Level.SEVERE, null, ex);
                        return;
                    }
                } else if (smanager.worldEdit(player)) {
                    try {
                        smanager.showSelectionInfo(player);
                        return;
                    } catch (final Exception ex) {
                        Logger.getLogger(ResidenceMain.class.getName()).log(Level.SEVERE, null, ex);
                        return;
                    }
                }
                return;
            case "vert":
                smanager.vert(player, resadmin);
                return;
            case "sky":
                smanager.sky(player, resadmin);
                return;
            case "bedrock":
                smanager.bedrock(player, resadmin);
                return;
            case "coords":
                final Location playerLoc1 = smanager.getPlayerLoc1(player.getName());
                if (playerLoc1 != null) {
                    player.sendMessage(ChatColor.GREEN + language.getPhrase("Primary.Selection") + ":" + ChatColor.AQUA + " (" + playerLoc1.getBlockX() + ", " + playerLoc1.getBlockY() + ", "
                            + playerLoc1.getBlockZ() + ")");
                }
                final Location playerLoc2 = smanager.getPlayerLoc2(player.getName());
                if (playerLoc2 != null) {
                    player.sendMessage(ChatColor.GREEN + language.getPhrase("Secondary.Selection") + ":" + ChatColor.AQUA + " (" + playerLoc2.getBlockX() + ", " + playerLoc2.getBlockY() + ", "
                            + playerLoc2.getBlockZ() + ")");
                }
                return;
            case "chunk":
                smanager.selectChunk(player);
                return;
            case "worldedit":
                if (smanager.worldEdit(player)) {
                    player.sendMessage(ChatColor.GREEN + plugin.getLanguage().getPhrase("SelectionSuccess"));
                }
                return;
            }
            return;
        case 2:
            int amount;
            try {
                amount = Integer.parseInt(args[1]);
            } catch (final Exception ex) {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidAmount"));
                return;
            }
            switch (subcmd) {
            case "expand":
                smanager.modify(player, false, amount);
                return;
            case "shift":
                smanager.modify(player, true, amount);
                return;
            }
            return;
        case 3:
            try {
                smanager.selectBySize(player, Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
            } catch (final Exception ex) {
                player.sendMessage(ChatColor.RED + language.getPhrase("SelectionFail"));
            }
            return;
        default:
            if (args.length > 0) {
                String resName;
                String areaName;
                ClaimedResidence res = null;
                if (args.length > 1) {
                    res = rmanager.getByName(args[0]);
                } else {
                    res = rmanager.getByLoc(player.getLocation());
                }
                if (res == null) {
                    player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
                    return;
                }
                resName = res.getName();
                CuboidArea area = null;
                if (args.length > 2) {
                    area = res.getArea(args[1]);
                    areaName = args[1];
                } else {
                    areaName = res.getAreaIDbyLoc(player.getLocation());
                    area = res.getArea(areaName);
                }
                if (area != null) {
                    smanager.placeLoc1(player, area.getHighLoc());
                    smanager.placeLoc2(player, area.getLowLoc());
                    player.sendMessage(ChatColor.GREEN + language.getPhrase("SelectionArea", ChatColor.GOLD + areaName + ChatColor.GREEN + "." + ChatColor.GOLD + resName + ChatColor.GREEN));
                } else {
                    player.sendMessage(ChatColor.RED + language.getPhrase("AreaNonExist"));
                }
            }
            return;
        }
    }

}
