package views;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import modele.ConcreteSensor;
import modele.Sensor;
import modele.VirtualSensor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class WeatherController extends FXMLWindow {

    Sensor sensor;
    List<Sensor> sensorList;

    @FXML
    Spinner<Double> tempSpinner;

    @FXML
    TreeView<Sensor> tree;
    public WeatherController(List<Sensor> sensorsList, String url, String title) throws IOException {
        super(title, url);

        TreeItem<Sensor> root = new TreeItem<>(sensorsList.get(0));
        root.setExpanded(true);
        tree.setRoot(root);
        sensorList = sensorsList;

        tree.setVisible(true);
        for (Sensor sensor : sensorsList) {
            TreeItem<Sensor> sensorItem = new TreeItem<>(sensor);
            root.getChildren().add(sensorItem);
        }



    }

    public void initialize(){
        //------------------------------------------------------------------------------------------------------

        sensor = new ConcreteSensor(56);

        tree.setCellFactory(param -> new TreeCell<Sensor>() {
            protected void updateItem(Sensor item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                } else {
                    textProperty().bindBidirectional(item.getName());
                }
            }
        });

        //------------------------------------------------------------------------------------------------------
        // SPINNER
        tempSpinner.setVisible(true);

        sensor.getTempProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("La température a changé de " + oldValue + " à " + newValue);
        });

        SpinnerValueFactory<Double> spinnerValueFactory= new SpinnerValueFactory.DoubleSpinnerValueFactory(-273,10000, sensor.getTemp(), 0.5);
        tempSpinner.setValueFactory(spinnerValueFactory);
        tempSpinner.getValueFactory().valueProperty().bindBidirectional(sensor.getTempProperty());
        sensor.setTemp(sensor.generateTemperature());
        System.out.println("initialize spinner");
        System.out.println(tempSpinner.getValueFactory().getValue());

    };
}
