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 pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.text.Language;

public class CommandSet extends BaseCommand {
    ResidenceMain plugin;

    public CommandSet(final ResidenceMain plugin) {
        super("set");
        this.plugin = plugin;
        setMinimumArguments(2);
        setOnlyPlayerExecutable();
        setPossibleArguments("<residence> [flag] [true/false/remove]");
    }

    @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();

        if (args.length == 2) {
            final String res = rmanager.getNameByLoc(player.getLocation());
            if (res != null) {
                rmanager.getByName(res).getPermissions().setFlag(player, args[0], args[1], resadmin);
            } else {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            }
        } else if (args.length == 3) {
            final ClaimedResidence res = rmanager.getByName(args[0]);
            if (res != null) {
                res.getPermissions().setFlag(player, args[1], args[2], resadmin);
            } else {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            }
        }
    }

}
