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

import be.iminds.ilabt.jfed.experimenter_gui.util.ImageUtil;
import be.iminds.ilabt.jfed.rspec.model.IPv4;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXAddressPool;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.VBox;
import org.controlsfx.glyphfont.FontAwesome;

/**
 * User: twalcari
 * Date: 10/29/2015
 * Time: 10:36
 */
public class AddressPoolPropertiesDialog extends Dialog<ButtonType> {

    private final TableView<IPv4> addressesTableView;
    private final TableColumn<IPv4, String> addressTableColumn;
    private final TableColumn<IPv4, String> netmaskTableColumn;


    private final FXAddressPool addressPool;

    public AddressPoolPropertiesDialog(FXAddressPool addressPool) {
        this.addressPool = addressPool;
        setTitle(String.format("Address pool '%s'", addressPool.getClientId()));
        setHeaderText(getTitle());


        setGraphic(new ImageView(new Image(ImageUtil.class.getResource("/images/node_types/64/addresspool.png").toExternalForm())));

        addressTableColumn = new TableColumn<>("IP Address");
        addressTableColumn.setCellValueFactory(param -> new ReadOnlyStringWrapper(param.getValue().getAddress()));
        netmaskTableColumn = new TableColumn<>("Netmask");
        netmaskTableColumn.setCellValueFactory(param -> new ReadOnlyStringWrapper(param.getValue().getNetmask()));


        addressesTableView = new TableView<>();
        addressesTableView.getColumns().add(addressTableColumn);
        addressesTableView.getColumns().add(netmaskTableColumn);
        addressTableColumn.prefWidthProperty().bind(addressesTableView.widthProperty().divide(2).subtract(1));
        netmaskTableColumn.prefWidthProperty().bind(addressesTableView.widthProperty().divide(2).subtract(1));

        addressesTableView.setRowFactory(param -> {
            TableRow<IPv4> iPv4TableRow = new TableRow<>();

            MenuItem copyAddressMenuItem = new MenuItem("Copy IP Address");
            copyAddressMenuItem.setGraphic(GlyphUtils.createGlyph(FontAwesome.Glyph.COPY));
            copyAddressMenuItem.setOnAction(actionEvent -> {
                final ClipboardContent content = new ClipboardContent();
                content.putString(iPv4TableRow.getItem().getAddress());
                Clipboard.getSystemClipboard().setContent(content);
            });


            MenuItem copyNetmaskMenuItem = new MenuItem("Copy Netmask");
            copyNetmaskMenuItem.setGraphic(GlyphUtils.createGlyph(FontAwesome.Glyph.COPY));
            copyNetmaskMenuItem.setOnAction(actionEvent -> {
                final ClipboardContent content = new ClipboardContent();
                content.putString(iPv4TableRow.getItem().getNetmask());
                Clipboard.getSystemClipboard().setContent(content);
            });

            ContextMenu contextMenu = new ContextMenu(copyAddressMenuItem,copyNetmaskMenuItem);
            iPv4TableRow.setContextMenu(contextMenu);

            return iPv4TableRow;
        });

        addressesTableView.setItems(addressPool.getIPv4s());
        addressesTableView.setPrefWidth(300);
        addressesTableView.prefHeightProperty().bind(Bindings.size(addressesTableView.getItems()).multiply(30).add(30/*header*/ + 5 /*safety margin */));

        VBox rootPane = new VBox(5);
        rootPane.setPadding(new Insets(5));
        rootPane.getChildren().add(addressesTableView);

        getDialogPane().setContent(rootPane);
        getDialogPane().getButtonTypes().setAll(ButtonType.OK);
    }


}
