package com.easternedgerobotics.rov;

import com.easternedgerobotics.rov.event.EventPublisher;
import com.easternedgerobotics.rov.event.UdpEventPublisher;
import com.easternedgerobotics.rov.fx.MainView;
import com.easternedgerobotics.rov.fx.ThrusterPowerSlidersView;
import com.easternedgerobotics.rov.fx.ViewLoader;
import com.easternedgerobotics.rov.io.Joystick;
import com.easternedgerobotics.rov.io.Joysticks;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import javafx.application.Application;
import javafx.stage.Stage;
import org.pmw.tinylog.Logger;

public final class Topside extends Application {
    private EventPublisher eventPublisher;

    private Injector injector;

    @Override
    public void init() {
        eventPublisher = new UdpEventPublisher("192.168.88.255");
        injector = Guice.createInjector(
            binder -> binder.bind(EventPublisher.class).toProvider(() -> eventPublisher).in(Singleton.class),
            Binder::requireAtInjectOnConstructors);

        Joysticks.logitechExtreme3dPro().flatMap(Joystick::axes)
            .subscribe(eventPublisher::emit, Logger::error);

        Logger.info("Initialised");
    }

    @Override
    public void start(final Stage stage) {
        Logger.info("Starting");
        final ViewLoader viewLoader = injector.getInstance(ViewLoader.class);

        viewLoader.loadIntoStage(MainView.class, stage);
        stage.setTitle("Control Software");
        stage.show();

        final Stage thrusterStage = viewLoader.load(ThrusterPowerSlidersView.class);
        thrusterStage.setTitle("Thruster Power");
        thrusterStage.initOwner(stage);
        thrusterStage.show();

        Logger.info("Started");
    }

    @Override
    public void stop() {
        Logger.info("Stopping");
        eventPublisher.stop();
        Logger.info("Stopped");
    }

    public static void main(final String[] args) {
        launch(args);
    }
}

@@ -31,16 +31,83 @@
 s.rov;%0A%0A
+import com.easternedgerobotics.rov.control.ExponentialMotionScale;%0A
 import c
@@ -1139,24 +1139,99 @@
 tructors);%0A%0A
+        final ExponentialMotionScale scale = new ExponentialMotionScale();%0A
         Joys
@@ -1283,16 +1283,47 @@
 ::axes)%0A
+            .map(scale::apply)%0A
         

Apply exponential motion scale to joystick input