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.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.ResidenceManager;

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

public class CommandMessage extends BaseCommand {
    ResidenceMain plugin;

    public CommandMessage(final ResidenceMain plugin) {
        super();
        this.plugin = plugin;
        setMinimumArguments(1);
        setOnlyPlayerExecutable();
        setPossibleArguments("[enter|leave] [消息(移除则留空)]");
    }

    @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 ClaimedResidence res = rmanager.getByLoc(player.getLocation());
        if (res == null) {
            player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            return;
        }
        String message = null;
        if (args.length != 1) {
            message = StringUtil.consolidateStrings(args, 1);
        }
        boolean enter = false;
        if (args[0].equalsIgnoreCase("enter")) {
            enter = true;
        }
        res.setEnterLeaveMessage(player, message, enter, resadmin);
    }

}
