package be.iminds.ilabt.jfed.experimenter_gui.slice;

import be.iminds.ilabt.jfed.experimenter_gui.util.ui.MacWithDotsBinding;
import be.iminds.ilabt.jfed.experimenter_gui.util.ui.ReadOnlyTextFieldTableCell;
import be.iminds.ilabt.jfed.rspec.model.RspecInterface;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecInterface;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecLink;
import be.iminds.ilabt.jfed.ui.javafx.FXMLUtil;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.util.Pair;
import org.controlsfx.glyphfont.FontAwesome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;

/**
 * User: twalcari
 * Date: 5/12/2015
 * Time: 10:13
 */
public class LinkPropertiesDialog extends VBox {
    private static final Logger LOG = LoggerFactory.getLogger(LinkPropertiesDialog.class);

    @FXML
    private TableView<Pair<FXRspecInterface, FXRspecInterface.IpAddress>> interfacesTableView;

    @FXML
    private TableColumn<Pair<FXRspecInterface, FXRspecInterface.IpAddress>, String> nodeIdTableColumn;
    @FXML
    private TableColumn<Pair<FXRspecInterface, FXRspecInterface.IpAddress>, String> ifaceIdTableColumn;
    @FXML
    private TableColumn<Pair<FXRspecInterface, FXRspecInterface.IpAddress>, String> ifaceIpTableColumn;
    @FXML
    private TableColumn<Pair<FXRspecInterface, FXRspecInterface.IpAddress>, String> ifaceNetmaskTableColumn;
    @FXML
    private TableColumn<Pair<FXRspecInterface, FXRspecInterface.IpAddress>, String> ifaceMacTableColumn;

    @FXML
    private HBox vlanTagBox;
    @FXML
    private TextField vlanTagTextField;

    private final FXRspecLink link;


    private final Dialog<ButtonType> dialog;

    public LinkPropertiesDialog(@Nonnull FXRspecLink link) {
        this.link = link;

        FXMLUtil.injectFXML(this);

        DoubleBinding tableWidth = interfacesTableView.widthProperty().subtract(4);

        nodeIdTableColumn.prefWidthProperty().bind(tableWidth.multiply(0.125));
        ifaceIdTableColumn.prefWidthProperty().bind(tableWidth.multiply(0.125));
        ifaceIpTableColumn.prefWidthProperty().bind(tableWidth.multiply(0.25));
        ifaceNetmaskTableColumn.prefWidthProperty().bind(tableWidth.multiply(0.25));
        ifaceMacTableColumn.prefWidthProperty().bind(tableWidth.multiply(0.25));

        nodeIdTableColumn.setCellValueFactory(param -> param.getValue().getKey().getNode().clientIdProperty());
        ifaceIdTableColumn.setCellValueFactory(param -> param.getValue().getKey().clientIdProperty());
        ifaceMacTableColumn.setCellValueFactory(param -> new MacWithDotsBinding(param.getValue().getKey().macAddressProperty()));
        ifaceIpTableColumn.setCellValueFactory(param -> new SimpleStringProperty(
                param.getValue() != null ? param.getValue().getValue().getAddress() : ""));
        ifaceNetmaskTableColumn.setCellValueFactory(param -> new SimpleStringProperty(
                param.getValue() != null ? param.getValue().getValue().getNetmask() : ""));


        interfacesTableView.prefHeightProperty().bind(Bindings.size(interfacesTableView.getItems()).multiply(28).add(25 /*header */ + 12 /*safety margin */));

        ifaceIpTableColumn.setCellFactory(ReadOnlyTextFieldTableCell.forTableColumn());
        ifaceNetmaskTableColumn.setCellFactory(ReadOnlyTextFieldTableCell.forTableColumn());
        ifaceMacTableColumn.setCellFactory(ReadOnlyTextFieldTableCell.forTableColumn());

        List<Pair<FXRspecInterface, FXRspecInterface.IpAddress>> ifaceWithAddressCombos =
                new ArrayList<>();

        for (FXRspecInterface iface : link.getInterfaces()) {
            if (!iface.getIpAddresses().isEmpty()) {
                for (RspecInterface.IpAddress ipAddress : iface.getIpAddresses()) {
                    ifaceWithAddressCombos.add(new Pair<>(iface, ipAddress));
                }
            } else {
                ifaceWithAddressCombos.add(new Pair<>(iface, null));
            }
        }

        if (ifaceWithAddressCombos.isEmpty()) {
            interfacesTableView.setVisible(false);
            interfacesTableView.setManaged(false);
        } else {
            interfacesTableView.getItems().addAll(ifaceWithAddressCombos);
        }

        if (link.getVlanTag() != null) {
            vlanTagTextField.setText(link.getVlanTag().toString());
        } else {
            vlanTagBox.setVisible(false);
            vlanTagBox.setManaged(false);
        }


        DialogPane dialogPane = new DialogPane();
        dialogPane.setContent(this);
        dialogPane.setHeaderText("Properties of " + link.getClientId());
        dialogPane.setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.INFO, Color.BLACK));
        dialogPane.getButtonTypes().setAll(ButtonType.CLOSE);

        //initialize dialog
        dialog = new javafx.scene.control.Dialog<>();
        dialog.setDialogPane(dialogPane);
        dialog.setTitle(dialogPane.getHeaderText());
        dialog.setHeaderText(dialog.getTitle());
        dialog.setResizable(true);


    }


    public void showDialog() {
        dialog.show();
    }
}
