package com.example.bricklog.view;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import static javafx.scene.paint.Color.WHITE;
import javafx.scene.text.Font;

public class SettingsWindow extends BorderPane {

    private final StackPane contentPane;
    private final VBox generalSettings;
    private final VBox serverSettings;
    private final Label connectionStatus;

    public SettingsWindow() {
        // Menu on the left side for navigation
        VBox menu = new VBox();
        menu.setSpacing(10);
        menu.setPadding(new Insets(10));
        menu.setStyle("-fx-background-color: #f0f0f0;");
        
        // Labels for the menu
        Label generalLabel = new Label("General");
        generalLabel.setStyle("-fx-font-weight: bold; cursor: hand;");
        generalLabel.setOnMouseClicked(event -> showGeneralSettings());

        Label serverLabel = new Label("Server");
        serverLabel.setStyle("-fx-font-weight: bold; cursor: hand;");
        serverLabel.setOnMouseClicked(event -> showServerSettings());

        menu.getChildren().addAll(generalLabel, serverLabel);
        this.setLeft(menu);

        // StackPane to hold different settings panels
        contentPane = new StackPane();
        this.setCenter(contentPane);

        // Define General Settings panel
        generalSettings = new VBox(10);
        generalSettings.setPadding(new Insets(15));
        generalSettings.setAlignment(Pos.TOP_LEFT);
        Label themeLabel = new Label("Theme:");
        CheckBox darkTheme = new CheckBox("Dark Theme");

        Label layoutLabel = new Label("Layout:");
        CheckBox compactLayout = new CheckBox("Compact Layout");

        generalSettings.getChildren().addAll(themeLabel, darkTheme, layoutLabel, compactLayout);

        // Define Server Settings panel
        serverSettings = new VBox(10);
        serverSettings.setPadding(new Insets(15));
        serverSettings.setAlignment(Pos.TOP_LEFT);

        // Connection Status Label
        connectionStatus = new Label("Server Status: Not Connected");
        connectionStatus.setFont(new Font("Arial", 14));
        connectionStatus.setTextFill(WHITE);
        connectionStatus.setStyle("-fx-background-color: lightpink; -fx-padding: 5;");
        connectionStatus.setAlignment(Pos.CENTER);
        connectionStatus.setMaxWidth(Double.MAX_VALUE);

        // Server IP field & Connect button
        Label ipLabel = new Label("Server IP:");
        TextField ipField = new TextField();
        ipField.setPromptText("Enter server IP address...");

        Button connectButton = new Button("Connect");
        connectButton.setOnAction(event -> connectToServer(ipField.getText()));

        serverSettings.getChildren().addAll(connectionStatus, ipLabel, ipField, connectButton);


        // Add panels to contentPane and show the default panel
        contentPane.getChildren().addAll(generalSettings, serverSettings);
        showGeneralSettings();  // Default to showing the general settings
    }

    // Method to show the General Settings panel
    private void showGeneralSettings() {
        generalSettings.setVisible(true);
        generalSettings.toFront();
        serverSettings.setVisible(false);
    }

    // Method to show the Server Settings panel
    private void showServerSettings() {
        serverSettings.setVisible(true);
        serverSettings.toFront();
        generalSettings.setVisible(false);
    }

        // Method to connect to the server
        private void connectToServer(String ipAddress) {
            boolean isConnected = simulateServerConnection(ipAddress); // Replace with real connection logic
    
            if (isConnected) {
                connectionStatus.setText("Connected");
                connectionStatus.setStyle("-fx-background-color: lightgreen; -fx-padding: 5;");
            } else {
                connectionStatus.setText("Not Connected");
                connectionStatus.setStyle("-fx-background-color: lightpink; -fx-padding: 5;");
            }
        }
    
        // Simulated connection logic for demonstration purposes
        private boolean simulateServerConnection(String ipAddress) {
            // Here, add logic for real server connection. For now, a simple placeholder:
            return !ipAddress.isEmpty() && ipAddress.equals("127.0.0.1");
        }
}
