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

import be.iminds.ilabt.jfed.gts.model.GtsLink;
import be.iminds.ilabt.jfed.gts.model.GtsPort;
import be.iminds.ilabt.jfed.gts.model.GtsStatus;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;

import java.util.Map;

/**
 * User: twalcari
 * Date: 6/5/2015
 * Time: 16:02
 */
public class FXGtsLink implements GtsLink {
    private final StringProperty id = new SimpleStringProperty();
    private final StringProperty providerId = new SimpleStringProperty();
    private final ObjectProperty<GtsStatus> status = new SimpleObjectProperty<>();
    private final ObservableMap<String, FXGtsPort> ports = FXCollections.observableHashMap();

    private final ObjectProperty<Integer> capacity = new SimpleObjectProperty<>();

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

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

    public StringProperty idProperty() {
        return id;
    }

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


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

    @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 void addPort(GtsPort port) {
        assert port instanceof FXGtsPort;
        ports.put(port.getId(),(FXGtsPort)port);
    }

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

    @Override
    public Integer getCapacity() {
        return capacity.get();
    }

    public ObjectProperty<Integer> capacityProperty() {
        return capacity;
    }

    public void setCapacity(Integer capacity) {
        this.capacity.set(capacity);
    }
}
