package be.iminds.ilabt.jfed.rspec.model.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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

    private GeniUrn componentManagerId;
    private GeniUrn componentId;
    private String componentName;
    private String frequency;

    private double editorX = -1.0;
    private double editorY = -1.0;

    private final List<String> leaseIdRefs = new ArrayList<>();
    private final List<ExtraXml> extraXml = new ArrayList<>();

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

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

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

    public BasicChannel() {

    }

    public GeniUrn getComponentId() {
        return componentId;
    }

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

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

        GeniUrn geniUrn = GeniUrn.parse(value);

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

    public GeniUrn getComponentManagerId() {
        return componentManagerId;
    }

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

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

        GeniUrn geniUrn = GeniUrn.parse(value);

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

    public String getComponentName() {
        return componentName;
    }

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

    public String getFrequency() {
        return frequency;
    }

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

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

    @Override
    public void setEditorX(double value) {
        editorX = value;
    }

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

    @Override
    public void setEditorY(double value) {
        editorY = value;
    }

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

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

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