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

    public CommandSetOwner(final ResidenceMain plugin) {
        super("so");
        this.plugin = plugin;
        setMinimumArguments(2);
        setPossibleArguments("[领地名] [玩家]");
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        final boolean resadmin = (command != null);
        final ResidenceManager rmanager = plugin.getResidenceManager();
        final Language language = plugin.getLanguage();
        if (!resadmin) {
            sender.sendMessage(ChatColor.RED + language.getPhrase("NoPermission"));
            return;
        }
        final ClaimedResidence area = rmanager.getByName(args[0]);
        if (area != null) {
            area.getPermissions().setOwner(args[1], true);
            if (area.getParent() == null) {
                sender.sendMessage(
                        ChatColor.GREEN + language.getPhrase("ResidenceOwnerChange", ChatColor.YELLOW + " " + args[0] + " " + ChatColor.GREEN + "." + ChatColor.YELLOW + args[1] + ChatColor.GREEN));
            } else {
                sender.sendMessage(ChatColor.GREEN + language.getPhrase("SubzoneOwnerChange",
                        ChatColor.YELLOW + " " + args[0].split("\\.")[args[1].split("\\.").length - 1] + " " + ChatColor.GREEN + "." + ChatColor.YELLOW + args[1] + ChatColor.GREEN));
            }
        } else {
            sender.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
        }
    }

}
