package be.iminds.ilabt.jfed.rspec.model.javafx_impl;

import be.iminds.ilabt.jfed.rspec.model.AddressPool;
import be.iminds.ilabt.jfed.rspec.model.IPv4;
import be.iminds.ilabt.jfed.rspec.model.impl.BasicIPv4;
import be.iminds.ilabt.jfed.util.GeniUrn;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 * User: twalcari
 * Date: 10/27/2015
 * Time: 14:06
 */
public class FXAddressPool implements AddressPool {

    private final StringProperty clientId = new SimpleStringProperty();
    private final ObjectProperty<GeniUrn> componentManagerId = new SimpleObjectProperty<>();
    private final ObjectProperty<Integer> count = new SimpleObjectProperty<>();
    private final StringProperty type = new SimpleStringProperty();
    private final ObservableList<IPv4> iPv4s = FXCollections.observableArrayList();

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

    public FXAddressPool() {
    }

    public FXAddressPool(AddressPool source) {
        setClientId(source.getClientId());
        setComponentManagerId(source.getComponentManagerId());
        setCount(source.getCount());
        setType(source.getType());
        source.getIPv4s().stream()
                .map(BasicIPv4::new)   //copy all instances
                .forEach(iPv4s::add);

        setEditorX(source.getEditorX());
        setEditorY(source.getEditorY());
    }


    @Override
    public String getClientId() {
        return clientId.get();
    }

    public StringProperty clientIdProperty() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId.set(clientId);
    }

    @Override
    public GeniUrn getComponentManagerId() {
        return componentManagerId.get();
    }

    public ObjectProperty<GeniUrn> componentManagerIdProperty() {
        return componentManagerId;
    }

    public void setComponentManagerId(GeniUrn componentManagerId) {
        this.componentManagerId.set(componentManagerId);
    }

    @Override
    public Integer getCount() {
        return count.get();
    }

    public ObjectProperty<Integer> countProperty() {
        return count;
    }

    public void setCount(Integer count) {
        this.count.set(count);
    }

    @Override
    public String getType() {
        return type.get();
    }

    public StringProperty typeProperty() {
        return type;
    }

    public void setType(String type) {
        this.type.set(type);
    }

    @Override
    public void addIPv4(IPv4 iPv4) {
        iPv4s.add(iPv4);
    }

    @Override
    public boolean deleteIPv4(IPv4 iPv4) {
        return iPv4s.remove(iPv4);
    }

    @Override
    public ObservableList<IPv4> getIPv4s() {
        return iPv4s;
    }

    @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);
    }
}
