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

import be.iminds.ilabt.jfed.rspec.model.Channel;
import be.iminds.ilabt.jfed.rspec.parser.ExtraXml;
import be.iminds.ilabt.jfed.util.GeniUrn;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * User: twalcari
 * Date: 4/23/2015
 * Time: 15:29
 */
public class FXRspecChannel implements Channel {
    private static final Logger LOG = LoggerFactory.getLogger(FXRspecChannel.class);

    protected final ObjectProperty<GeniUrn> componentManagerId = new SimpleObjectProperty<>();
    protected final ObjectProperty<GeniUrn> componentId = new SimpleObjectProperty<>();
    protected final StringProperty componentName = new SimpleStringProperty();
    protected final StringProperty frequency = new SimpleStringProperty();

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

    protected final ObservableList<String> leaseIdRefs = FXCollections.observableArrayList();
    private final List<ExtraXml> extraXml = new ArrayList<>();

    public FXRspecChannel() {
    }

    public FXRspecChannel(Channel channel) {
        this.componentId.set(channel.getComponentId());
        this.componentManagerId.set(channel.getComponentManagerId());
        this.componentName.set(channel.getComponentName());
        this.frequency.set(channel.getFrequency());

        this.editorX.set(channel.getEditorX());
        this.editorY.set(channel.getEditorY());

        leaseIdRefs.addAll(channel.getLeaseIdRefs());
    }

    @Override
    public GeniUrn getComponentId() {
        return componentId.get();
    }

    public ObjectProperty<GeniUrn> componentIdProperty() {
        return componentId;
    }

    public void setComponentId(GeniUrn componentId) {
        this.componentId.set(componentId);
    }

    @Override
    public void setComponentId(String value) {
        if (value == null) {
            this.componentId.set(null);
            return;
        }

        GeniUrn geniUrn = GeniUrn.parse(value);

        if (geniUrn != null) {
            this.componentId.set(geniUrn);
        } else {
            LOG.warn("Could not set componentId to {} as it is not a valid URN!", value);
            this.componentId.set(null);
        }
    }

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

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

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

    @Override
    public void setComponentManagerId(String value) {
        if (value == null) {
            this.componentManagerId.set(null);
            return;
        }

        GeniUrn geniUrn = GeniUrn.parse(value);

        if (geniUrn != null) {
            this.componentManagerId.set(geniUrn);
        } else {
            LOG.warn("Could not set componentManagerId to {} as it is not a valid URN!", value);
            this.componentManagerId.set(null);
        }
    }


    @Override
    public String getComponentName() {
        return componentName.get();
    }

    public StringProperty componentNameProperty() {
        return componentName;
    }

    public void setComponentName(String componentName) {
        this.componentName.set(componentName);
    }

    @Override
    public String getFrequency() {
        return frequency.get();
    }

    public StringProperty frequencyProperty() {
        return frequency;
    }

    public void setFrequency(String frequency) {
        this.frequency.set(frequency);
    }

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

    @Override
    public void addLeaseIdRef(String leaseIdRef) {
        leaseIdRefs.add(leaseIdRef);
    }

    @Override
    public ObservableList<String> getLeaseIdRefs() {
        return leaseIdRefs;
    }

    @Override
    public List<ExtraXml> getExtraXml() {
        return extraXml;
    }
}
