package be.iminds.ilabt.jfed.experimenter_gui.editor.rspec_validation;

import be.iminds.ilabt.jfed.rspec.model.ModelRspecType;
import be.iminds.ilabt.jfed.rspec.rspec_source.RequestRspecSource;
import be.iminds.ilabt.jfed.ui.javafx.dialogs.JFDialogs;
import javafx.stage.Window;

/**
 * Checks if rspec can be parsed into an FXModelRspec
 * <p/>
 * User: twalcari
 * Date: 1/22/2015
 * Time: 17:09
 */
public class ModelParsingValidator implements RspecValidator {
    @Override
    public RspecValidationResult validateRSpec(RspecCheckReason reason, RequestRspecSource requestRspecSource, Window dialogOwner) {
        if (requestRspecSource.getModelRspec(ModelRspecType.FX) == null) {
            String message = "The Rspec cannot be parsed! Please check your input for syntax errors.";

            if (requestRspecSource.getLastInvalidRspecException() != null) {
                message += "\n\n"
                        + requestRspecSource.getLastInvalidRspecException().getMessage();
            }

            JFDialogs.create()
                    .owner(dialogOwner)
                    .message(message)
                    .masthead("Fatal Parsing Error")
                    .title("Fatal Parsing Error")
                    .showException(requestRspecSource.getLastInvalidRspecException());

            return new RspecValidationResult(requestRspecSource, true/* == abort*/);
        } else {
            return null; //everything is OK
        }

    }
}
