package com.insa.app.view;

import com.insa.app.Resources;
import com.insa.app.controller.Controller;
import com.insa.app.controller.GuiController;
import com.insa.app.network.Discoverer;
import com.insa.app.network.NetworkService;
import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Gui extends Application {

    private Controller controller;

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

    @Override
    public void stop() throws Exception {
        controller.closeDatabase();
        controller.disconnectFromPresenceServer();
        Logger.getAnonymousLogger().log(Level.INFO, "Clean quit");
        System.exit(0);
    }

    @Override
    public void start(Stage primaryStage) {
        try {

            List<String> parameters = getParameters().getUnnamed();
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("gui.fxml"));
            Scene scene = new Scene(loader.load(), 800, 500);

            // Controller initialization
            GuiController ctrl = loader.getController();
            controller = new Controller(ctrl, parameters);
            runServer(controller).start();
            ctrl.setup(controller);

            // Window initialization
            primaryStage.setTitle("NewClav3000");
            primaryStage.setScene(scene);
            primaryStage.show();
            if (!Resources.getPresenceServerIpFromArguments(parameters).isPresent()) {
                runDiscoverThread(controller).start();
            } else {
                controller.connectToPresenceServer(Resources.getPresenceServerIpFromArguments(parameters).get());
            }

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public Service<Void> runDiscoverThread(Controller controller) {
        Service<Void> service = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() {
                        Discoverer discoverer = new Discoverer(Resources.discoverPort,controller);
                        discoverer.run();
                        return null;
                    }
                };
            }
        };
        return service;
    }


    public Service<Void> runServer(Controller controller) {
        Service<Void> service = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() {
                        NetworkService app = new NetworkService(controller);
                        controller.setNetwork(app);
                        app.run();
                        return null;
                    }
                };
            }
        };
        return service;
    }
}
