// SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk>
// SPDX-License-Identifier: GPL-3.0-or-later

package dk.biks.bachelorizer;

import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.Scene;
import javafx.stage.Stage;

/// Bachelorizer - JavaFX Window view
// Class is final to forbid subclassing,
// because object is passed to controller during instantiation
public final class Window extends Application {

	/// Default constructor
	// (declared explicitly only to silence javadoc)
	public Window() {
	}

	/// window width
	private static final int WINDOW_WIDTH = 500;

	/// window height
	private static final int WINDOW_HEIGHT = 500;

	/// box height
	private static final int BOX_HEIGHT = 10;

	/// Label styling
	private static final String LABEL_STYLE =
		"-fx-font-weight: bold; -fx-font-size: 20;";

	/// Application model
	private GUI model = new GUI();

	/// Application controller
	private Control control = new Control(model, this);

	/// Name of student
	private TextField nameEntry = new TextField();

	/// Text entry for adding an activity
	private TextField activityEntry = new TextField();

	/// Text area for activity entries
	private TextArea area = new TextArea();

	/// Button to delete one activity
	private Button delOne = new Button("Delete one");

	/// Button to delete all activities
	private Button delAll = new Button("Delete all");

	/// Application instantiation
	///
	/// @param args  application parameters
	public static void main(final String[] args) {
		launch(args);
	}

	@Override
	public void start(final Stage stage) {

		// pass application parameters to controller
		control.setParameters(getParameters().getRaw());

		// add listeners
//		NameEntry.setOnAction(e -> control.enterName(
//			activityEntry.getText()));
		activityEntry.setOnAction(e -> control.enterActivity(
			activityEntry.getText()));
		delOne.setOnAction(e -> control.delOne());
		delAll.setOnAction(e -> control.delAll());

		// add buttons
		VBox root = new VBox(BOX_HEIGHT,
			ourHBox("Student", nameEntry),
			ourHBox("Add activity", activityEntry),
			new HBox(BOX_HEIGHT, delOne, delAll),
			area);

		// compose stage
		Scene scene = new Scene(
			root, WINDOW_WIDTH, WINDOW_HEIGHT);
		stage.setTitle("JavaFX Demo");
		stage.setScene(scene);
		stage.show();
	}

	/// action to apply student name
	///
	/// @param s  Text to apply
	public void setStudentName(final String s) {
		nameEntry.setText(s);
	}

	/// action to apply text to area
	///
	/// @param s  Text to apply
	public void setArea(final String s) {
		area.setText(s);
	}

	/// Button action to clear field
	public void clearActivityEntry() {
		activityEntry.setText("");
	}

	/// Styled HBox with label and TextField
	///
	/// @param s  Label string
	/// @param f  Text field
	/// @return   HBox containing styled label and text field
	public HBox ourHBox(final String s, final TextField f) {
		Label label = new Label(s + ":");
		label.setStyle(LABEL_STYLE);

		return new HBox(BOX_HEIGHT, label, f);
	}
}