import socktype from "./socktype";
export default {
  ///////////////////////////////
  // APP (GLOBAL) openobject CLICK HANDLER
  //
  // Deal with a request to open an object (from main datatables mainly)
  // if it's an open object url that triggered here the url would be in the format of {host/open/[socktype integer]/[id integer]}, i.e.
  // http://localhost:8080/open/2/105
  // called from App.vue
  handleOpenObjectClick(vm, tid) {
    //expects extra data (tid) to be one of { type: [AYATYPE], id: [RECORDID] }
    //NOTE: for new objects all edit pages assume record ID 0 (or null) means create rather than open

    //for sake of ease of coding I'm going to assume null id also means make a new record intent
    //so I don't have to parse and decide constantly on forms for every control that has a open record link in it
    if (tid.id == null) {
      tid.id = 0;
    }

    if (tid.type && tid.id != null) {
      const isCustomerTypeUser =
        window.$gz.store.state.userType == 3 ||
        window.$gz.store.state.userType == 4;
      //if these come from route parameters they may well be strings
      tid.type = Number.parseInt(tid.type, 10);
      tid.id = Number.parseInt(tid.id, 10);
      if (isCustomerTypeUser) {
        switch (tid.type) {
          case socktype.NotifySubscription:
            vm.$router.push({
              name: "home-notify-subscription",
              params: { recordid: tid.id }
            });
            break;

          default:
            window.$gz.eventBus.$emit(
              "notify-warning",
              `Customer user: open-object-handler unable to open link - [type:${tid.type}, id:${tid.id}]`
            );
        }
      } else {
        switch (tid.type) {
          case socktype.Memo:
            vm.$router.push({
              name: "memo-edit",
              params: { recordid: tid.id }
            });
            break;
          case socktype.Customer:
            vm.$router.push({
              name: "customer-edit",
              params: { recordid: tid.id }
            });
            break;
          case socktype.CustomerNote:
            vm.$router.push({
              name: "customer-note-edit",
              params: { recordid: tid.id }
            });
            break;
          case socktype.HeadOffice:
            vm.$router.push({
              name: "head-office-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.User:
            //Is it an "Inside" user (staff or subcontractor)
            //or an "outside" user (customer or headoffice)
            //if key doesn't provide this then need to directly find out first before determining which form to redirect to
            if (tid.id != 0) {
              //lookup which one to open from server
              (async () => {
                try {
                  //shortcut for superuser, always id 1
                  if (tid.inside == undefined && tid.id == 1) {
                    tid.inside = true;
                  }
                  if (tid.inside == undefined) {
                    const res = await window.$gz.api.get(
                      "user/inside-type/" + tid.id
                    );
                    if (res.error) {
                      throw new Error(
                        window.$gz.errorHandler.errorToString(res, vm)
                      );
                    }
                    if (res.data) {
                      tid.inside = res.data;
                    }
                  }
                  if (tid.inside == true) {
                    vm.$router.push({
                      name: "adm-user",
                      params: { recordid: tid.id }
                    });
                  } else {
                    vm.$router.push({
                      name: "cust-user",
                      params: { recordid: tid.id }
                    });
                  }
                } catch (e) {
                  throw new Error(window.$gz.errorHandler.errorToString(e, vm));
                  //throw new Error(e);
                }
              })();
            }
            break;
          case socktype.NotifySubscription:
            vm.$router.push({
              name: "home-notify-subscription",
              params: { recordid: tid.id }
            });
            break;
          case socktype.FileAttachment:
            //lookup the actual type
            //then call this method again to do the actual open

            (async () => {
              try {
                const res = await window.$gz.api.get(
                  "attachment/parent/" + tid.id
                );

                if (res.error) {
                  throw new Error(
                    window.$gz.errorHandler.errorToString(res, vm)
                  );
                  // throw new Error(res.error);
                }
                if (res.data.id && res.data.id != 0) {
                  this.handleOpenObjectClick(vm, res.data);
                  return;
                }
              } catch (e) {
                //throw new Error(e);
                throw new Error(window.$gz.errorHandler.errorToString(e, vm));
              }
            })();

            break;

          case socktype.Translation:
            vm.$router.push({
              name: "adm-translation",
              params: { recordid: tid.id }
            });
            break;
          case socktype.Report:
            vm.$router.push({
              name: "sock-report-edit",
              params: { recordid: tid.id }
            });
            break;
          case socktype.Backup:
            vm.$router.push({
              name: "ops-backup"
            });
            break;

          case socktype.FormCustom:
            //all we have is the id, but need the formkey to open it
            (async () => {
              try {
                const res = await window.$gz.api.get(
                  "form-custom/form-key/" + tid.id
                );

                if (res.error) {
                  throw new Error(
                    window.$gz.errorHandler.errorToString(res, vm)
                  );
                }
                if (res && res.data) {
                  vm.$router.push({
                    name: "sock-customize",
                    params: {
                      formCustomTemplateKey: res.data
                    }
                  });
                  return;
                }
              } catch (e) {
                //throw new Error(e);
                throw new Error(window.$gz.errorHandler.errorToString(e, vm));
              }
            })();
            break;

          case socktype.Reminder:
            vm.$router.push({
              name: "reminder-edit",
              params: { recordid: tid.id }
            });
            break;
          case socktype.Review:
            vm.$router.push({
              name: "review-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.CustomerNotifySubscription:
            vm.$router.push({
              name: "cust-notify-subscription",
              params: { recordid: tid.id }
            });
            break;
          case socktype.OpsNotificationSettings:
            vm.$router.push({
              name: "ops-notification-settings"
            });
            break;
          case socktype.Integration:
            vm.$router.push({
              name: "adm-integration",
              params: { recordid: tid.id }
            });
            break;

          case socktype.Vendor:
            vm.$router.push({
              name: "vendor-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.Product:
            vm.$router.push({
              name: "product-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.Purchase:
            vm.$router.push({
              name: "purchase-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.VendorNotification:
            vm.$router.push({
              name: "vendor-notification-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.License:
            vm.$router.push({
              name: "license-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.TrialLicenseRequest:
            vm.$router.push({
              name: "trial-request-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.SubscriptionServer:
            vm.$router.push({
              name: "subscription-server-edit",
              params: { recordid: tid.id }
            });
            break;

          case socktype.GZCase:
            vm.$router.push({
              name: "gzcase-edit",
              params: { recordid: tid.id }
            });
            break;

          default:
            window.$gz.eventBus.$emit(
              "notify-warning",
              `open-object-handler: unknown [type:${tid.type}, id:${tid.id}]`
            );
        }
      }
    }
  },

  ///////////////////////////////////
  // WIRE UP MENU EVENTS
  //
  // called once from app.vue only
  //
  wireUpEventHandlers(vm) {
    const that = this;
    //expects extra data (tid) to be { type: [AYATYPE], id: [RECORDID] }
    window.$gz.eventBus.$on("openobject", function handleOpenObjectClickHandler(
      tid
    ) {
      that.handleOpenObjectClick(vm, tid);
    });
  }
  //new functions above here
};
