package com.heroku.myapp.myorchestrator.consumers.common;

import com.heroku.myapp.commons.config.enumerate.Kind;
import com.heroku.myapp.commons.config.enumerate.KindOptions;
import com.heroku.myapp.commons.consumers.QueueConsumer;
import com.heroku.myapp.commons.util.actions.DiffUtil;
import com.heroku.myapp.commons.util.actions.MasterUtil;
import com.heroku.myapp.commons.util.consumers.QueueConsumerUtil;
import static com.heroku.myapp.commons.util.consumers.QueueMessage.messageKindIs;
import org.apache.camel.Exchange;
import org.apache.camel.model.ChoiceDefinition;
import org.springframework.stereotype.Component;

@Component
public class CompletionQueueConsumer extends QueueConsumer {

    @Override
    public void configure() {
        ChoiceDefinition choice = from(util().completion().ironmqConsumeUri())
                .routeId(util().id())
                .filter((Exchange exchange) -> {
                    return new MasterUtil(exchange).toCompleteLogic(this);
                })
                .filter((Exchange exchange)
                        -> new MasterUtil(exchange).snapshotSaveToMaster(this))
                .filter((Exchange exchange)
                        -> new DiffUtil(exchange).enableDiff(this))
                .choice()
                .when(MasterUtil.isNotFilled(this))
                .process(util().requestSnapshotProcess())
                .otherwise();

        for (Kind k : Kind.values()) {
            if (k.optionIsEnable()) {
                choice.when(messageKindIs(k)).to("log:" + k.expression());
                if (k.isEnable(KindOptions.affect)) {
                    k.affects().stream().forEach((affect) -> {
                        choice.setBody().constant(affect.preMessage())
                                .to(new QueueConsumerUtil(affect).snapshot()
                                        .ironmqPostUri());
                    });
                }
            }
        }
    }
}

@@ -400,24 +400,85 @@
 nsumerUtil;%0A
+import com.heroku.myapp.commons.util.consumers.QueueMessage;%0A
 import stati
@@ -547,16 +547,43 @@
 KindIs;%0A
+import java.util.Optional;%0A
 import o
@@ -778,17 +778,21 @@
 sumer %7B%0A
+    
 %0A
-
     @Ove
@@ -852,17 +852,25 @@
 inition 
-c
+topLevelC
 hoice = 
@@ -951,33 +951,156 @@
 ().id())
-%0A                
+.choice();%0A        setAlwaysAffects(topLevelChoice);%0A        ChoiceDefinition secondLevelChoice%0A                = topLevelChoice.otherwise()
 .filter(
@@ -1218,16 +1218,58 @@
      %7D)%0A
+                .id(%22filter:to_complete%22)%0A
         
@@ -1376,32 +1376,86 @@
 ToMaster(this))%0A
+                .id(%22filter:snapshot_save_to_master%22)%0A
                 
@@ -1542,32 +1542,74 @@
 ableDiff(this))%0A
+                .id(%22filter:enable_diff%22)%0A
                 
@@ -1617,16 +1617,17 @@
 choice()
+;
 %0A       
@@ -1631,50 +1631,161 @@
     
-        .when(MasterUtil.isNotFilled(this)
+ChoiceDefinition secondLevelChoiceOtherwise%0A                = secondLevelChoice.when(MasterUtil.isNotFilled(this))%0A                .id(%22when:isNotFilled%22
 )%0A  
@@ -1868,18 +1868,35 @@
 erwise()
-;%0A
+.choice();%0A        
 %0A       
@@ -1981,21 +1981,41 @@
         
-choic
+secondLevelChoiceOtherwis
 e.when(m
@@ -2201,17 +2201,1107 @@
         
-c
+secondLevelChoiceOtherwise.setBody()%0A                                .constant(affect.preMessage())%0A                                .to(new QueueConsumerUtil(affect).snapshot()%0A                                        .ironmqPostUri());%0A                    %7D);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    %0A    public void setAlwaysAffects(ChoiceDefinition choice) %7B%0A        ChoiceDefinition nestedChoice = choice.when((Exchange exchange)%0A                -%3E !Optional.ofNullable(%0A                        (Boolean) new QueueMessage(exchange)%0A                        .map().get(%22changed%22)).orElse(true))%0A                .id(%22when:changedIsFalse%22)%0A                .choice();%0A        for (Kind k : Kind.values()) %7B%0A            if (k.optionIsEnable()) %7B%0A                if (k.isEnable(KindOptions.always_affect)) %7B%0A                    nestedChoice.when(messageKindIs(k))%0A                            .id(%22when:is_%22 + k.expression())%0A                            .to(%22log:%22 + k.expression());%0A                    k.alwaysAffects().stream().forEach((affect) -%3E %7B%0A                        nestedC
 hoice.se
@@ -3522,32 +3522,32 @@
 %7D%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -3536,20 +3536,48 @@
 %7D%0A        %7D%0A
+        choice.endChoice();%0A
     %7D%0A%7D%0A

modify CompletionQueueConsumer to show route detail