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

import be.iminds.ilabt.jfed.rspec.model.LinkSetting;

import java.util.Objects;

/**
 * BasicLinkSetting
 */
public class BasicLinkSetting implements LinkSetting {
    private final BasicRspecInterface from;
    private final BasicRspecInterface to;
    private Long capacity_kbps;
    private Integer latency_ms;
    private Double packetLoss;

    private boolean shownIfNotActive;

    public BasicLinkSetting(BasicRspecInterface fromIface, BasicRspecInterface toIface) {
        this.from = fromIface;
        this.to = toIface;
        this.shownIfNotActive = false;

        assert from != null;
        assert to != null;
    }

    @Override
    public BasicRspecInterface getFromIface() {
        return from;
    }

    @Override
    public BasicRspecInterface getToIface() {
        return to;
    }

    @Override
    public long getCapacity_Kbps() {
        return capacity_kbps == null ? -1 : capacity_kbps;
    }

    @Override
    public void setCapacity_Kbps(Long value) {
        capacity_kbps = value;
    }

    @Override
    public boolean isCapacitySet() {
        return capacity_kbps != null && capacity_kbps >= 0;
    }

    @Override
    public int getLatency_ms() {
        return latency_ms == null ? -1 : latency_ms;
    }

    @Override
    public void setLatency_ms(Integer value) {
        latency_ms = value;
    }

    @Override
    public boolean isLatencySet() {
        return latency_ms != null && latency_ms >= 0;
    }

    @Override
    public double getPacketLoss() {
        return packetLoss == null ? -1.0 : packetLoss;
    }

    @Override
    public void setPacketLoss(Double value) {
        packetLoss = value;
    }

    @Override
    public boolean isPacketLossSet() {
        return packetLoss != null && packetLoss >= 0.0;
    }

    @Override
    public boolean isActive() {
        return isCapacitySet() || isPacketLossSet() || isLatencySet();
    }

    @Override
    public boolean isShownIfNotActive() {
        return shownIfNotActive;
    }

    @Override
    public void setShownIfNotActive(boolean shownIfNotActive) {
        this.shownIfNotActive = shownIfNotActive;
    }

    @Override
    public void setSameAs(LinkSetting o) {
        assert Objects.equals(o.getFromIface().getClientId(), getFromIface().getClientId());
        assert Objects.equals(o.getToIface().getClientId(), getToIface().getClientId());

        if (o.isCapacitySet())
            setCapacity_Kbps(o.getCapacity_Kbps());
        else
            setCapacity_Kbps(null);

        if (o.isLatencySet())
            setLatency_ms(o.getLatency_ms());
        else
            setLatency_ms(null);

        if (o.isPacketLossSet())
            setPacketLoss(o.getPacketLoss());
        else
            setPacketLoss(null);

        this.shownIfNotActive = o.isShownIfNotActive();
    }
}
