/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package demo;

import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gcp.pubsub.core.PubSubTemplate;
import org.springframework.cloud.gcp.pubsub.integration.AckMode;
import org.springframework.cloud.gcp.pubsub.integration.inbound.PubSubInboundChannelAdapter;
import org.springframework.cloud.gcp.pubsub.integration.outbound.PubSubMessageHandler;
import org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage;
import org.springframework.cloud.gcp.pubsub.support.GcpPubSubHeaders;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.util.concurrent.ListenableFutureCallback;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

@SpringBootApplication
public class PubSubApplication {

  private static final Log LOGGER = LogFactory.getLog(PubSubApplication.class);
  private static final Random rand = new Random(2020);

  public static void main(String[] args) {
    SpringApplication.run(PubSubApplication.class, args);
  }

  // [START pubsub_spring_inbound_channel_adapter]
  // Create a message channel for messages arriving from the subscription `sub-one`.
  @Bean
  public MessageChannel inputMessageChannelForSubOne() {
    return new PublishSubscribeChannel();
  }

  // Create an inbound channel adapter to listen to the subscription `sub-one` and send
  // messages to the input message channel.
  @Bean
  public PubSubInboundChannelAdapter inboundChannelAdapter(
      @Qualifier("inputMessageChannelForSubOne") MessageChannel messageChannel,
      PubSubTemplate pubSubTemplate) {
    PubSubInboundChannelAdapter adapter =
        new PubSubInboundChannelAdapter(pubSubTemplate, "sub-one");
    adapter.setOutputChannel(messageChannel);
    adapter.setAckMode(AckMode.MANUAL);
    adapter.setPayloadType(String.class);
    return adapter;
  }

  // Define what happens to the messages arriving in the message channel.
  @ServiceActivator(inputChannel = "inputMessageChannelForSubOne")
  public void messageReceiver(
      String payload,
      @Header(GcpPubSubHeaders.ORIGINAL_MESSAGE) BasicAcknowledgeablePubsubMessage message) {
    LOGGER.info("Message arrived via an inbound channel adapter from sub-one! Payload: " + payload);
    message.ack();
  }
  // [END pubsub_spring_inbound_channel_adapter]

  // [START pubsub_spring_outbound_channel_adapter]
  // Create an outbound channel adapter to send messages from the input message channel to the
  // topic `topic-two`.
  @Bean
  @ServiceActivator(inputChannel = "inputMessageChannelForSubOne")
  public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
    PubSubMessageHandler adapter = new PubSubMessageHandler(pubsubTemplate, "topic-two");

    adapter.setPublishCallback(
        new ListenableFutureCallback<String>() {
          @Override
          public void onFailure(Throwable throwable) {
            LOGGER.info("There was an error sending the message.");
          }

          @Override
          public void onSuccess(String result) {
            LOGGER.info("Message was sent via the outbound channel adapter to topic-two!");
          }
        });
    return adapter;
  }
  // [END pubsub_spring_outbound_channel_adapter]

  // [START pubsub_spring_cloud_stream_input_binder]
  // Create an input binder to receive messages from `topic-two` using a Consumer bean.
  @Bean
  public Consumer<Message<String>> receiveMessageFromTopicTwo() {
    return message -> {
      LOGGER.info(
          "Message arrived via an input binder from topic-two! Payload: " + message.getPayload());
    };
  }
  // [END pubsub_spring_cloud_stream_input_binder]

  // [START pubsub_spring_cloud_stream_output_binder]
  // Create an output binder to send messages to `topic-one` using a Supplier bean.
  @Bean
  public Supplier<Flux<Message<String>>> sendMessageToTopicOne() {
    return () ->
        Flux.fromStream(
                // Generate a stream that sends a numbered message every 10 seconds.
                Stream.generate(
                    new Supplier<Message<String>>() {
                      @Override
                      public Message<String> get() {
                        try {
                          Thread.sleep(10000);
                        } finally {
                          Message<String> message =
                              MessageBuilder.withPayload("message-" + rand.nextInt(1000)).build();
                          LOGGER.info(
                              "Sending a message via the output binder to topic-one! Payload: "
                                  + message.getPayload());
                          return message;
                        }
                      }
                    }))
            .subscribeOn(Schedulers.elastic())
            .share();
  }
  // [END pubsub_spring_cloud_stream_output_binder]
}

@@ -5245,393 +5245,173 @@
 lux.
-fromStream(%0A                // Generate a stream that sends a numbered message every 10 seconds.%0A                Stream.generate(%0A                    new Supplier%3CMessage%3CString%3E%3E() %7B%0A                      @Override%0A                      public Message%3CString%3E get() %7B%0A                        try %7B%0A                          Thread.sleep(10000);%0A                        %7D finally %7B%0A   
+%3CMessage%3CString%3E%3Egenerate(sink -%3E %7B%0A          try %7B%0A            Thread.sleep(10000);%0A          %7D catch (InterruptedException e) %7B%0A            // stop sleep earlier.%0A
     
@@ -5408,35 +5408,35 @@
 lier.%0A          
-   
+%7D%0A%0A
           Messag
@@ -5455,30 +5455,16 @@
 ssage =%0A
-              
         
@@ -5550,32 +5550,16 @@
         
-       
-         
 LOGGER.i
@@ -5563,32 +5563,16 @@
 R.info(%0A
-                
         
@@ -5663,30 +5663,16 @@
         
-        
-      
 + messag
@@ -5702,38 +5702,26 @@
     
-                return 
+sink.next(
 message
+)
 ;%0A  
@@ -5730,86 +5730,10 @@
     
-                %7D%0A                      %7D%0A                    %7D))%0A            
+%7D)
 .sub
@@ -5766,29 +5766,8 @@
 c())
-%0A            .share()
 ;%0A  

change sending (#3863)