package ui;

import javafx.animation.PauseTransition;
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.effect.DropShadow;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.ArrayList;
import java.util.List;

public class EvaluationPopupUI {

    private final List<Double> votes = new ArrayList<>();
    private double average = -1;

    public double showAndWait() {
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("Schulungsevaluation");

        VBox root = new VBox(30);
        root.setPadding(new Insets(35));
        root.setAlignment(Pos.TOP_CENTER);
        root.setStyle("-fx-background-color: linear-gradient(to bottom right, #f8f9fa, #e9ecef);");

        Label heading = new Label("Wie empfanden Sie diese Schulung in Schulnoten?");
        heading.setStyle("-fx-font-size: 22; -fx-font-weight: bold; -fx-text-fill: #333;");

        GridPane grid = new GridPane();
        grid.setHgap(30);
        grid.setVgap(30);
        grid.setAlignment(Pos.CENTER);

        for (int i = 1; i <= 6; i++) {
            StackPane tile = createTile(i, stage, grid);
            int row = (i - 1) / 3;
            int col = (i - 1) % 3;
            grid.add(tile, col, row);
        }

        Label thankYou = new Label("Vielen Dank!");
        thankYou.setStyle("-fx-font-size: 26; -fx-text-fill: #2ecc71; -fx-font-weight: bold;");
        thankYou.setVisible(false);

        Button btnFinish = new Button("Evaluation beenden");
        btnFinish.setStyle("""
                -fx-font-size: 15;
                -fx-padding: 10 25;
                -fx-background-color: #007bff;
                -fx-text-fill: white;
                -fx-background-radius: 8;
                -fx-font-weight: bold;
                """);
        btnFinish.setOnMouseEntered(e -> btnFinish.setStyle(btnFinish.getStyle() + "-fx-background-color: #3399ff;"));
        btnFinish.setOnMouseExited(e -> btnFinish.setStyle(btnFinish.getStyle().replace("-fx-background-color: #3399ff;", "-fx-background-color: #007bff;")));

        btnFinish.setOnAction(e -> {
            if (!votes.isEmpty()) {
                average = votes.stream().mapToDouble(Double::doubleValue).average().orElse(-1);
            } else {
                average = -1;
            }
            stage.close();
        });

        root.getChildren().addAll(heading, grid, thankYou, btnFinish);

        Scene scene = new Scene(root, 800, 650);
        stage.setScene(scene);
        stage.showAndWait();

        return average;
    }

    private StackPane createTile(int number, Stage stage, GridPane grid) {
        String[] labels = {
                "", "sehr gut", "gut", "befriedigend", "ausreichend", "mangelhaft", "ungenügend"
        };

        VBox tileBox = new VBox(5);
        tileBox.setAlignment(Pos.CENTER);

        Label numLabel = new Label(String.valueOf(number));
        numLabel.setStyle("-fx-font-size: 54; -fx-font-weight: bold; -fx-text-fill: #333;");

        Label textLabel = new Label(labels[number]);
        textLabel.setStyle("-fx-font-size: 16; -fx-text-fill: #666;");

        tileBox.getChildren().addAll(numLabel, textLabel);

        StackPane tile = new StackPane(tileBox);
        tile.setPrefSize(240, 240);
        tile.setStyle("""
                -fx-background-color: white;
                -fx-border-color: #ccc;
                -fx-border-radius: 20;
                -fx-background-radius: 20;
                -fx-border-width: 2;
                """);

        DropShadow shadow = new DropShadow(15, Color.rgb(0, 0, 0, 0.15));
        tile.setEffect(shadow);

        tile.setOnMouseEntered(e -> tile.setStyle(tile.getStyle().replace("-fx-background-color: white;", "-fx-background-color: #dff0ff;")));
        tile.setOnMouseExited(e -> tile.setStyle(tile.getStyle().replace("-fx-background-color: #dff0ff;", "-fx-background-color: white;")));

        tile.setOnMouseClicked(e -> {
            votes.add((double) number);
            grid.setVisible(false);

            Label thankYou = (Label) ((VBox) stage.getScene().getRoot()).getChildren().get(2);
            thankYou.setVisible(true);

            PauseTransition pause = new PauseTransition(Duration.seconds(1.5));
            pause.setOnFinished(ev -> {
                thankYou.setVisible(false);
                grid.setVisible(true);
            });
            pause.play();
        });

        return tile;
    }
}
