// SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk>
// SPDX-FileCopyrightText: 2025 <Alexander Marthin Klemensen stud-marthin@ruc.dk>
// SPDX-FileCopyrightText: 2025 <Ian Valentin Christensen stud-ianc@ruc.dk>
// SPDX-FileCopyrightText: 2025 <Zahed Noos zahed@ruc.dk>
// SPDX-License-Identifier: GPL-3.0-or-later

package com.example.portfolio2;

import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

import java.util.List;

/// Bachelorizer - Controller
class Controller {

	/// Storage model
	private Database store;

	/// Application view
	private Window view;

	/// clear the participation database at program launch
	void initialize() {
		store.initialize();
	}

	/// default constructor
	///
	/// @param store  Storage model
	/// @param view   Application view
	Controller(final Database store, final Window view) {
		this.store = store;
		this.view = view;
	}

	/// callback when category has been selected
	///
	/// @param combo   involved activity box
	/// @param select  selected item
	/// @param area    whole text area
	void onCategorySelected(
		final ComboBox<String> combo,
		final ComboBox<String> select,
		final TextArea area
	) {

		// clear the activity selection box
		select.getItems().clear();

		// clear text area
		area.clear();

		// fill activity box from data in store
		select.getItems().addAll(
			store.selectProgram(combo.getValue()));
	}

	/// callback when activity has been selected
	///
	/// @param combo   involved activity box
	/// @param select  selected item
	/// @param area    whole text area
	void onActivitySelected(
		final ComboBox<String> combo,
		final ComboBox<String> select,
		final TextArea area
	) {

		// pass the value chosen in the box
		addActivity(select.getValue(), area);

		// update text area based on category choice
		//
		// Users can choose from the ComboBox,
		// and string (activity) and the area will then update.
		updateArea(combo, area);
	}

	/// callback when subject module has been selected
	///
	/// @param subject1        involved 1st column subject module box
	/// @param subject2        involved 2nd column subject module box
	/// @param subjectModules  list of selected subject modules
	void onSubjectModuleSelected(
		final ComboBox<String> subject1,
		final ComboBox<String> subject2,
		final List<String> subjectModules
	) {

		// remove chosen option from opposite subject module box
		for (String sub: subjectModules) {
			if (sub.equals(subject1.getValue())) {
				subject2.getItems().remove(
					subject1.getValue());
			} else if (
				!sub.equals(subject1.getValue())
				&& !subject2.getItems().contains(sub)
			) {
				subject2.getItems().add(sub);
			}
		}
	}

	/// add participation to database
	/// @param s         activity identifier
	/// @param textArea  whole text area
	void addActivity(final String s, final TextArea textArea) {
		store.addParticipation(store.getActivityIndeks(s));
	}

	/// update text area for an activity box
	///
	/// Clears the text area
	/// and adds all activity names from activities in participation.
	///
	/// @param combo     involved activity box
	/// @param textArea  whole text area
	void updateArea(
		final ComboBox<String> combo, final TextArea textArea
	) {
		textArea.clear();
		for (String s: store.getParticipation(combo.getValue())
		) {
			textArea.appendText(s + "\n");
		}
	}

	/// update label with current ECTS of program type
	/// @param ectslabel  text display area for ECTS points
	/// @param comboBox   involved activity box
	void updateEcts(
		final Label ectslabel, final ComboBox<String> comboBox
	) {
		ectslabel.setText("ECTS: "
			+ store.getSumEcts(comboBox.getValue()));
	}

	void fillElective(final ComboBox<String> electiveBox) {
		electiveBox.getItems().addAll(store.getAllActivities());
	}
}