package loginscreendemo;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class LoginDemo extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage window) throws Exception {
    	// Welcome message
    	Label lbWelcome = new Label("Welcome to the application!");
    	
        // Login form
    	Label lbUserName = new Label("Username:");
    	lbUserName.setPrefWidth(100);
        TextField tbUserName = new TextField();
        Label lbPassword = new Label("Password:");
        lbPassword.setPrefWidth(100);
        TextField tbPassword = new TextField();
        
        // Error message
        Label lbError = new Label();
        
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.add(lbWelcome, 0, 0, 2, 1);
        grid.add(lbUserName, 0, 1);
        grid.add(tbUserName, 1, 1);
        grid.add(lbPassword, 0, 2);
        grid.add(tbPassword, 1, 2);
        grid.add(lbError, 0, 3, 2, 1);

        // login button
        Button button = new Button("Login");
        button.setOnAction( e -> {
        	String username = tbUserName.getText();
        	String password = tbPassword.getText();
        	if (username != null && username.equals("admin") &&
        			password != null && password.equals("123")) {
            	lbError.setTextFill(Color.BLACK);
        		lbError.setText("Login sucessful!");
        	} else {
            	lbError.setTextFill(Color.RED);
        		lbError.setText("*** Login error! ***");
        	}
        });
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn.getChildren().add(button);
        
        //Layout
        BorderPane layout = new BorderPane();
        layout.setPadding(new Insets(25, 50, 25, 50));
        layout.setCenter(grid);
        layout.setBottom(hbBtn);

        window.setTitle("Login");
        window.setScene(new Scene(layout, 300, 250));
        window.show();
    }
}