package com.creatubbles.ctbmod.common.command;

import java.io.File;
import java.io.FileFilter;
import java.nio.file.Files;

import com.creatubbles.api.request.amazon.UploadS3ImageRequest;
import com.creatubbles.api.request.creation.CreateCreationRequest;
import com.creatubbles.api.request.creation.CreationsUploadsRequest;
import com.creatubbles.api.request.creation.PingCreationsUploadsRequest;
import com.creatubbles.api.response.creation.CreateCreationResponse;
import com.creatubbles.api.response.creation.CreationsUploadsResponse;

import lombok.SneakyThrows;
import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.NumberInvalidException;
import net.minecraft.command.WrongUsageException;

import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.lang3.ArrayUtils;

public class CommandUpload extends ClientCommandBase {

    @Override
    public String getCommandName() {
        return "ctb-upload";
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "/ctb-upload <name> <screenshot number>";
    }

    @Override
    @SneakyThrows
    public void processCommand(ICommandSender sender, String[] args) throws CommandException {
        if (args.length < 2) {
            throw new WrongUsageException("Not enough arguments. Usage: %s", getCommandUsage(sender));
        }
        
        if (CommandLogin.accessToken == null) {
            throw new WrongUsageException("/ctb-login must be run before this command can be used");
        }
        // create creation
        CreateCreationRequest createCreation = new CreateCreationRequest(CommandLogin.accessToken);
        createCreation.setData("{\"name\":\"" + args[0] + "\"}");
        CreateCreationResponse createCreationResponse = createCreation.execute().getResponse();
        System.out.println(createCreationResponse.creation.id);

        // create url for upload
        CreationsUploadsRequest creationsUploads = new CreationsUploadsRequest(createCreationResponse.creation.id, CommandLogin.accessToken);
        CreationsUploadsResponse creationsUploadsResponse = creationsUploads.execute().getResponse();
        System.out.println(creationsUploadsResponse.url);

        File screenshotsFolder = new File(Minecraft.getMinecraft().mcDataDir, "screenshots");
        File[] screenshots = screenshotsFolder.listFiles((FileFilter) FileFilterUtils.suffixFileFilter(".png"));
        ArrayUtils.reverse(screenshots);

        int id;
        try {
            id = Integer.valueOf(args[args.length - 1]);
        } catch (Exception e) {
            throw new NumberInvalidException("%s is not a valid ID number. Must be a number from 0 to %d", args[args.length - 1], screenshots.length - 1);
        }

        byte[] data = Files.readAllBytes(screenshots[id].toPath());

        // upload image to s3
        UploadS3ImageRequest uploadS3Image = new UploadS3ImageRequest(data, creationsUploadsResponse.url);
        uploadS3Image.execute().getResponse();

        PingCreationsUploadsRequest pingCreationsUploads = new PingCreationsUploadsRequest(creationsUploadsResponse.id, CommandLogin.accessToken);
        pingCreationsUploads.setData(""); // fixes null PUT error
        System.out.println(pingCreationsUploads.execute().getResponse().message);
    }
}

@@ -793,16 +793,222 @@
 ception;
+%0Aimport net.minecraft.event.ClickEvent;%0Aimport net.minecraft.event.HoverEvent;%0Aimport net.minecraft.util.ChatComponentText;%0Aimport net.minecraft.util.ChatStyle;%0Aimport net.minecraft.util.EnumChatFormatting;
 %0A%0Aimport
@@ -1159,16 +1159,105 @@
 Base %7B%0A%0A
+    public static final String URL_BASE = %22https://www.creatubbles.com/creations/%22;%0A    %0A
     @Ove
@@ -1443,17 +1443,17 @@
  %3Cname%3E 
-%3C
+%5B
 screensh
@@ -1461,17 +1461,17 @@
 t number
-%3E
+%5D
 %22;%0A    %7D
@@ -1629,9 +1629,9 @@
 h %3C 
-2
+1
 ) %7B%0A
@@ -2208,35 +2208,45 @@
         
-System.out.println(
+%0A        String creationID = 
 createCr
@@ -2271,17 +2271,16 @@
 ation.id
-)
 ;%0A%0A     
@@ -2554,66 +2554,8 @@
 e();
-%0A        System.out.println(creationsUploadsResponse.url);
 %0A%0A  
@@ -2815,16 +2815,20 @@
   int id
+ = 0
 ;%0A      
@@ -2927,132 +2927,209 @@
 e) %7B
-%0A            throw new NumberInvalidException(%22%25s is not a valid ID number. Must be a number from 0 to %25d%22, args%5Barg
+%7D%0A        %0A        if (id %3E= screenshots.length) %7B%0A            throw new NumberInvalidException(%22ID %25d is too high! You only have %25d screenshots, so ID must be between 0 and %25d%22, id, screenshot
 s.length
  - 1
@@ -3124,21 +3124,16 @@
 s.length
- - 1%5D
 , screen
@@ -3641,39 +3641,154 @@
     
-System.out.println(ping
+pingCreationsUploads.execute();%0A        %0A        sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN.toString().concat(%22%5B
 Creation
 sUpl
@@ -3787,48 +3787,320 @@
 tion
-sU
+ u
 pload
-s.execute().getResponse().message
+ successful! (Click to view)%5D%22)).setChatStyle(new ChatStyle()%0A                .setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, URL_BASE + creationID))%0A                .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(%22Click to view creation on website.%22))))
 );%0A 

Fix #4 and clean up the upload command some