package chatapp;

import chatapp.Controller.ChatApp;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {
    ChatApp chatapp;

    /**
     * Permet de lancer la fenetre principale de l'application
     * @param primaryStage notre fenetre principale
     * @throws Exception
     */
    @Override
    public void start(Stage primaryStage) throws Exception {
        Rectangle2D tailleEcran = Screen.getPrimary().getBounds();
        this.chatapp = ChatApp.getInstance("Null",1234,this);
        FXMLLoader fichier = new FXMLLoader(getClass().getResource("/fenetres/ConnexionScreen.fxml"));
        Scene scene1 = new Scene(fichier.load(),600,400);
        primaryStage.setScene(scene1);
        primaryStage.setTitle("ChatApp");
        primaryStage.setMinWidth(600);
        primaryStage.setMinHeight(400);
        //primaryStage.setMaxWidth(600);
        //primaryStage.setMaxHeight(400);
        /*primaryStage.setMaxWidth(tailleEcran.getWidth());
        primaryStage.setMaxHeight(tailleEcran.getHeight());*/
        primaryStage.show();
        primaryStage.centerOnScreen();
        chatapp.activerEcouteTCP();
        chatapp.activerEcouteUDP();

    }

    /**
     * <p> Handler associe a la fermeture de l'application</p>
     * @throws Exception
     */
    @Override
    public void stop() throws Exception {
        if(chatapp.isConnecte()){
            chatapp.deconnexion();
        }
        else {
            super.stop();
            Platform.exit();
            System.exit(0);
        }
    }

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