package io.paradoxical.cassieq.discoverable.auth;

import com.godaddy.logging.Logger;
import io.paradoxical.cassieq.model.QueueName;
import io.paradoxical.cassieq.model.accounts.AccountName;
import io.paradoxical.cassieq.model.auth.AuthorizationLevel;
import io.paradoxical.cassieq.model.auth.SignatureGenerator;
import io.paradoxical.cassieq.model.auth.SignedUrlSignatureGenerator;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.Value;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Instant;

import javax.validation.constraints.NotNull;
import java.util.EnumSet;
import java.util.Optional;

import static com.godaddy.logging.LoggerFactory.getLogger;

/**
 * Represents the parsed signed URL pamaters from a request
 */
@EqualsAndHashCode(callSuper = false)
@Value
@Builder
public class SignedUrlParameters extends SignedParametersBase implements RequestParameters {

    private static final Logger logger = getLogger(SignedUrlParameters.class);

    @NonNull
    @NotNull
    private final AccountName accountName;

    @NonNull
    @NotNull
    private final String querySignature;

    @NonNull
    @NotNull
    private final EnumSet<AuthorizationLevel> authorizationLevels;

    @NonNull
    @NotNull
    private final Optional<DateTime> startDateTime;

    @NonNull
    @NotNull
    private final Optional<DateTime> endDateTime;

    @NonNull
    @NotNull
    private final Optional<QueueName> queueName;

    @Override
    public boolean verify(final VerificationContext context) {
        final boolean verified = super.verify(context);

        if (verified) {
            final Instant now = context.getClock().now();
            return startDateTime.map(now::isAfter).orElse(true) &&
                   endDateTime.map(now::isBefore).orElse(true) &&
                   (!queueName.isPresent() || // no queue restriction
                    context.getQueueName().equals(queueName));
        }

        return false;
    }

    @Override
    protected SignatureGenerator getSignatureGenerator() {
        return new SignedUrlSignatureGenerator(accountName, authorizationLevels, startDateTime, endDateTime, queueName);
    }

    @Override
    protected String getProvidedSignature() {
        return querySignature;
    }
}


@@ -511,43 +511,8 @@
 me;%0A
-import org.joda.time.DateTimeZone;%0A
 impo
@@ -1603,20 +1603,23 @@
     
-if (
+return 
 verified
 ) %7B%0A
@@ -1614,19 +1614,19 @@
 verified
-) %7B
+ &&
 %0A       
@@ -1634,207 +1634,180 @@
     
-final Instant now = context.getClock().now();%0A            return startDateTime.map(now::isAfter).orElse(true) &&%0A  
+   requestInAllowedTimeFrame(context) &&%0A               queueAllowed(context);%0A%0A
     
+%7D%0A%0A
     
-         endDateTime.map(now::isBefore).orElse(true) &&%0A                   (
+private boolean queueAllowed(final VerificationContext context) %7B%0A        return 
 !que
@@ -1852,32 +1852,16 @@
 riction%0A
-                
     cont
@@ -1900,42 +1900,276 @@
 ame)
-);%0A        %7D%0A%0A        return false
+;%0A    %7D%0A%0A    private boolean requestInAllowedTimeFrame(final VerificationContext context) %7B%0A        final Instant now = context.getClock().now();%0A%0A        return startDateTime.map(now::isAfter).orElse(true) &&%0A               endDateTime.map(now::isBefore).orElse(true)
 ;%0A  

clean up validation