package pw.yumc.Residence.commandsub;

import org.bukkit.ChatColor;
import org.bukkit.Material;
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 CommandLset extends BaseCommand {
    ResidenceMain plugin;

    public CommandLset(final ResidenceMain plugin) {
        super();
        this.plugin = plugin;
        setMinimumArguments(2);
        setOnlyPlayerExecutable();
        setPossibleArguments("<领地名称> [blacklist(bl)/ignorelist(il)||info] [material]");
    }

    @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.getByName(args[0]);

        if (res == null) {
            player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            return;
        }

        Material mat = null;

        if (args.length == 3) {
            try {
                mat = Material.valueOf(args[3].toUpperCase());
            } catch (final Exception ex) {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidMaterial"));
                return;
            }
        }

        final String subcmd = args[1];

        switch (subcmd) {

        case "info":
            player.sendMessage(ChatColor.RED + "Blacklist:");
            res.getItemBlacklist().printList(player);
            player.sendMessage(ChatColor.GREEN + "Ignorelist:");
            res.getItemIgnoreList().printList(player);
            return;
        case "bl":
        case "blacklist":
            res.getItemBlacklist().playerListChange(player, mat, resadmin);
            return;
        case "il":
        case "ignorelist":
            res.getItemIgnoreList().playerListChange(player, mat, resadmin);
            return;
        }
    }

}
