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

import be.iminds.ilabt.jfed.rspec.model.ModelRspecType;
import be.iminds.ilabt.jfed.rspec.parser.RspecCompare;
import be.iminds.ilabt.jfed.rspec.rspec_source.RequestRspecSource;
import be.iminds.ilabt.jfed.ui.javafx.dialogs.JFDialogs;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Window;

import java.util.Objects;
import java.util.Optional;

/**
 * User: twalcari
 * Date: 1/22/2015
 * Time: 17:02
 */
public class CheckIfLosingDataValidator implements RspecValidator {
    private static final ButtonType MORE_DETAILS =
            new ButtonType("More Details...", ButtonBar.ButtonData.HELP);

//    {
//                {
//                    lock();
//                }
//
//                public String toString() {
//                    return "EditorRibbonTab.MORE_DETAILS";
//                }
//            };


    @Override
    public RspecValidationResult validateRSpec(RspecCheckReason reason, RequestRspecSource requestRspecSource, Window dialogOwner) {
        //check if we are losing data
        Boolean isLosingData = requestRspecSource.isLosingData(ModelRspecType.FX);

        if (isLosingData == null || isLosingData) {
            //we are losing data! warn the user.

            Optional<ButtonType> response;
            if (reason.equals(RspecCheckReason.SWITCH_TO_VISUAL_EDITOR) || reason.equals(RspecCheckReason.RUN_EXPERIMENT))
                response =
                        JFDialogs.create().owner(dialogOwner)
                                .message(
                                        "The Rspec contains directives that are not supported by the jFed Experimenter toolkit! " +
                                                (Objects.equals(reason, RspecCheckReason.SWITCH_TO_VISUAL_EDITOR)
                                                        ? "Changing to the graphical editor will incur data loss. "
                                                        : "") +
                                                "Are you sure you want to continue?")
                                .title("Data loss imminent!")
                                .masthead("Data loss imminent!")
                                .buttonTypes(ButtonType.YES, MORE_DETAILS, ButtonType.NO)
                                .showWarning();

            else { //reason is CHECK_RSPEC or OPENED_FILE
                response =
                        JFDialogs.create().owner(dialogOwner)
                                .message(
                                        "The Rspec contains directives that are not supported by the jFed Experimenter toolkit! " +
                                                "Note that because of this, switching to the graphical editor will incur data loss.")
                                .title("Data loss imminent!")
                                .masthead("Data loss imminent!")
                                .buttonTypes(MORE_DETAILS, ButtonType.OK)
                                .showWarning();
            }

            if (response.isPresent() && response.get() == MORE_DETAILS) {
                JFDialogs.create().owner(dialogOwner)
                        .message(
                                RspecCompare.differencesListToString(
                                        requestRspecSource.getLosingDataDifferences(ModelRspecType.FX)
                                )
                        )
                        .title("Data loss details").buttonTypes(ButtonType.CLOSE)
                        .showInformation();
                System.out.println("Differences:\n" +
                        RspecCompare.differencesListToString(
                                requestRspecSource.getLosingDataDifferences(ModelRspecType.FX)));
            }

            if (reason.equals(RspecCheckReason.SWITCH_TO_VISUAL_EDITOR) || reason.equals(RspecCheckReason.RUN_EXPERIMENT)) {
                boolean doAbort = response.isPresent() && response.get() != ButtonType.YES;
                return new RspecValidationResult(requestRspecSource, doAbort);
            }else{
                return new RspecValidationResult(requestRspecSource, false /* == ignore */);
            }
        } else
            return null;
    }
}
