package be.iminds.ilabt.jfed.experimenter_gui.ui;

import be.iminds.ilabt.jfed.experimenter_gui.debug.DebugGUIController;
import be.iminds.ilabt.jfed.experimenter_gui.debug.LogViewer;
import be.iminds.ilabt.jfed.ui.javafx.GuiceFXMLUtil;
import be.iminds.ilabt.jfed.ui.javafx.util.StageUtils;
import com.google.inject.Injector;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Provider;

/**
 * User: twalcari
 * Date: 12/16/2014
 * Time: 17:50
 */
public class AcceleratorService {
    private final Logger LOG = LoggerFactory.getLogger(AcceleratorService.class);

    private final GuiceFXMLUtil guiceFXMLUtil;
    private final Provider<LogViewer> logviewerProvider;
    //private final GraphvizGrapher graphvizGrapher;
    private final Injector injector;

    @Inject
    public AcceleratorService(GuiceFXMLUtil guiceFXMLUtil, Provider<LogViewer> logviewerProvider,
                              // GraphvizGrapher graphvizGrapher,
                              Injector injector) {
        this.guiceFXMLUtil = guiceFXMLUtil;
        this.logviewerProvider = logviewerProvider;
        //  this.graphvizGrapher = graphvizGrapher;
        this.injector = injector;
    }

    public void registerOnScene(Scene scene) {
        //add shortcut to debug-screen
        scene.getAccelerators().put(
                new KeyCodeCombination(KeyCode.F12, KeyCodeCombination.CONTROL_DOWN), this::showDebugGui);
//        scene.getAccelerators().put(
//                new KeyCodeCombination(KeyCode.F11, KeyCodeCombination.CONTROL_DOWN),
//                this::printInjectionGraph
//        );
        //add shortcut to debug-screen
        scene.getAccelerators().put(
                new KeyCodeCombination(KeyCode.F10, KeyCodeCombination.CONTROL_DOWN),
                () -> logviewerProvider.get().show()
        );

        scene.getAccelerators().put(
                new KeyCodeCombination(KeyCode.F4, KeyCodeCombination.CONTROL_DOWN, KeyCombination.ALT_DOWN),
                () -> {throw new RuntimeException("Just a test exception");}
        );

    }

    private Stage debugStage;

    public void showDebugGui() {

        if (debugStage == null) {
            GuiceFXMLUtil.Result<?, DebugGUIController> fxmlResult =
                    guiceFXMLUtil.createFromFXML(DebugGUIController.class);

            debugStage = new Stage();
            debugStage.setTitle("jFed DEBUGGING");
            debugStage.setResizable(false);
            debugStage.setScene(new Scene(fxmlResult.getRoot()));
            StageUtils.setJFedStageIcons(debugStage);
        }
        debugStage.show();
    }

//    private void printInjectionGraph(){
//        File file = new File("di-graph.dot");
//        try {
//            PrintWriter out = new PrintWriter(file, "UTF-8");
//
//            graphvizGrapher.setOut(out);
//            graphvizGrapher.setRankdir("TB");
//
//            graphvizGrapher.graph(injector);
//            LOG.info("Wrote Guice graph to {}", file.getAbsolutePath());
//        } catch (IOException e) {
//            LOG.error("Error while writing Guice graph to " + file.getAbsolutePath());
//        }
//    }
}
