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 CommandCheck extends BaseCommand {
    ResidenceMain plugin;

    public CommandCheck(final ResidenceMain plugin) {
        super();
        this.plugin = plugin;
        setMinimumArguments(2);
        setOnlyPlayerExecutable();
        setPossibleArguments("<领地名称> <权限> [玩家]");
    }

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

        if (args.length == 2 || args.length == 3) {
            if (args.length == 3) {
                pname = args[2];
            }
            final ClaimedResidence res = rmanager.getByName(args[0]);
            if (res == null) {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
                return;
            }
            if (!res.getPermissions().hasApplicableFlag(pname, args[1])) {
                player.sendMessage(language.getPhrase("FlagCheckFalse",
                        ChatColor.YELLOW + args[0] + ChatColor.RED + "." + ChatColor.YELLOW + pname + ChatColor.RED + "." + ChatColor.YELLOW + args[0] + ChatColor.RED));
            } else {
                player.sendMessage(language.getPhrase("FlagCheckTrue",
                        ChatColor.GREEN + args[0] + ChatColor.YELLOW + "." + ChatColor.GREEN + pname + ChatColor.YELLOW + "." + ChatColor.YELLOW + args[0] + ChatColor.RED + "."
                                + (res.getPermissions().playerHas(pname, res.getPermissions().getWorld(), args[1], false) ? ChatColor.GREEN + "TRUE" : ChatColor.RED + "FALSE")));
            }
        }
    }

}
