package de.prob2.ui.persistence;

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

import com.google.inject.Inject;
import com.google.inject.Singleton;

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

@Singleton
public final class TablePersistenceHandler {
	private final UIState uiState;

	@Inject
	private TablePersistenceHandler(UIState uiState) {
		this.uiState = uiState;
	}

	public double[] getColumnsWidth(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;
	}

	@SuppressWarnings("unchecked")
	public void setColumnsWidth(Control control, ObservableList<? extends TableColumnBase<?, ?>> columns) {
		if (!(control instanceof TreeTableView || control instanceof TableView)) {
			return;
		}

		double[] widths = uiState.getStatesViewColumnsWidth();
		double sum = 0.0;
		for (final TableColumnBase<?, ?> column : columns) {
			sum += column.getWidth();
		}

		for (int i = 0; i < columns.size() - 1; i++) {
			((TreeTableView<Object>) control).resizeColumn((TreeTableColumn<Object, ?>) columns.get(i),
					widths[i] * sum - columns.get(i).getWidth());
		}
	}

	@SuppressWarnings("unchecked")
	public void setColumnsOrder(ObservableList<? extends TableColumnBase<?, ?>> columns) {
		ObservableList<TableColumnBase<?, ?>> newColumns = FXCollections.observableArrayList();
		for (final String text : uiState.getStatesViewColumnsOrder()) {
			newColumns.addAll(columns.stream().filter(column -> column.getId().equals(text)).collect(Collectors.toList()));
		}

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

		columns.clear();
		((ObservableList<TableColumnBase<?, ?>>) columns).addAll(newColumns);
	}

	public 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;
	}
}

@@ -272,99 +272,23 @@
 rol.
-Control;%0Aimport javafx.scene.control.TableColumnBase;%0Aimport javafx.scene.control.TableView
+TableColumnBase
 ;%0Aim
@@ -910,47 +910,19 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22unchecked%22)%0A
 %09public
+ %3CS%3E
  voi
@@ -943,23 +943,34 @@
 dth(
-Control control
+TreeTableView%3CS%3E tableView
 , Ob
@@ -978,34 +978,28 @@
 ervableList%3C
-? extends 
+Tree
 TableColumnB
@@ -989,38 +989,34 @@
 %3CTreeTableColumn
-Base%3C?
+%3CS
 , ?%3E%3E columns) %7B
@@ -1020,101 +1020,8 @@
 ) %7B%0A
-%09%09if (!(control instanceof TreeTableView %7C%7C control instanceof TableView)) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A
 %09%09do
@@ -1238,23 +1238,17 @@
 %0A%09%09%09
-((TreeT
+t
 ableView
 %3CObj
@@ -1247,69 +1247,22 @@
 View
-%3CObject%3E) control).resizeColumn((TreeTableColumn%3CObject, ?%3E) 
+.resizeColumn(
 colu
@@ -1276,14 +1276,9 @@
 (i),
-%0A%09%09%09%09%09
+ 
 widt
@@ -1331,47 +1331,19 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22unchecked%22)%0A
 %09public
+ %3CS%3E
  voi
@@ -1371,34 +1371,28 @@
 ervableList%3C
-? extends 
+Tree
 TableColumnB
@@ -1382,38 +1382,34 @@
 %3CTreeTableColumn
-Base%3C?
+%3CS
 , ?%3E%3E columns) %7B
@@ -1418,32 +1418,36 @@
 %09ObservableList%3C
+Tree
 TableColumnBase%3C
@@ -1437,30 +1437,26 @@
 eTableColumn
-Base%3C?
+%3CS
 , ?%3E%3E newCol
@@ -1777,17 +1777,16 @@
 ::add);%0A
-%0A
 %09%09column
@@ -1791,72 +1791,11 @@
 mns.
-clear();%0A%09%09((ObservableList%3CTableColumnBase%3C?, ?%3E%3E) columns).add
+set
 All(

Remove unchecked casts in TablePersistenceHandler