"use strict";
const NodeHelper = require("node_helper");
const BvgFetcher = require("./BvgFetcher");
const lineColors = require("vbb-line-colors");

module.exports = NodeHelper.create({
  start: function () {
    this.departuresFetchers = [];
  },

  createFetcher: async function (config) {
    let fetcher;

    if (typeof this.departuresFetchers[config.stationId] === "undefined") {
      fetcher = new BvgFetcher(config);
      this.departuresFetchers[config.stationId] = fetcher;
      this.sendInit(fetcher);

      try {
        let stationName = await fetcher.getStationName();
        console.log(
          "Transportation fetcher for station " + stationName + " created. (Station ID: " + fetcher.getStationId() + ")"
        );
      } catch (error) {
        console.error(error); 
      }

    } else {
      fetcher = this.departuresFetchers[config.stationId];
      this.sendInit(fetcher);

      try {
        let stationName = await fetcher.getStationName();
        console.log(
          "Using existing transportation fetcher for station " + res + " (Station ID: " + fetcher.getStationId() + ")"
        );
      } catch (error) {
        console.error(error)
      }
    }

    await this.getDepartures(fetcher.getStationId());
  },

  sendInit: async function (fetcher) {
    try {
      let stationName = fetcher.getStationName();

      this.sendSocketNotification("FETCHER_INIT", {
        stationId: fetcher.getStationId(),
        stationName: stationName,
      });
    } catch (error) {
      console.error("Error initializing fetcher: ");
      console.error(error);
    }
  },

  getDepartures: async function (stationId) {
    try {
      let departuresData = await this.departuresFetchers[stationId].fetchDepartures();

      this.pimpDeparturesArray(departuresData.departuresArray);
      this.sendSocketNotification("DEPARTURES", departuresData);
    } catch (error) {
      console.log("Error while fetching departures (for Station ID " + stationId + "): " + error);
      // Add stationId to error for identification in the main instance
      error.stationId = stationId;
      error.message = e;
      this.sendSocketNotification("FETCH_ERROR", error);
    }
  },

  pimpDeparturesArray: function (departuresArray) {
    let currentProperties = {};

    departuresArray.forEach((current) => {
      currentProperties = this.getLineProperties(current);

      //if (!this.config.marqueeLongDirections) {
      //    current.direction = this.trimDirectionString(current.direction);
      //}
      current.bgColor = currentProperties.bgColor;
      current.fgColor = currentProperties.fgColor;
      current.cssClass = currentProperties.cssClass;
    });

    return departuresArray;
  },

  getLineProperties: function (product) {
    let properties = {
      bgColor: "#333",
      fgColor: "#FFF",
      cssClass: "",
    };

    let type = product.type;
    let lineType = product.line;
    let name = product.name;
    let colors = {};

    switch (type) {
      case "suburban":
        colors = lineColors.suburban[name];
        properties.cssClass = "sbahnsign";
        break;
      case "subway":
        colors = lineColors.subway[name];
        properties.cssClass = "ubahnsign";
        break;
      case "bus":
        colors.bg = "#B60079";
        colors.fg = "#FFF";
        properties.cssClass = "bussign";
        break;
      case "tram":
        colors = lineColors.tram[name];
        properties.cssClass = "tramsign";
        break;
      case "regional":
        colors = lineColors.regional[name];
        properties.cssClass = "dbsign";
        break;
      case "express":
        if (lineType === "LOCOMORE") {
          colors.bg = "#E5690B";
          colors.fg = "#3E1717";
          properties.cssClass = "locsign";
        } else {
          properties.cssClass = "expresssign";
        }
        break;
    }

    // Change default values if we changed them
    if ("bg" in colors) {
      properties.bgColor = colors.bg;
    }

    if ("fg" in colors) {
      properties.fgColor = colors.fg;
    }

    return properties;
  },

  socketNotificationReceived: function (notification, payload) {
    if (notification === "GET_DEPARTURES") {
      this.getDepartures(payload);
    }

    if (notification === "CREATE_FETCHER") {
      this.createFetcher(payload);
    }

    if (notification === "STATION_NAME_MISSING_AFTER_INIT") {
      this.sendInit(this.departuresFetchers[payload]);
    }
  },
});

@@ -789,10 +789,9 @@
 or);
- 
 %0A
+
     
@@ -1054,19 +1054,27 @@
 ion %22 + 
-res
+stationName
  + %22 (St
@@ -1296,32 +1296,32 @@
 er) %7B%0A    try %7B%0A
-
       let statio
@@ -1327,16 +1327,22 @@
 onName =
+ await
  fetcher

Fix stationName not being display in headline