package pw.yumc.Residence.commandsub;

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.permissions.PermissionGroup;
import com.bekvon.bukkit.residence.permissions.PermissionManager;
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 CommandUnStuck extends BaseCommand {
    ResidenceMain plugin;

    public CommandUnStuck(final ResidenceMain plugin) {
        super("us");
        this.plugin = plugin;
        setOnlyPlayerExecutable();
    }

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

        if (!group.hasUnstuckAccess()) {
            player.sendMessage(ChatColor.RED + language.getPhrase("NoPermission"));
            return;
        }
        final ClaimedResidence res = rmanager.getByLoc(player.getLocation());
        if (res == null) {
            player.sendMessage(ChatColor.RED + language.getPhrase("NotInResidence"));
        } else {
            player.sendMessage(ChatColor.YELLOW + language.getPhrase("Moved") + "...");
            player.teleport(res.getOutsideFreeLoc(player.getLocation()));
        }
    }

}
