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

import be.iminds.ilabt.jfed.experiment.ExperimentController;
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.BooleanBinding;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Window;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.Glyph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: twalcari
 * Date: 3/26/2015
 * Time: 10:15
 */
public class ShareLanAction extends ExperimentAction {
    private static final Logger LOG = LoggerFactory.getLogger(ShareLanAction.class);


    private final FXRspecLink link;

    public ShareLanAction(Window parentWindow, ExperimentController experimentController, FXRspecLink link) {
        super(parentWindow, experimentController);
        this.link = link;
    }

    @Override
    public void run() {
        new SharedLanDialog().showDialog();
    }

    private class SharedLanDialog extends GridPane {
        private final Dialog<ButtonType> dialog;

        @FXML
        private TextField linkClientIdField;
        @FXML
        private TextField sharedLanNameField;
        @FXML
        private Glyph invalidNameGlyph;

        private final ButtonType shareAction;
        private final ButtonType unshareAction;


        SharedLanDialog() {
            FXMLUtil.injectFXML(this);

            shareAction =
                    new ButtonType("Share LAN", ButtonBar.ButtonData.OK_DONE);
            unshareAction =
                    new ButtonType("Unshare LAN", ButtonBar.ButtonData.OK_DONE);

            linkClientIdField.setText(link.getClientId());
            sharedLanNameField.setText("shared_lan_" + link.getClientId());

            dialog = new Dialog<>();
            dialog.initOwner(parentWindow);
            dialog.setTitle("Share/Unshare LAN" + link.getClientId());
            dialog.setHeaderText("Share/Unshare LAN " + link.getClientId());
            dialog.setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.SHARE_ALT, Color.GREEN));

            dialog.setResizable(false);
            dialog.getDialogPane().setContent(this);

            dialog.getDialogPane().getButtonTypes().setAll(shareAction, unshareAction, ButtonType.CANCEL);

            dialog.resultProperty().addListener(observable -> {
                if (dialog.getResult() == shareAction) {
                    onShareLanAction();
                } else if (dialog.getResult() == unshareAction) {
                    onUnshareLanAction();
                }
            });

            Node shareButton = dialog.getDialogPane().lookupButton(shareAction);
            Node unshareButton = dialog.getDialogPane().lookupButton(unshareAction);

            BooleanBinding isValidLanNameBinding = Bindings.createBooleanBinding(this::isValidLanName,
                    sharedLanNameField.textProperty());
            shareButton.disableProperty().bind(isValidLanNameBinding.not());
            unshareButton.disableProperty().bind(isValidLanNameBinding.not());
            invalidNameGlyph.visibleProperty().bind(isValidLanNameBinding.not());
        }

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

        private boolean isValidLanName() {
            sharedLanNameField.setText(sharedLanNameField.getText().trim());
            String sharedLanName = sharedLanNameField.getText();
            return sharedLanName.matches("[\\w-]*");
        }


        private void onUnshareLanAction() {
            assert isValidLanName();

            String sharedLanName = sharedLanNameField.getText();
            experimentController.unshareLan(link, sharedLanName);
        }

        private void onShareLanAction() {
            assert isValidLanName();

            String sharedLanName = sharedLanNameField.getText();
            experimentController.shareLan(link, sharedLanName);
        }
    }

}
