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

public class CommandPset extends BaseCommand {
    ResidenceMain plugin;

    public CommandPset(final ResidenceMain plugin) {
        super("ps");
        this.plugin = plugin;
        setMinimumArguments(1);
        setOnlyPlayerExecutable();
        setPossibleArguments("<residence> [player] [flag|removeall] [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 == 1) {
            final ClaimedResidence area = rmanager.getByLoc(player.getLocation());
            if (area != null) {
                FlagUtil.open(area, player, args[0], resadmin);
            } else {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            }
        } else if (args.length == 2 && args[1].equalsIgnoreCase("removeall")) {
            final ClaimedResidence area = rmanager.getByLoc(player.getLocation());
            if (area != null) {
                area.getPermissions().removeAllPlayerFlags(player, args[1], resadmin);
            } else {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            }
        } else if (args.length == 3 && args[2].equalsIgnoreCase("removeall")) {
            final ClaimedResidence area = rmanager.getByName(args[0]);
            if (area != null) {
                area.getPermissions().removeAllPlayerFlags(player, args[2], resadmin);
            } else {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            }
        } else if (args.length == 3) {
            final ClaimedResidence area = rmanager.getByLoc(player.getLocation());
            if (area != null) {
                area.getPermissions().setPlayerFlag(player, args[0], args[1], args[2], resadmin);
            } else {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            }
        } else if (args.length == 4) {
            final ClaimedResidence area = rmanager.getByName(args[0]);
            if (area != null) {
                area.getPermissions().setPlayerFlag(player, args[1], args[2], args[3], resadmin);
            } else {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            }
        }
    }
}
