package de.prob2.ui.persistence;

import java.util.List;
import java.util.stream.Collectors;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumnBase;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;

public final class TablePersistenceHandler {
	private TablePersistenceHandler() {
		throw new AssertionError("Utility class");
	}

	public static List<Double> getAbsoluteColumnWidths(List<? extends TableColumnBase<?, ?>> columns) {
		return columns.stream()
			.map(TableColumnBase::getWidth)
			.collect(Collectors.toList());
	}
	
	public static <S> void setAbsoluteColumnWidths(TreeTableView<S> tableView, List<TreeTableColumn<S, ?>> columns, List<Double> widths) {
		for (int i = 0; i < columns.size(); i++) {
			tableView.resizeColumn(columns.get(i), widths.get(i) - columns.get(i).getWidth());
		}
	}

	public static double[] getRelativeColumnWidths(List<? extends TableColumnBase<?, ?>> columns) {
		double[] result = new double[columns.size()];
		double sum = 0.0;
		for (int i = 0; i < columns.size(); i++) {
			result[i] = columns.get(i).getWidth();
			sum += result[i];
		}
		for (int i = 0; i < result.length; i++) {
			result[i] /= sum;
		}
		return result;
	}

	public static <S> void setRelativeColumnWidths(TreeTableView<S> tableView, List<TreeTableColumn<S, ?>> columns, double[] widths) {
		double sum = 0.0;
		for (final TableColumnBase<?, ?> column : columns) {
			sum += column.getWidth();
		}

		for (int i = 0; i < columns.size() - 1; i++) {
			tableView.resizeColumn(columns.get(i), widths[i] * sum - columns.get(i).getWidth());
		}
	}

	public static <S> void setColumnsOrder(ObservableList<TreeTableColumn<S, ?>> columns, final String[] order) {
		ObservableList<TreeTableColumn<S, ?>> newColumns = FXCollections.observableArrayList();
		for (final String text : order) {
			newColumns.addAll(columns.stream().filter(column -> column.getId().equals(text)).collect(Collectors.toList()));
		}

		columns.stream().filter(column -> !newColumns.contains(column)).forEach(newColumns::add);
		columns.setAll(newColumns);
	}

	public static String[] getColumnsOrder(List<? extends TableColumnBase<?, ?>> columns) {
		String[] order = new String[columns.size()];
		for (int i = 0; i < columns.size(); i++) {
			order[i] = columns.get(i).getId();
		}
		return order;
	}
}

@@ -27,16 +27,44 @@
 tence;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -119,49 +119,8 @@
 s;%0A%0A
-import javafx.collections.FXCollections;%0A
 impo
@@ -913,24 +913,28 @@
  static 
-d
+List%3CD
 ouble
-%5B%5D
+%3E
  getRela
@@ -1005,270 +1005,185 @@
 %7B%0A%09%09
-double%5B%5D result = new double%5Bcolumns.size()%5D;%0A%09%09double sum = 0.0;%0A%09%09for (int i = 0; i %3C columns.size(); i++) %7B%0A%09%09%09result%5Bi%5D = columns.get(i).getWidth();%0A%09%09%09sum += result%5Bi%5D;%0A%09%09%7D%0A%09%09for (int i = 0; i %3C result.length; i++) %7B%0A%09%09%09result%5Bi%5D /= sum;%0A%09%09%7D%0A%09%09return result
+final double sum = columns.stream().mapToDouble(TableColumnBase::getWidth).sum();%0A%09%09return columns.stream()%0A%09%09%09.map(col -%3E col.getWidth() / sum)%0A%09%09%09.collect(Collectors.toList())
 ;%0A%09%7D
@@ -1301,16 +1301,20 @@
 ns, 
-d
+List%3CD
 ouble
-%5B%5D
+%3E
  wid
@@ -1322,16 +1322,22 @@
 hs) %7B%0A%09%09
+final 
 double s
@@ -1345,101 +1345,70 @@
 m = 
-0.0;%0A%09%09for (final TableColumnBase%3C?, ?%3E column : columns) %7B%0A%09%09%09sum += column.getWidth();%0A%09%09%7D%0A
+columns.stream().mapToDouble(TableColumnBase::getWidth).sum();
 %0A%09%09f
@@ -1501,19 +1501,23 @@
 , widths
-%5Bi%5D
+.get(i)
  * sum -
@@ -1646,24 +1646,28 @@
 , final 
+List%3C
 String
-%5B%5D
+%3E
  order) 
@@ -1670,26 +1670,22 @@
 er) %7B%0A%09%09
-Observable
+final 
 List%3CTre
@@ -1721,32 +1721,12 @@
 s = 
-FXCollections.observable
+new 
 Arra
@@ -1730,16 +1730,18 @@
 rrayList
+%3C%3E
 ();%0A%09%09fo
@@ -2033,24 +2033,28 @@
  static 
+List%3C
 String
-%5B%5D
+%3E
  getColu
@@ -2117,154 +2117,96 @@
 %7B%0A%09%09
-String%5B%5D order = new String%5Bcolumns.size()%5D;%0A%09%09for (int i = 0; i %3C columns.size(); i++) %7B%0A%09%09%09order%5Bi%5D = columns.get(i).getId();%0A%09%09%7D%0A%09%09return order
+return columns.stream()%0A%09%09%09.map(TableColumnBase::getId)%0A%09%09%09.collect(Collectors.toList())
 ;%0A%09%7D

Change TablePersistenceHandler methods to use lists instead of arrays