/*
 * Copyright (c) 2016-2021 Flux Capacitor.
 *
 * 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 io.fluxcapacitor.javaclient.givenwhenthen;

import io.fluxcapacitor.javaclient.FluxCapacitor;
import io.fluxcapacitor.javaclient.configuration.client.WebSocketClient;
import io.fluxcapacitor.javaclient.scheduling.Periodic;
import io.fluxcapacitor.javaclient.test.TestFixture;
import io.fluxcapacitor.javaclient.test.spring.FluxCapacitorTestConfig;
import io.fluxcapacitor.javaclient.tracking.handling.HandleCommand;
import io.fluxcapacitor.javaclient.tracking.handling.HandleEvent;
import io.fluxcapacitor.javaclient.tracking.handling.HandleSchedule;
import lombok.SneakyThrows;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.hamcrest.Matchers.isA;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;

@Disabled("Leave here to enable tests against real server")
@ExtendWith(SpringExtension.class)
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
@ContextConfiguration(classes = {FluxCapacitorTestConfig.class, GivenWhenThenSpringCustomClientTest.FooConfig.class, GivenWhenThenSpringCustomClientTest.BarConfig.class})
@Slf4j
class GivenWhenThenSpringCustomClientTest {

    @Autowired
    private TestFixture testFixture;

    @Test
    void testFoo() {
        testFixture.givenNoPriorActivity().whenCommand("command1").expectEvents("event");
    }

    @Test
    void testBar() {
        testFixture.givenNoPriorActivity().whenCommand("command1").expectCommands("command2");
    }

    @Test
    void testAutomaticPeriodicSchedule() {
        testFixture.givenNoPriorActivity().when(GivenWhenThenSpringCustomClientTest::sleepAWhile)
                .expectSchedules(isA(PeriodicSchedule.class));
    }

    @Value
    @Periodic(value = 200, scheduleId = "test")
    static class PeriodicSchedule {
    }

    @Configuration
    static class FooConfig {
        @Bean
        public WebSocketClient.Properties webSocketClientProperties() {
            return WebSocketClient.Properties.builder()
                    .serviceBaseUrl("http://localhost:8081")
                    .name("GivenWhenThenSpringCustomClientTest")
                    .build();
        }

        @Bean
        public FooHandler fooHandler() {
            return new FooHandler();
        }
    }

    private static class FooHandler {

        @HandleSchedule
        public void handle(PeriodicSchedule schedule) {
            FluxCapacitor.publishEvent("triggered");
        }

        @HandleCommand
        public void handle(String command) {
            FluxCapacitor.publishEvent("event");
        }

    }

    @Configuration
    static class BarConfig {
        @Bean
        public BarHandler barHandler() {
            return new BarHandler();
        }
    }

    static class BarHandler {
        @HandleEvent
        public void handle(String event) {
            FluxCapacitor.sendAndForgetCommand("command2");
        }
    }

    @SneakyThrows
    private static void sleepAWhile() {
        Thread.sleep(500);
    }

}
@@ -775,64 +775,8 @@
 nt;%0A
-import io.fluxcapacitor.javaclient.scheduling.Periodic;%0A
 impo
@@ -1034,77 +1034,8 @@
 nt;%0A
-import io.fluxcapacitor.javaclient.tracking.handling.HandleSchedule;%0A
 impo
@@ -1618,41 +1618,48 @@
 ort 
-static org.hamcrest.Matchers.isA;
+java.util.concurrent.CompletableFuture;%0A
 %0Aimp
@@ -2268,40 +2268,57 @@
 and(
-%22command1%22).expectEvents(%22event%22
+new DoSomething()).expectEvents(new DoSomething()
 );%0A 
@@ -2413,370 +2413,65 @@
 and(
-%22command1%22).expectCommands(%22command2%22);%0A    %7D%0A%0A    @Test%0A    void testAutomaticPeriodicSchedule() %7B%0A        testFixture.givenNoPriorActivity().when(GivenWhenThenSpringCustomClientTest::sleepAWhile)%0A                .expectSchedules(isA(PeriodicSchedule.class));%0A    %7D%0A%0A    @Value%0A    @Periodic(value = 200, scheduleId = %22test%22)%0A    static class PeriodicSchedule %7B
+new DoSomething()).expectCommands(new DoSomethingElse());
 %0A   
@@ -2721,17 +2721,62 @@
 host:808
-1
+0%22)%0A                    .projectId(%22clienttest
 %22)%0A     
@@ -3023,17 +3023,16 @@
 ndler %7B%0A
-%0A
         
@@ -3038,24 +3038,23 @@
  @Handle
-Schedule
+Command
 %0A       
@@ -3077,33 +3077,27 @@
 dle(
-PeriodicSchedule schedule
+DoSomething command
 ) %7B%0A
@@ -3139,19 +3139,15 @@
 ent(
-%22triggered%22
+command
 );%0A 
@@ -3198,84 +3198,223 @@
 lic 
-void handle(String command) %7B%0A            FluxCapacitor.publishEvent(%22event%22
+CompletableFuture%3C?%3E handle(SlowCommand command) %7B%0A            return CompletableFuture.runAsync(this::sleepAWhile);%0A        %7D%0A%0A        @SneakyThrows%0A        private void sleepAWhile() %7B%0A            Thread.sleep(500
 );%0A 
@@ -3418,25 +3418,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    @
@@ -3668,11 +3668,16 @@
 dle(
-Str
+DoSometh
 ing 
@@ -3736,30 +3736,216 @@
 and(
-%22command2%22);%0A        %7D
+new DoSomethingElse());%0A        %7D%0A    %7D%0A%0A    @Value%0A    private static class DoSomething %7B%0A    %7D%0A%0A    @Value%0A    private static class DoSomethingElse %7B%0A    %7D%0A%0A    @Value%0A    private static class SlowCommand %7B
 %0A   

Fix custom client test