package be.iminds.ilabt.jfed.gts.model.impl;

import be.iminds.ilabt.jfed.gts.model.GtsNode;
import be.iminds.ilabt.jfed.gts.model.GtsPort;
import be.iminds.ilabt.jfed.gts.model.GtsStatus;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;

import java.util.Map;

/**
 * User: twalcari
 * Date: 2/8/2016
 * Time: 10:33
 */
public class FXGtsNode implements GtsNode{
    private final StringProperty location = new SimpleStringProperty();
    private final StringProperty id = new SimpleStringProperty();
    private final StringProperty providerId = new SimpleStringProperty();

    private final DoubleProperty editorX = new SimpleDoubleProperty(-1.0);
    private final DoubleProperty editorY = new SimpleDoubleProperty(-1.0);

    private final ObjectProperty<GtsStatus> status = new SimpleObjectProperty<>();
    private final ObservableMap<String, FXGtsPort> ports = FXCollections.observableHashMap();

    private final ObservableMap<String,String> extraProperties = FXCollections.observableHashMap();


    @Override
    public String getId() {
        return id.get();
    }

    public StringProperty idProperty() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id.set(id);
    }

    @Override
    public String getLocation() {
        return location.get();
    }

    public StringProperty locationProperty() {
        return location;
    }

    @Override
    public void setLocation(String location) {
        this.location.set(location);
    }

    @Override
    public ObservableMap<String, FXGtsPort> getPorts() {
        return ports;
    }

    @Override
    public void addPort(GtsPort port) {
        assert port instanceof FXGtsPort;
        ports.put(port.getId(), (FXGtsPort)port);
    }

    @Override
    public void setPorts(Map<String, ? extends GtsPort> ports) {
        ports.forEach((id,port) -> this.ports.put(id, new FXGtsPort(port)));
        this.ports.keySet().retainAll(ports.keySet());
    }


    @Override
    public String getProviderId() {
        return providerId.get();
    }

    public StringProperty providerIdProperty() {
        return providerId;
    }

    @Override
    public void setProviderId(String providerId) {
        this.providerId.set(providerId);
    }

    @Override
    public GtsStatus getStatus() {
        return status.get();
    }

    public ObjectProperty<GtsStatus> statusProperty() {
        return status;
    }

    @Override
    public void setStatus(GtsStatus status) {
        this.status.set(status);
    }

    @Override
    public ObservableMap<String, String> getExtraProperties() {
        return extraProperties;
    }

    @Override
    public double getEditorX() {
        return editorX.get();
    }

    public DoubleProperty editorXProperty() {
        return editorX;
    }

    public void setEditorX(double editorX) {
        this.editorX.set(editorX);
    }

    @Override
    public double getEditorY() {
        return editorY.get();
    }

    public DoubleProperty editorYProperty() {
        return editorY;
    }

    public void setEditorY(double editorY) {
        this.editorY.set(editorY);
    }
}
