package com.example.bricklog.view;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
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.layout.VBox;
// import javafx.scene.layout.StackPane;

public class MainLayout extends BorderPane {

    private VBox menu; // makes menu accessible for updates

    public MainLayout() {

        // Title & Coppyright
        Label title = new Label("BRICK LOG Client");
        title.setStyle("-fx-font-size: 24px; -fx-font-weight: bold;");

        Label copyright = new Label("built by Bryce © 2024");
        copyright.setStyle("-fx-font-size: 14px;");

        VBox titleBox = new VBox(title, copyright);
        titleBox.setAlignment(Pos.CENTER);
        titleBox.setPadding(new Insets(10));

        // Set title at the top
        VBox topContainer = new VBox(titleBox);
        this.setTop(topContainer);

        menu = new VBox();
        menu.setSpacing(10);
        menu.setPadding(new Insets(10));
        menu.setStyle("-fx-background-color: #f0f0f0;"); // Optional: Background color for the menu

        // Search Bar
        TextField searchField = new TextField();
        searchField.setPromptText("Search...");

        // Set up the menu
        menu = new VBox();
        menu.setSpacing(10);
        menu.setPadding(new Insets(10));
        menu.setStyle("-fx-background-color: #f0f0f0;"); // Optional: Background color for the menu

        // Add the Gallery menu item
        Label galleryLabel = new Label("Gallery");
        galleryLabel.setStyle("-fx-font-weight: bold; cursor: hand;"); // Make it look interactive
        galleryLabel.setOnMouseClicked(event -> switchToGalleryLayout()); // Switch layout on click

        Label settingsLabel = new Label("Settings");
        settingsLabel.setStyle("-fx-font-weight: bold; cursor: hand;");
        settingsLabel.setOnMouseClicked(event -> switchToSettingsLayout());

        // Add menu items
        menu.getChildren().addAll(new Label("Menu"), galleryLabel, settingsLabel);
        this.setLeft(menu); // Set the menu on the left side

        // Set up the '+' button
        Button addButton = new Button("+");
        addButton.setStyle("-fx-font-size: 24px; -fx-background-color: lightblue;");

        // Set up the action for the '+' button to show the add dialog
        addButton.setOnAction(e -> showAddDialog());

        // Create an HBox to hold the button and align it to the bottom right
        HBox buttonContainer = new HBox(addButton);
        buttonContainer.setAlignment(Pos.BOTTOM_RIGHT);
        buttonContainer.setPadding(new Insets(10)); // Optional padding

        // Add the buttonContainer to the bottom of the layout
        this.setBottom(buttonContainer);

        // Optionally, set the center to another pane or leave it empty
        // this.setCenter(someOtherPane);
    }

    private void showAddDialog() {
        Dialog<ButtonType> dialog = new Dialog<>();
        dialog.setTitle("Add Build");
        
        // Create the dialog layout
        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20));

        TextField nameField = new TextField();
        nameField.setPromptText("Build Name");
        TextField builderField = new TextField();
        builderField.setPromptText("Builder Name");
        // Add more fields as needed...

        grid.add(new Label("Name:"), 0, 0);
        grid.add(nameField, 1, 0);
        grid.add(new Label("Builder:"), 0, 1);
        grid.add(builderField, 1, 1);
        // Add more fields to the grid as needed...

        dialog.getDialogPane().setContent(grid);
        
        // Add buttons for adding and canceling
        dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);

        // Show dialog and handle button events
        dialog.setResultConverter(button -> {
            if (button == ButtonType.OK) {
                // Insert data into the database and handle image upload
                String name = nameField.getText();
                String builder = builderField.getText();
                // Handle database insert...
                System.out.println("Name: " + name + ", Builder: " + builder); // Debugging line
            }
            return null;
        });

        dialog.showAndWait();
    }
      // This method is essential for switching to the GalleryLayout
      private void switchToGalleryLayout() {
        // Create an instance of GalleryLayout
        GalleryLayout galleryLayout = new GalleryLayout();
        
        // Set the center of the MainLayout to the GalleryLayout
        this.setCenter(galleryLayout);
    }
    private void switchToSettingsLayout() {
        SettingsWindow settingsLayout = new SettingsWindow();
        this.setCenter(settingsLayout);
    }
}
