package ninja.eivind.hotsreplayuploader.providers;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import ninja.eivind.hotsreplayuploader.models.ReplayFile;
import ninja.eivind.hotsreplayuploader.models.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

public class HotsLogsProvider extends Provider {

    private static final Logger LOG = LoggerFactory.getLogger(HotsLogsProvider.class);
    private static final String ACCESS_KEY_ID = "AKIAIESBHEUH4KAAG4UA";
    private static final String SECRET_ACCESS_KEY = "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";
    private static final String CLIENT_ID = "HotSLogsUploaderFX";
    private static long maintenance;
    private final AmazonS3Client s3Client;

    public HotsLogsProvider() {
        super("HotSLogs.com");
        final AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
        s3Client = new AmazonS3Client(credentials);
    }

    public static boolean isMaintenance() {
        return maintenance + 600000L > System.currentTimeMillis();
    }

    @Override
    public Status upload(final ReplayFile replayFile) {
        if (isMaintenance()) {
            return null;
        }

        String fileName = UUID.randomUUID() + ".StormReplay";
        LOG.info("Assigning remote file name " + fileName + " to " + replayFile);
        String uri = "https://www.hotslogs.com/UploadFile.aspx?Source=" + CLIENT_ID + "&FileName=" + fileName;

        try {
            s3Client.putObject("heroesreplays", fileName, replayFile.getFile());
            LOG.info("File " + fileName + "uploaded to remote storage.");
            String result = getHttpClient().simpleRequest(uri);
            switch (result) {
                case "Duplicate":
                case "Success":
                case "ComputerPlayerFound":
                case "TryMeMode":
                    LOG.info("File registered with HotSLogs.com");
                    return Status.UPLOADED;
                case "PreAlphaWipe":
                    LOG.warn("File not supported by HotSLogs.com");
                    return Status.UNSUPPORTED_GAME_MODE;
                case "Maintenance":
                    LOG.error("HotSLogs.com is currently undergoing maintenance.");
                    maintenance = System.currentTimeMillis();
                    return Status.NEW;
                default:
                    LOG.error("Could not upload file. Unknown status \"" + result + "\" received.");
                    return Status.EXCEPTION;
            }

        } catch (Exception e) {
            LOG.error("Could not upload file.", e);
            return Status.EXCEPTION;
        }

    }
}

@@ -295,16 +295,106 @@
 Status;%0A
+import ninja.eivind.mpq.models.MpqException;%0Aimport ninja.eivind.stormparser.StormParser;%0A
 import o
@@ -443,16 +443,65 @@
 ctory;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A
 import j
@@ -887,16 +887,117 @@
 derFX%22;%0A
+    public static final String BASE_URL = %22https://www.hotslogs.com/UploadFile?Source=%22 + CLIENT_ID;%0A
     priv
@@ -1560,255 +1560,996 @@
     
-String fileName = UUID.randomUUID() + %22.StormReplay%22;%0A        LOG.info(%22Assigning remote file name %22 + fileName + %22 to %22 + replayFile);%0A        String uri = %22https://www.hotslogs.com/UploadFile.aspx?Source=%22 + CLIENT_ID + %22&FileName=%22 + fileName;%0A
+File file = replayFile.getFile();%0A        try %7B%0A            boolean fileAlreadyUploaded = parseAndCheckStatus(file);%0A            if(fileAlreadyUploaded) %7B%0A                LOG.info(%22File already uploaded. No need to upload.%22);%0A                return Status.UPLOADED;%0A            %7D else %7B%0A                LOG.info(%22New replay. Uploading to HotSLogs.com.%22);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            LOG.warn(%22Could not check status for replay: %22 + file, e);%0A            return Status.EXCEPTION;%0A        %7D catch (MpqException e) %7B%0A            LOG.error(%22Could not parse replay. Skipping directly to upload.%22, e);%0A        %7D%0A%0A        String fileName = UUID.randomUUID() + %22.StormReplay%22;%0A        LOG.info(%22Assigning remote file name %22 + fileName + %22 to %22 + replayFile);%0A        String uri = BASE_URL + %22&FileName=%22 + fileName;%0A%0A        return uploadFileToHotSLogs(file, fileName, uri);%0A%0A    %7D%0A%0A    private Status uploadFileToHotSLogs(File file, String fileName, String uri) %7B
 %0A   
@@ -2617,36 +2617,20 @@
 leName, 
-replayFile.getF
+f
 ile
-()
 );%0A     
@@ -3743,21 +3743,436 @@
 ;%0A        %7D%0A
+    %7D%0A%0A    private boolean parseAndCheckStatus(File file) throws IOException %7B%0A        final StormParser stormParser = new StormParser(file);%0A        final String matchId = stormParser.getMatchId();%0A        LOG.info(%22Calculated matchId to be%22 + matchId);%0A        String uri = BASE_URL + %22&ReplayHash=%22 + matchId;%0A        String result = getHttpClient().simpleRequest(uri);%0A        return result.equals(%22DUPLICATE%22);
 %0A    %7D%0A%7D%0A

update hotslogsprovider to use new parser