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

import be.iminds.ilabt.jfed.rspec.model.GeantTestbedType;
import be.iminds.ilabt.jfed.util.GeniUrn;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.Objects;

/**
 * User: twalcari
 * Date: 5/22/2015
 * Time: 10:38
 */
public class FXGeantTestbedType implements GeantTestbedType {
    public static final GtsVlan[] GTS_TO_RSPEC_VLANS = {
            new GtsVlan("AMS", "ep1", GeniUrn.parse("urn:publicid:IDN+gts1175-edge.ilabt.iminds.be+authority+cm"), "vlan1175"),
            new GtsVlan("LJU", "ep2", GeniUrn.parse("urn:publicid:IDN+gts1225-edge.ilabt.iminds.be+authority+cm"), "vlan1225"),
            };
    private final StringProperty name = new SimpleStringProperty();
    private final StringProperty dsl = new SimpleStringProperty();

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

    private final ObservableList<ActiveExternalDomain> activeExternalDomains = FXCollections.observableArrayList();

    public FXGeantTestbedType() {
    }

    public FXGeantTestbedType(String name) {
        this.name.setValue(name);
    }

    public FXGeantTestbedType(GeantTestbedType gtt) {
        setName(gtt.getName());
        setDsl(gtt.getDsl());

        setEditorX(gtt.getEditorX());
        setEditorY(gtt.getEditorY());

    }

    @Override
    public String getDsl() {
        return dsl.get();
    }

    public StringProperty dslProperty() {
        return dsl;
    }

    public void setDsl(String dsl) {
        this.dsl.set(dsl);
    }

    @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 String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }


    public ObservableList<ActiveExternalDomain> getActiveExternalDomains() {
        return activeExternalDomains;
    }

    public static class ActiveExternalDomain {
        private final String gtsLocation;
        private final String gtsPortId;

        public ActiveExternalDomain(String gtsLocation, String gtsPortId) {
            this.gtsLocation = gtsLocation;
            this.gtsPortId = gtsPortId;
        }

        public String getGtsLocation() {
            return gtsLocation;
        }

        public String getGtsPortId() {
            return gtsPortId;
        }
    }

    public static class GtsVlan {
        private final String gtsLocation;
        private final String gtsPortId;
        private final GeniUrn vlanUrn;
        private final String rspecNodeName;

        private GtsVlan(String gtsLocation, String gtsPortId, GeniUrn vlanUrn, String rspecNodeName) {
            this.gtsLocation = gtsLocation;
            this.gtsPortId = gtsPortId;
            this.vlanUrn = vlanUrn;
            this.rspecNodeName = rspecNodeName;
        }

        public String getGtsLocation() {
            return gtsLocation;
        }

        public String getGtsPortId() {
            return gtsPortId;
        }

        public GeniUrn getVlanUrn() {
            return vlanUrn;
        }

        public String getRspecNodeName() {
            return rspecNodeName;
        }
    }

    public static FXGeantTestbedType.GtsVlan findGtsVlan(FXGeantTestbedType.ActiveExternalDomain activeExternalDomain){
        for (GtsVlan gtsToRspecVlan : GTS_TO_RSPEC_VLANS) {
            if(Objects.equals(gtsToRspecVlan.getGtsLocation(), activeExternalDomain.getGtsLocation()) &&
                    Objects.equals(gtsToRspecVlan.getGtsPortId(), activeExternalDomain.getGtsPortId()))
                return gtsToRspecVlan;
        }
        return null;
    }

}
