package net.ranides.assira.events;

import lombok.Data;
import net.ranides.assira.junit.ManualTestUtils;
import net.ranides.assira.time.TimeMetric;
import org.junit.Ignore;
import org.junit.Test;

@Ignore("manual")
public class EventObserverTest {

    /**
     * Expected something like:
     *
     * NOW   100 first
     * NOW   100 second
     * NOW   200 third
     *
     * LAZY  400 third
     * NOW   600 fourth
     *
     * LAZY  800 fourth
     * LATER 1100 first
     * LATER 1100 second
     * LATER 1200 third
     * LATER 1600 fourth
     *
     */
    @Test
    public void lazy() {
        TimeMetric tm = new TimeMetric(false);

        EventObserver observer = EventObserver.newInstance(new Object() {
            @EventHandler()
            public void now(Event event) {
                System.out.printf("NOW   %d %s%n", tm.time(), event);
            }

            @EventHandler(router = EventHandler.LATER, delay = 1000)
            public void later(Event event) {
                System.out.printf("LATER %d %s%n", tm.time(), event);
            }

            @EventHandler(router = EventHandler.LAZY, delay = 200)
            public void lazy(Event event) {
                System.out.printf("%nLAZY  %d %s%n", tm.time(), event);
            }
        });

        System.out.println("creation: " + tm.time());

        observer.handleEvent(new MyEvent("first"));
        observer.handleEvent(new MyEvent("second"));
        ManualTestUtils.sleep(100);
        observer.handleEvent(new MyEvent("third"));
        ManualTestUtils.sleep(400);
        observer.handleEvent(new MyEvent("fourth"));

        ManualTestUtils.sleep(3000);
    }

    @Data
    public static class MyEvent implements Event {
        private final String tag;

        @Override
        public String toString() {
            return tag;
        }
    }
}