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.permissions.PermissionManager;
import com.bekvon.bukkit.residence.protection.ResidenceManager;

import cn.citycraft.PluginHelper.commands.BaseCommand;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.economy.TransactionManager;
import pw.yumc.Residence.economy.rent.RentManager;
import pw.yumc.Residence.manager.ConfigManager;
import pw.yumc.Residence.selection.SelectionManager;
import pw.yumc.Residence.text.Language;

public class CommandMarket extends BaseCommand {
    ConfigManager cmanager;
    PermissionManager gmanager;
    Language language;
    ResidenceMain plugin;
    RentManager rentmanager;
    ResidenceManager rmanager;
    SelectionManager smanager;
    TransactionManager tmanager;

    public CommandMarket(final ResidenceMain plugin) {
        super();
        this.plugin = plugin;
        rmanager = plugin.getResidenceManager();
        language = plugin.getLanguage();
        smanager = plugin.getSelectionManager();
        gmanager = plugin.getPermissionManager();
        rentmanager = plugin.getRentManager();
        cmanager = plugin.getConfigManager();
        tmanager = plugin.getTransactionManager();
        setMinimumArguments(1);
        setOnlyPlayerExecutable();
    }

    @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);
        int page = 1;
        try {
            if (args.length > 0) {
                page = Integer.parseInt(args[args.length - 1]);
            }
        } catch (final Exception ex) {
        }

        final String cmd = args[0].toLowerCase();
        if (cmd.equals("list")) {
            commandResMarketList(args, resadmin, player, page);
            return;
        }
        if (cmd.equals("autorenew")) {
            commandResMarketAutorenew(args, resadmin, player, page);
            return;
        }
        if (cmd.equals("rentable")) {
            commandResMarketRentable(args, resadmin, player, page);
            return;
        }
        if (cmd.equals("rent")) {
            commandResMarketRent(args, resadmin, player, page);
            return;
        }

        if (cmd.equals("release")) {
            if (args.length != 2) {
                return;
            }
            if (rentmanager.isRented(args[1])) {
                rentmanager.removeFromForRent(player, args[1], resadmin);
            } else {
                rentmanager.unrent(player, args[1], resadmin);
            }
            return;
        }
        if (cmd.equals("info")) {
            if (args.length == 1) {
                final String areaname = rmanager.getNameByLoc(player.getLocation());
                tmanager.viewSaleInfo(areaname, player);
                if (cmanager.enabledRentSystem() && rentmanager.isForRent(areaname)) {
                    rentmanager.printRentInfo(player, areaname);
                }
            } else if (args.length == 2) {
                tmanager.viewSaleInfo(args[1], player);
                if (cmanager.enabledRentSystem() && rentmanager.isForRent(args[1])) {
                    rentmanager.printRentInfo(player, args[1]);
                }
            } else {
                return;
            }
            return;
        }
        if (cmd.equals("buy")) {
            if (args.length != 2) {
                return;
            }
            tmanager.buyPlot(args[1], player, resadmin);
            return;
        }
        if (cmd.equals("unsell")) {
            if (args.length != 2) {
                return;
            }
            tmanager.removeFromSale(player, args[1], resadmin);
            return;
        }
        if (cmd.equals("sell")) {
            if (args.length != 3) {
                return;
            }
            int amount;
            try {
                amount = Integer.parseInt(args[2]);
            } catch (final Exception ex) {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidAmount"));
                return;
            }
            tmanager.putForSale(args[1], player, amount, resadmin);
            return;
        }
    }

    private boolean commandResMarketAutorenew(final String[] args, final boolean resadmin, final Player player, final int page) {
        if (!cmanager.enableEconomy()) {
            player.sendMessage(ChatColor.RED + language.getPhrase("MarketDisabled"));
            return true;
        }
        if (args.length != 3) {
            return true;
        }
        boolean value;
        if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("t")) {
            value = true;
        } else if (args[2].equalsIgnoreCase("false") || args[2].equalsIgnoreCase("f")) {
            value = false;
        } else {
            player.sendMessage(ChatColor.RED + language.getPhrase("InvalidBoolean"));
            return true;
        }
        if (rentmanager.isRented(args[1]) && rentmanager.getRentingPlayer(args[1]).equalsIgnoreCase(player.getName())) {
            rentmanager.setRentedRepeatable(player, args[1], value, resadmin);
        } else if (rentmanager.isForRent(args[1])) {
            rentmanager.setRentRepeatable(player, args[1], value, resadmin);
        } else {
            player.sendMessage(ChatColor.RED + language.getPhrase("RentReleaseInvalid", ChatColor.YELLOW + args[1] + ChatColor.RED));
        }
        return true;
    }

    private boolean commandResMarketList(final String[] args, final boolean resadmin, final Player player, final int page) {
        if (!cmanager.enableEconomy()) {
            player.sendMessage(ChatColor.RED + language.getPhrase("MarketDisabled"));
            return true;
        }
        player.sendMessage(ChatColor.BLUE + "---" + language.getPhrase("MarketList") + "---");
        tmanager.printForSaleResidences(player);
        if (cmanager.enabledRentSystem()) {
            rentmanager.printRentableResidences(player);
        }
        return true;
    }

    private boolean commandResMarketRent(final String[] args, final boolean resadmin, final Player player, final int page) {
        if (args.length < 2 || args.length > 3) {
            return false;
        }
        boolean repeat = false;
        if (args.length == 3) {
            if (args[2].equalsIgnoreCase("t") || args[2].equalsIgnoreCase("true")) {
                repeat = true;
            } else if (!args[2].equalsIgnoreCase("f") && !args[2].equalsIgnoreCase("false")) {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidBoolean"));
                return true;
            }
        }
        rentmanager.rent(player, args[1], repeat, resadmin);
        return true;
    }

    private boolean commandResMarketRentable(final String[] args, final boolean resadmin, final Player player, final int page) {
        if (args.length < 4 || args.length > 5) {
            return false;
        }
        if (!cmanager.enabledRentSystem()) {
            player.sendMessage(ChatColor.RED + language.getPhrase("RentDisabled"));
            return true;
        }
        int days;
        int cost;
        try {
            cost = Integer.parseInt(args[2]);
        } catch (final Exception ex) {
            player.sendMessage(ChatColor.RED + language.getPhrase("InvalidCost"));
            return true;
        }
        try {
            days = Integer.parseInt(args[3]);
        } catch (final Exception ex) {
            player.sendMessage(ChatColor.RED + language.getPhrase("InvalidDays"));
            return true;
        }
        boolean repeat = false;
        if (args.length == 5) {
            final String state = args[4];
            if (state.equalsIgnoreCase("t") || state.equalsIgnoreCase("true")) {
                repeat = true;
            } else if (!state.equalsIgnoreCase("f") && !state.equalsIgnoreCase("false")) {
                player.sendMessage(ChatColor.RED + language.getPhrase("InvalidBoolean"));
                return true;
            }
        }
        rentmanager.setForRent(player, args[1], cost, days, repeat, resadmin);
        return true;
    }

}
