
package chatapp.View;

        import java.io.IOException;
        import java.net.URL;
        import java.util.ResourceBundle;

        import chatapp.Controller.ChatApp;
        import javafx.event.ActionEvent;
        import javafx.fxml.FXML;
        import javafx.fxml.FXMLLoader;
        import javafx.scene.Parent;
        import javafx.scene.Scene;
        import javafx.scene.control.Alert;
        import javafx.scene.control.Button;
        import javafx.scene.control.TextField;
        import javafx.scene.text.Text;
        import javafx.stage.Stage;
/**
 * <p>
 *     Classe representant l'interface graphique permettant de changer de pseudo
 * </p>
 */
public class ChangementPseudo {

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="validationButton"
    private Button validationButton; // Value injected by FXMLLoader

    @FXML // fx:id="pseudonyme"
    private TextField pseudonyme; // Value injected by FXMLLoader

    @FXML // fx:id="pseudonyme1"
    private Text pseudonyme1; // Value injected by FXMLLoader

    @FXML // fx:id="MenuButton"
    private Button MenuButton; // Value injected by FXMLLoader

    private ChatApp chatApp;
    @FXML
    void nouveauPseudo(ActionEvent event) {

    }


    @FXML
    /**
     * <p> Handler permettant de retourner au menu principal sur pression du MenuButton</p>
     * @param event L'utilisateur vient d'appuyer sur le bouton Retour au menu
     */
    void retourMenu(ActionEvent event) {
        System.out.println("Retour au menu principal");
        Stage stage = (Stage) MenuButton.getScene().getWindow();
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("/fenetres/View_Menu.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    /**
     * <p> Handler permettant de lancer la verrification de l'unicite du ppseudo sur pression du validtionButton</p>
     * @param event L'utilisateur vient d'appuyer sur le bouton Valider pseudo
     */
    void validerPseudo(ActionEvent event) {
        boolean pseudoOK = false;
        try {
            pseudoOK = chatApp.modifierPseudo(pseudonyme.getText());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(pseudoOK){
            this.retourMenu(null);
        }
        else{
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("Changement pseudo");
            // Header Text: null
            alert.setHeaderText(null);
            alert.setContentText("Echec de Connexion: le pseudo "+pseudonyme.getText()+" est deja pris");
            alert.showAndWait();
        }
    }

    /**
     * <p> Initialization de la classe ChangementPseudo et permet d'initialiser les parametres</p>
     */
    @FXML // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
       chatApp = ChatApp.getInstance();
       pseudonyme1.setText(chatApp.getMe().getPseudo());
       this.validationButton.setDefaultButton(true);
       //System.out.println(this.validationButton.isDefaultButton());
    }
}