import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.control.*;
import javafx.geometry.Insets;
import java.time.LocalDate;
import java.util.List;

public class SignupApp extends Application 
{
    private TextField nameField            = new TextField();
    private TextArea descriptionArea       = new TextArea();
    private ToggleGroup genderToggle       = new ToggleGroup();
    private RadioButton maleButton         = new RadioButton("Male");
    private RadioButton femaleButton       = new RadioButton("Female");
    private RadioButton otherButton        = new RadioButton("Other");
    private CheckBox notifyCheckBox        = new CheckBox("Receive notifications");
    private ComboBox<String> countryCombo  = new ComboBox<>();
    private DatePicker dobPicker           = new DatePicker();
    private ListView<String> genreListView = new ListView<>();

    @Override
    public void start(Stage stage) 
    {
        stage.setTitle("Social Media Signup");
        VBox vbox = new VBox(10);
        
        // name
        HBox  nameBox   = new HBox(10);        
        Label nameLabel = new Label("Name: ");
        nameLabel.setMinWidth(75);
        nameField.setMinWidth(175);
        nameBox.getChildren().add(nameLabel);
        nameBox.getChildren().add(nameField);
        vbox.getChildren().add(nameBox);
    
        // date of birth (DOB)
        HBox  dobBox   = new HBox(10);
        Label dobLabel = new Label("Date of Birth: ");
        dobLabel.setMinWidth(75);
        dobBox.getChildren().add(dobLabel);
        dobBox.getChildren().add(dobPicker);
        vbox.getChildren().add(dobBox);

        // gender
        HBox  genderBox   = new HBox(10);        
        Label genderLabel = new Label("Gender: ");
        genderLabel.setMinWidth(75);        
        maleButton.setToggleGroup(genderToggle);
        femaleButton.setToggleGroup(genderToggle);
        otherButton.setToggleGroup(genderToggle);
        maleButton.setSelected(true);
        genderBox.getChildren().add(genderLabel);
        genderBox.getChildren().add(maleButton);
        genderBox.getChildren().add(femaleButton);
        genderBox.getChildren().add(otherButton);
        vbox.getChildren().add(genderBox);

        // country
        HBox  countryBox   = new HBox(10);        
        Label countryLabel = new Label("Country: ");
        countryLabel.setMinWidth(75);        
        countryCombo.setPromptText("Select Country");
        String[] countries = {"United States", "Canada", "Mexico"};
        for (String country : countries) {
            countryCombo.getItems().add(country);            
        }
        countryCombo.setMinWidth(175);        
        countryBox.getChildren().add(countryLabel);
        countryBox.getChildren().add(countryCombo);
        vbox.getChildren().add(countryBox);

        // movie genres
        Label movieLabel = new Label("Favorite Movie Genres: ");
        genreListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        String[] genres = {"Action", "Comedy", "Sci-Fi", "Horror", "Drama"};
        for (String genre : genres) {
            genreListView.getItems().add(genre);            
        }
        genreListView.setPrefHeight(genres.length * 24);
        genreListView.setMaxWidth(260);
        vbox.getChildren().add(movieLabel);
        vbox.getChildren().add(genreListView);

        // profile description
        Label descriptionLabel = new Label("Profile Description: ");   
        descriptionArea.setPrefColumnCount(80);
        descriptionArea.setPrefRowCount(4);
        descriptionArea.setWrapText(true);
        ScrollPane descriptionScroll = new ScrollPane(descriptionArea);
        vbox.getChildren().add(descriptionLabel);
        vbox.getChildren().add(descriptionScroll);

        // notification
        vbox.getChildren().add(notifyCheckBox);

        // button
        Button signUpButton = new Button("Sign Up");        
        signUpButton.setOnAction(event -> signUpButtonClicked());           //event listener
        vbox.getChildren().add(signUpButton);

        Insets padding = new Insets(10, 10, 0, 10); 
        vbox.setPadding(padding);
     
        Scene scene = new Scene(vbox, 800, 510);
        stage.setScene(scene);
        stage.show();
    }

    public void signUpButtonClicked() 
    {
        // get entered/selected data
        String name                = nameField.getText();
        LocalDate dob              = dobPicker.getValue(); 
        RadioButton selectedGender = (RadioButton) genderToggle.getSelectedToggle();
        String gender              = selectedGender.getText();
        boolean notification       = notifyCheckBox.isSelected(); 
        String country             = countryCombo.getSelectionModel().getSelectedItem();
        String profile             = descriptionArea.getText();
        String allGenres = "";
        List<String> genres        = genreListView.getSelectionModel().getSelectedItems();  
        for(String genre : genres) {
            allGenres += genre + " ";
        }                
        String notify = (notification == true)? "on" : "false";

        // build output string
        String outputString = 
            "Name: "                  + name      + "\n" +
            "DOB: "                   + dob       + "\n" +
            "Gender: "                + gender    + "\n" +
            "Country: "               + country   + "\n" +
            "Favorite movie genres: " + allGenres + "\n" +
            "Profile description: "   + profile   + "\n" +
            "Notifications: "         + notify    + "\n";

        // display output string
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Signup Info");
        alert.setHeaderText(null);
        alert.setContentText(outputString);        
        alert.showAndWait();
    }

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