package prv.rli.codetest;

import java.util.List;
import java.util.Map;

import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

@SuppressWarnings("all")
public class TableViewRefreshScrollingBug extends Application {

    ObservableList<Map<String, String>> items = FXCollections.observableArrayList();
    TableView<Map<String, String>> table = new TableView<>();
    Timeline reloadTimeline = new Timeline();

	@Override
    public void start(Stage primaryStage) throws Exception {
        table.setItems(items);
        TableColumn<Map<String, String>, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setCellValueFactory(new MapValueFactory("firstname"));
        TableColumn<Map<String, String>, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setCellValueFactory(new MapValueFactory("lastname"));
        TableColumn<Map<String, String>, String> quoteCol = new TableColumn<>("Quote");
        quoteCol.setCellValueFactory(new MapValueFactory("quote"));
        table.getColumns().setAll(firstNameCol, lastNameCol, quoteCol);
        addData();
        
        Button clearButton = new Button("Clear");
        clearButton.setOnAction(e -> {
        	items.clear();
        });
        Button loadButton = new Button("Load");
        loadButton.setOnAction(e -> {
        	addData();
        });
        
        VBox vbox = new VBox();
        vbox.getChildren().setAll(clearButton, loadButton, table);

        Scene scene = new Scene(vbox, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void addData() {
    	items.addAll(List.of(
    		Map.of("firstname", "Albert", "lastname", "Einstein", "quote", "Everything should be made as simple as possible, but not simpler") 
    		, Map.of("firstname", "John R.R.", "lastname", "Tolkien", "quote", "All we have to decide is what to do with the time that is given us") 
    		, Map.of("firstname", "Niklaus.", "lastname", "Wirth", "quote", "The idea that one might derive satisfaction from his or her successful work, because that work is ingenious, beautiful, or just pleasing, has become ridiculed")
    		, Map.of("firstname", "Time", "lastname", "Stamp", "quote", "" + System.currentTimeMillis())    		
    	));
    }

    public static void main(String[] args) {
        launch(args);
    }
}
