let VM_LOCAL = null;

//Calculate a reasonable time to show the alert based on the size of the message and some sane bounds
//https://ux.stackexchange.com/a/85898
function CalculateDelay(msg) {
  //Min 2 seconds max 8 seconds
  return Math.min(Math.max(msg.length * 50, 3000), 8000);
}

/////////////////////////////////
// Dialog, toast, notification
// utils and handlers
//
export default {
  ///////////////////////////////////
  // WIRE UP DIALOG EVENTS
  //
  // called once from app.vue only
  //
  wireUpEventHandlers(vm) {
    //###########################################
    //Notifications: pops up and slowly disappears
    //ACTUAL UI IN gznotify.vue
    //###########################################

    ///////////
    //ERROR
    window.$gz.eventBus.$on("notify-error", function handleNotifyWarn(
      msg,
      helpUrl
    ) {
      //log full message
      window.$gz.store.commit("logItem", "notify-error: " + msg);
      //trim really long message as it's likely useless beyond the first few lines (stack trace etc)
      msg = msg.substring(0, 600);
      vm.$root.$gznotify({
        message: msg,
        type: "error",
        timeout: CalculateDelay(msg),
        helpUrl: helpUrl
      });
    });

    ///////////
    //WARNING
    window.$gz.eventBus.$on("notify-warning", function handleNotifyWarn(
      msg,
      helpUrl
    ) {
      window.$gz.store.commit("logItem", "notify-warning: " + msg);
      msg = msg.substring(0, 600);
      vm.$root.$gznotify({
        message: msg,
        type: "warning",
        timeout: CalculateDelay(msg),
        helpUrl: helpUrl
      });
    });

    ///////////
    //INFO
    window.$gz.eventBus.$on("notify-info", function handleNotifyInfo(
      msg,
      helpUrl
    ) {
      window.$gz.store.commit("logItem", "notify-info: " + msg);
      msg = msg.substring(0, 600);
      vm.$root.$gznotify({
        message: msg,
        type: "info",
        timeout: CalculateDelay(msg),
        helpUrl: helpUrl
      });
    });

    ///////////
    //SUCCESS
    window.$gz.eventBus.$on("notify-success", function handleNotifySuccess(
      msg,
      helpUrl
    ) {
      vm.$root.$gznotify({
        message: msg,
        type: "success",
        timeout: CalculateDelay(msg),
        helpUrl: helpUrl
      });
    });

    VM_LOCAL = vm;
  },
  //###########################################
  //CONFIRMATION DIALOGS
  //ACTUAL UI IN gzconfirm.vue
  //###########################################
  /////////////////////////////////////
  // Are you sure you want to delete?
  //
  confirmDelete() {
    return VM_LOCAL.$root.$gzconfirm({
      message: window.$gz.translation.get("DeletePrompt"),
      yesButtonText: window.$gz.translation.get("Delete"),
      noButtonText: window.$gz.translation.get("Cancel"),
      type: "warning"
    });
  },
  /////////////////////////////////////
  // Are you sure you want to leave unsaved?
  //
  confirmLeaveUnsaved() {
    return VM_LOCAL.$root.$gzconfirm({
      message: window.$gz.translation.get("AreYouSureUnsavedChanges"),
      yesButtonText: window.$gz.translation.get("Leave"),
      noButtonText: window.$gz.translation.get("Cancel"),
      type: "warning"
    });
  },
  /////////////////////////////////////
  // Display LT message with wait for ok
  //
  displayLTErrorMessage(tKeyText, tKeyTitle = undefined) {
    return VM_LOCAL.$root.$gzconfirm({
      message: tKeyText ? window.$gz.translation.get(tKeyText) : "",
      title: tKeyTitle ? window.$gz.translation.get(tKeyTitle) : "",
      yesButtonText: window.$gz.translation.get("OK"),
      type: "error"
    });
  },
  /////////////////////////////////////
  // Display LT message with wait for ok
  //
  displayLTModalNotificationMessage(
    tKeyText,
    tKeyTitle = undefined,
    ttype = "info",
    tHelpUrl = undefined
  ) {
    return VM_LOCAL.$root.$gzconfirm({
      message: tKeyText ? window.$gz.translation.get(tKeyText) : "",
      title: tKeyTitle ? window.$gz.translation.get(tKeyTitle) : "",
      yesButtonText: window.$gz.translation.get("OK"),
      type: ttype,
      helpUrl: tHelpUrl
    });
  },
  /////////////////////////////////////
  // Custom confirmation
  //
  confirmGeneric(tKey, ttype = "info") {
    return VM_LOCAL.$root.$gzconfirm({
      message: window.$gz.translation.get(tKey),
      yesButtonText: window.$gz.translation.get("OK"),
      noButtonText: window.$gz.translation.get("Cancel"),
      type: ttype
    });
  },
  /////////////////////////////////////
  // Custom confirmation pre-translated
  //
  confirmGenericPreTranslated(msg, ttype = "info") {
    return VM_LOCAL.$root.$gzconfirm({
      message: msg,
      yesButtonText: window.$gz.translation.get("OK"),
      noButtonText: window.$gz.translation.get("Cancel"),
      type: ttype
    });
  },
  /////////////////////////////////////
  // Custom confirmation no translation
  // with all options available
  //
  displayNoTranslationModalNotificationMessage(
    tKeyText,
    tKeyTitle = undefined,
    ttype = "info",
    tHelpUrl = undefined
  ) {
    return VM_LOCAL.$root.$gzconfirm({
      message: tKeyText,
      title: tKeyTitle,
      yesButtonText: window.$gz.translation.get("OK"),
      type: ttype,
      helpUrl: tHelpUrl
    });
  }

  //new functions above here
};
