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

import be.iminds.ilabt.jfed.rspec.model.ModelRspecType;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXModelRspec;
import be.iminds.ilabt.jfed.rspec.rspec_source.RequestRspecSource;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Window;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: twalcari
 * Date: 1/22/2015
 * Time: 17:01
 */
public class HasExpiresValidator implements RspecValidator {
    private static final Logger LOG = LoggerFactory.getLogger(HasExpiresValidator.class);

    @Override
    public RspecValidationResult validateRSpec(RspecCheckReason reason, RequestRspecSource requestRspecSource, Window dialogOwner) {
        if (requestRspecSource.getModelRspec(ModelRspecType.FX).getExpiresString() != null) {
            String abortText = "Abort";
            String fixText = "Remove \"expires\" and continue";
            String ignoreText = "Keep as is and continue";

            boolean showIgnore = false;
            switch (reason) {
                case SWITCH_TO_VISUAL_EDITOR: {
                    return null; //no action needed
                }
                case RUN_EXPERIMENT: {
                    abortText = "Abort and continue editing";
                    ignoreText = "Ignore and Run Experiment Anyway";
                    fixText = "Remove \"expires\" and Run Experiment";
                    showIgnore = true;
                    break;
                }
                case CHECK_RSPEC: {
                    abortText = "Keep as is";
                    fixText = "Remove \"expires\"";
                    showIgnore = false;
                    break;
                }
                case OPENED_FILE: {
                    abortText = "Keep as is";
                    fixText = "Remove \"expires\"";
                    showIgnore = false;
                    break;
                }
            }

            ButtonType fixButtonType = new ButtonType(fixText, ButtonBar.ButtonData.YES);
            ButtonType ignoreButtonType = new ButtonType(ignoreText, ButtonBar.ButtonData.CANCEL_CLOSE);
            ButtonType abortButtonType = new ButtonType(abortText, ButtonBar.ButtonData.CANCEL_CLOSE);

            Alert alert =new Alert(Alert.AlertType.ERROR);
            alert.initOwner(dialogOwner);
            alert.setTitle("RSpec has \"expires\" attribute");
            alert.setContentText("Problem in RSpec:\n" +
                                    "This RSpec has the \"expires\" attribute. This attribute determines the expiration time of the slivers when sent to certain AM's.\n" +
                                    "jFed add and removes this attribute when needed, but when using this RSpec with older jFed versions, or other tools, it can cause problems.\n" +
                                    "It is advised to remove the \"expires\" attribute.\n\n" +
                                    "Do you want to remove the \"expires\" attribute?"
                    )            ;


            if (showIgnore)
                alert.getButtonTypes().setAll(fixButtonType, ignoreButtonType, abortButtonType);
            else
                alert.getButtonTypes().setAll(fixButtonType, abortButtonType);
            ButtonType chosenAction = alert.showAndWait().orElse(null);

            if (chosenAction == fixButtonType) {
                LOG.info("Removing expires attribute");

                FXModelRspec newModel = (FXModelRspec) requestRspecSource.getModelRspec(ModelRspecType.FX).copy();
                newModel.setExpires((String)null);

                return new RspecValidationResult(newModel);
            } else if (chosenAction == ignoreButtonType) {
                return new RspecValidationResult(requestRspecSource, false /* == ignore */);

            } else if (chosenAction == abortButtonType) {
                return new RspecValidationResult(requestRspecSource, true/* == abort*/);
            } else {
                throw new RuntimeException("Unknown chosen action: " + chosenAction);
            }
        }

        return null;
    }
}
