package io.github.exaberries.boa.gui;

import io.github.exaberries.boa.Boa;
import io.github.exaberries.boa.gui.control.MultiSwitch;
import io.github.exaberries.boa.util.ByteUtil;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.ListChangeListener;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

public class SettingsPane extends Pane {
	private VBox vbox;
	private GridPane generalGrid;
	private GridPane debugGrid;
	
	public SettingsPane() {
		vbox = new VBox();
		vbox.getStyleClass().add("layout-padding");
		vbox.setSpacing(10);
		Label generalLabel = new Label("General");
		vbox.getChildren().add(generalLabel);
		Separator generalSeparator = new Separator();
		vbox.getChildren().add(generalSeparator);
		generalGrid = new GridPane();
		generalGrid.setHgap(10);
		generalGrid.setVgap(10);
		Label resolutionLabel = new Label();
		resolutionLabel.setText("Resolution");
		generalGrid.add(resolutionLabel, 0, 0);
		MultiSwitch resolution = new MultiSwitch("8-bit", "16-bit");
		generalGrid.add(resolution, 1, 0);
		Label artNetLabel = new Label();
		artNetLabel.setText("ArtNet Server");
		generalGrid.add(artNetLabel, 0, 1);
		MultiSwitch artNet = new MultiSwitch("on", "off");
		artNet.setAccessibleHelp("Toggle ArtNet Server");
		artNet.getOptions().addListener((ListChangeListener<String>)event -> {
			/*
			if (artNet.toggledProperty().get()) {
				Boa.getPlayBackEngine().getArtNetServer().start();
			} else {
				Boa.getPlayBackEngine().getArtNetServer().stop();
			}
			*/
		});
		generalGrid.add(artNet, 1, 1);
		vbox.getChildren().add(generalGrid);
		Label debugLabel = new Label("Debug");
		vbox.getChildren().add(debugLabel);
		Separator debugSeparator = new Separator();
		vbox.getChildren().add(debugSeparator);
		debugGrid = new GridPane();
		debugGrid.setHgap(10);
		debugGrid.setVgap(10);
		Label processorLabel = new Label("Processors: " + Runtime.getRuntime().availableProcessors());
		debugGrid.add(processorLabel, 0, 0);
		Label memoryLabel = new Label("Memory: 0/0");
		debugGrid.add(memoryLabel, 1, 0);
		Timeline memoryUpdateTimeLine = new Timeline();
		memoryUpdateTimeLine.setCycleCount(Animation.INDEFINITE);
		memoryUpdateTimeLine.getKeyFrames().add(new KeyFrame(Duration.seconds(1), ev -> {
			memoryLabel.setText("Memory: " + ByteUtil.getHumanReadableString(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()) + "/" + ByteUtil.getHumanReadableString(Runtime.getRuntime().totalMemory()));
		}));
		memoryUpdateTimeLine.play();
		Label engineRateLablel = new Label("Rate: 0hz");
		Timeline timeLine = new Timeline();
		timeLine.setCycleCount(Animation.INDEFINITE);
		timeLine.getKeyFrames().add(new KeyFrame(Duration.seconds(1), ev -> {
			engineRateLablel.setText("Rate: " + Boa.getPlayBackEngine().getUpdateRate() + "hz");
		}));
		timeLine.play();
		debugGrid.add(engineRateLablel, 2, 0);
		vbox.getChildren().add(debugGrid);
		getChildren().add(vbox);
	}
}