package com.commercetools.pspadapter.payone.domain.ctp;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import io.sphere.sdk.models.LocalizedString;
import io.sphere.sdk.models.TextInputHint;
import io.sphere.sdk.payments.commands.updateactions.AddInterfaceInteraction;
import io.sphere.sdk.queries.PagedQueryResult;
import io.sphere.sdk.types.DateTimeType;
import io.sphere.sdk.types.FieldDefinition;
import io.sphere.sdk.types.StringType;
import io.sphere.sdk.types.Type;
import io.sphere.sdk.types.TypeDraftBuilder;
import io.sphere.sdk.types.commands.TypeCreateCommand;
import io.sphere.sdk.types.queries.TypeQuery;

/**
 * @author Jan Wolter
 */
public class CustomTypeBuilder {
    public static final String PAYONE_INTERACTION_REQUEST = "PAYONE_INTERACTION_REQUEST";
    public static final String PAYONE_UNSUPPORTED_TRANSACTION = "PAYONE_UNSUPPORTED_TRANSACTION";
    public static final String TIMESTAMP_FIELD = "timestamp";
    public static final String TRANSACTION_ID_FIELD = "transactionId";
    public static final String MESSAGE_FIELD = "message";
    public static final boolean REQUIRED = true;

    final BlockingClient client;

    public CustomTypeBuilder(final BlockingClient client) {
        this.client = client;
    }

    public void run() {
        final PagedQueryResult<Type> result = client.complete(
                TypeQuery.of()
                        .withPredicates(m -> m.key().is(PAYONE_INTERACTION_REQUEST))
                        .withLimit(1));

        if (result.getTotal() == 0) {
            final FieldDefinition timestampField = FieldDefinition.of(
                    DateTimeType.of(),
                    TIMESTAMP_FIELD,
                    LocalizedString.ofEnglishLocale(TIMESTAMP_FIELD),
                    REQUIRED,
                    TextInputHint.SINGLE_LINE);

            final FieldDefinition transactionIdField = FieldDefinition.of(
                    StringType.of(),
                    TRANSACTION_ID_FIELD,
                    LocalizedString.ofEnglishLocale(TRANSACTION_ID_FIELD),
                    REQUIRED,
                    TextInputHint.SINGLE_LINE);

            // TODO jw: add other fields and types
//            final FieldDefinition transactionIdField = FieldDefinition.of(
//                    StringType.of(),
//                    TRANSACTION_ID_FIELD,
//                    LocalizedString.ofEnglishLocale(TRANSACTION_ID_FIELD),
//                    REQUIRED,
//                    TextInputHint.SINGLE_LINE);

            // TODO jw: cache id <-> name
            final Type complete = client.complete(TypeCreateCommand.of(TypeDraftBuilder.of(
                    PAYONE_INTERACTION_REQUEST,
                    LocalizedString.ofEnglishLocale(PAYONE_INTERACTION_REQUEST),
                    ImmutableSet.of(AddInterfaceInteraction.resourceTypeId()))
                    .fieldDefinitions(ImmutableList.of(timestampField, transactionIdField))
                    .build()));
        }
    }
}

@@ -2223,30 +2223,67 @@
 d types%0A
-//    
+%0A            // TODO jw: cache id %3C-%3E name%0A
         
 final Fi
@@ -2278,160 +2278,539 @@
     
+    
 final 
-FieldDefinition transactionIdField = FieldDefinition.of(%0A//                    StringType.of(),%0A//                    TRANSACTION_ID_FIELD,%0A//    
+Type payoneInteractionRequestType = createPayoneInteractionRequestType(timestampField, transactionIdField);%0A            final Type payoneInteractionUnsupportedTransactionType = createPayoneUnsupportedTransactionType(timestampField, transactionIdField);%0A        %7D%0A    %7D%0A%0A    private Type createPayoneInteractionRequestType(final FieldDefinition timestampField, final FieldDefinition transactionIdField) %7B%0A        return client.complete(TypeCreateCommand.of(TypeDraftBuilder.of(%0A                PAYONE_INTERACTION_REQUEST,%0A
     
@@ -2857,33 +2857,37 @@
 ale(
-TRANSACTION_ID_FIELD
+PAYONE_INTERACTION_REQUEST
 ),%0A
-//
     
@@ -2902,148 +2902,606 @@
     
-    REQUIRED,%0A//                    TextInputHint.SINGLE_LINE);%0A%0A            // TODO jw: cache id %3C-%3E name%0A            final Type complete =
+ImmutableSet.of(AddInterfaceInteraction.resourceTypeId()))%0A                .fieldDefinitions(ImmutableList.of(timestampField, transactionIdField))%0A                .build()));%0A    %7D%0A%0A    private Type createPayoneUnsupportedTransactionType(final FieldDefinition timestampField, final FieldDefinition transactionIdField) %7B%0A        final FieldDefinition messageField = FieldDefinition.of(%0A                StringType.of(),%0A                MESSAGE_FIELD,%0A                LocalizedString.ofEnglishLocale(MESSAGE_FIELD),%0A                REQUIRED,%0A                TextInputHint.SINGLE_LINE);%0A%0A        return
  cli
@@ -3575,44 +3575,40 @@
     
-    
 PAYONE_
-INTERACTION_REQUEST,%0A    
+UNSUPPORTED_TRANSACTION,%0A
     
@@ -3662,34 +3662,34 @@
 ONE_
-INTERACTION_REQUEST),%0A    
+UNSUPPORTED_TRANSACTION),%0A
     
@@ -3767,36 +3767,32 @@
                 
-    
 .fieldDefinition
@@ -3836,39 +3836,49 @@
 ansactionIdField
-))%0A    
+, messageField))%0A
                 
@@ -3889,26 +3889,16 @@
 ld()));%0A
-        %7D%0A
     %7D%0A%7D%0A

Add new custom interface interaction type for unsupported transactions.