package com.github.davidmoten.aws.maven;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;
import java.util.Optional;

import com.amazonaws.services.lambda.model.CreateAliasRequest;
import com.amazonaws.services.lambda.model.CreateAliasResult;
import com.amazonaws.services.lambda.model.UpdateFunctionCodeResult;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.logging.Log;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.model.UpdateFunctionCodeRequest;

class LambdaDeployer {

    private final Log log;

    LambdaDeployer(Log log) {
        this.log = log;
    }

    void deploy(AwsKeyPair keyPair, String region, String zipFilename, String functionName, String functionAlias, Proxy proxy) {
        long t = System.currentTimeMillis();
        final AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider(
                new BasicAWSCredentials(keyPair.key, keyPair.secret));

        AWSLambda lambda = AWSLambdaClientBuilder.standard().withCredentials(credentials)
                .withClientConfiguration(Util.createConfiguration(proxy)).withRegion(region).build();

        byte[] bytes;
        try {
            bytes = IOUtils.toByteArray(new FileInputStream(zipFilename));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        DecimalFormat df = new DecimalFormat("0.000");
        log.info("deploying " + zipFilename + ", length=" + df.format(bytes.length / 1024.0 / 1024.0)
                + "MB, to functionName=" + functionName);
        UpdateFunctionCodeResult updateFunctionCodeResult = lambda.updateFunctionCode( //
                new UpdateFunctionCodeRequest() //
                        .withFunctionName(functionName) //
                        .withPublish(true) //
                        .withZipFile(ByteBuffer.wrap(bytes)));
        log.info("deployed in " + (System.currentTimeMillis() - t) + "ms");
        Optional<String> optionalFunctionAlias = Optional.ofNullable(functionAlias);
        if (optionalFunctionAlias.isPresent()) {
            CreateAliasResult createAliasResult = lambda.createAlias(
                new CreateAliasRequest()
                    .withFunctionVersion(updateFunctionCodeResult.getVersion())
                    .withFunctionName(functionName)
                    .withName(optionalFunctionAlias.get()));
            log.info("created alias=" + optionalFunctionAlias.get() + " to functionName=" + functionName + " for version=" + updateFunctionCodeResult.getVersion());
        }
    }

}

@@ -2358,24 +2358,287 @@
 resent()) %7B%0A
+            // ailas only likes underscores, have to strip out other characters if they are present%0A            String sanitisedFunctionalAlias = optionalFunctionAlias.get()%0A                    .replaceAll(%22%5B-%5C%5C.%5D%22, %22_%22)%0A                    .replaceAll(%22:%22, %22%22);%0A
             
@@ -2695,16 +2695,16 @@
 eAlias(%0A
-
         
@@ -2898,32 +2898,33 @@
 ithName(
-optional
+sanitised
 Function
 Alias.ge
@@ -2915,27 +2915,23 @@
 Function
+al
 Alias
-.get()
 ));%0A    
@@ -2966,32 +2966,33 @@
 ias=%22 + 
-optional
+sanitised
 Function
 Alias.ge
@@ -2983,27 +2983,23 @@
 Function
+al
 Alias
-.get()
  + %22 to 

Fix non-underscore aliases