package net.buycraft.plugin.sponge.command;

import com.google.common.base.Optional;
import lombok.AllArgsConstructor;
import net.buycraft.plugin.client.ApiException;
import net.buycraft.plugin.data.Package;
import net.buycraft.plugin.sponge.BuycraftPlugin;
import net.buycraft.plugin.sponge.tasks.SendCheckoutLinkTask;
import net.buycraft.plugin.util.Node;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.service.pagination.PaginationBuilder;
import org.spongepowered.api.service.pagination.PaginationService;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.action.TextActions;
import org.spongepowered.api.text.format.TextColors;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
public class ListPackagesCmd implements CommandExecutor {

    private final BuycraftPlugin plugin;

    @Override
    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {

        if (plugin.getApiClient() == null) {
            src.sendMessage(Text.builder("Set up a secret key first with /buycraft secret.").color(TextColors.RED).build());
            return CommandResult.success();
        }

        if (plugin.getListingUpdateTask().getListing() == null) {
            src.sendMessage(Text.builder("We're currently retrieving the listing. Sit tight!").color(TextColors.RED).build());
            return CommandResult.success();
        }

        try {
            sendPaginatedMessage(new Node(plugin.getListingUpdateTask().getListing().getCategories(), new ArrayList<Package>(), "Categories", Optional
                    .absent()), src);
        } catch (IOException | ApiException e) {
            e.printStackTrace();
        }

        return CommandResult.success();
    }

    private void sendPaginatedMessage(Node node, CommandSource source) throws IOException, ApiException {
        PaginationService paginationService = Sponge.getServiceManager().provide(PaginationService.class).get();
        PaginationBuilder builder = paginationService.builder();
        List<Text> contents = node.getSubcategories().stream()
                .map(category -> Text.builder("> " + category.getName()).color(TextColors.AQUA).onClick(TextActions.executeCallback(commandSource -> {
                    if (commandSource instanceof Player) {
                        try {
                            sendPaginatedMessage(node.getChild(category), source);
                        } catch (IOException | ApiException e) {
                            e.printStackTrace();
                        }
                    }
                })).build()).collect(Collectors.toList());
        for (Package p : node.getPackages()) {
            contents.add(Text.builder(p.getName()).color(TextColors.GRAY).append(Text.builder(" for $x.".replace("$", plugin.getApiClient()
                    .getServerInformation().getAccount().getCurrency().getSymbol()).replace("x", "" + p.getEffectivePrice())).color(TextColors
                    .BLUE).build()).onClick(TextActions.executeCallback(commandSource -> {
                if (commandSource instanceof Player) {
                    new SendCheckoutLinkTask(plugin, p.getId(), (Player) commandSource).run();
                }
            })).build());
        }
        builder.title(Text.builder("BuycraftX Listing").color(TextColors.AQUA).build()).contents(contents).paddingString("-").sendTo(source);
    }

}

@@ -2608,20 +2608,20 @@
 tColors.
-AQUA
+GRAY
 ).onClic
@@ -3177,20 +3177,21 @@
 tColors.
-GRAY
+WHITE
 ).append
@@ -3214,68 +3214,109 @@
 for 
-$x.%22.replace(%22$%22, plugin.getApiClient()%0A                    
+%22).color(TextColors.GRAY).build())%0A                    .append(Text.builder(%22$x.%22.replace(%22$%22, plugin
 .get
@@ -3374,16 +3374,45 @@
 ymbol())
+%0A                            
 .replace
@@ -3462,16 +3462,32 @@
 xtColors
+.GREEN).build())
 %0A       
@@ -3503,23 +3503,8 @@
     
-.BLUE).build())
 .onC
@@ -3630,16 +3630,50 @@
         
+plugin.getPlatform().executeAsync(
 new Send
@@ -3735,13 +3735,8 @@
 rce)
-.run(
 );%0A 

Improve listing.