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.LeaseManager;
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 CommandLease extends BaseCommand {
    ResidenceMain plugin;

    public CommandLease(final ResidenceMain plugin) {
        super();
        this.plugin = plugin;
        setMinimumArguments(2);
        setOnlyPlayerExecutable();
        setPossibleArguments("[renew/cost] [领地名]");
    }

    @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 LeaseManager leasemanager = plugin.getLeaseManager();

        if (args.length == 1 || args.length == 2) {
            if (args[0].equals("renew")) {
                if (args.length == 2) {
                    leasemanager.renewArea(args[1], player);
                } else {
                    leasemanager.renewArea(rmanager.getNameByLoc(player.getLocation()), player);
                }
                return;
            } else if (args[0].equals("cost")) {
                if (args.length == 2) {
                    final ClaimedResidence res = plugin.getResidenceManager().getByName(args[1]);
                    if (res == null || leasemanager.leaseExpires(args[1])) {
                        final int cost = leasemanager.getRenewCost(res);
                        player.sendMessage(ChatColor.YELLOW + language.getPhrase("LeaseRenewalCost", ChatColor.RED + args[1] + ChatColor.YELLOW + "." + ChatColor.RED + cost + ChatColor.YELLOW));
                    } else {
                        player.sendMessage(ChatColor.RED + language.getPhrase("LeaseNotExpire"));
                    }
                } else {
                    final String area = rmanager.getNameByLoc(player.getLocation());
                    final ClaimedResidence res = rmanager.getByName(area);
                    if (area == null || res == null) {
                        player.sendMessage(ChatColor.RED + language.getPhrase("InvalidArea"));
                        return;
                    }
                    if (leasemanager.leaseExpires(area)) {
                        final int cost = leasemanager.getRenewCost(res);
                        player.sendMessage(ChatColor.YELLOW + language.getPhrase("LeaseRenewalCost", ChatColor.RED + area + ChatColor.YELLOW + "." + ChatColor.RED + cost + ChatColor.YELLOW));
                    } else {
                        player.sendMessage(ChatColor.RED + language.getPhrase("LeaseNotExpire"));
                    }
                }
            }
        } else if (args.length == 3) {
            if (args[0].equals("set")) {
                if (!resadmin) {
                    player.sendMessage(ChatColor.RED + language.getPhrase("NoPermission"));
                    return;
                }
                if (args[2].equals("infinite")) {
                    if (leasemanager.leaseExpires(args[1])) {
                        leasemanager.removeExpireTime(args[1]);
                        player.sendMessage(ChatColor.GREEN + language.getPhrase("LeaseInfinite"));
                    } else {
                        player.sendMessage(ChatColor.RED + language.getPhrase("LeaseNotExpire"));
                    }
                } else {
                    int days;
                    try {
                        days = Integer.parseInt(args[2]);
                    } catch (final Exception ex) {
                        player.sendMessage(ChatColor.RED + language.getPhrase("InvalidDays"));
                        return;
                    }
                    leasemanager.setExpireTime(player, args[1], days);
                }
            }
        }
    }

}
