import App from "../app";
import { pubsub } from "../helpers/subscriptionManager.js";
import uuid from "uuid";

//Creating a new long range message
App.on(
  "sendLongRangeMessage",
  ({ id, simulatorId, message, crew, decoded, sender }) => {
    let system;
    if (id) {
      system = App.systems.find(s => s.id === id);
    }
    if (simulatorId) {
      system = App.systems.find(
        s => s.simulatorId === simulatorId && s.type === "LongRangeComm"
      );
    }
    const simulator = App.simulators.find(s => s.id === system.simulatorId);
    system &&
      system.createMessage(
        message.replace(/#SIM/gi, simulator.name),
        crew,
        decoded,
        sender
      );
    if (crew === false) {
      // Find the station(s) which has long range message sending
      const sim = App.simulators.find(s => s.id === system.simulatorId);
      const stations = sim.stations.filter(s =>
        s.cards.find(c => c.component === "LongRangeComm")
      );
      stations.forEach(s => {
        if (s.name !== sender) {
          pubsub.publish("notify", {
            id: uuid.v4(),
            simulatorId: system.simulatorId,
            station: s.name,
            title: `New Long Range Message Queued`,
            body: `Message composed by ${sender}`,
            color: "info"
          });
        }
      });
    }
    pubsub.publish(
      "longRangeCommunicationsUpdate",
      App.systems.filter(s => s.type === "LongRangeComm")
    );
  }
);
//Queued messages are sent
App.on("longRangeMessageSend", ({ id, message }) => {
  const sys = App.systems.find(s => s.id === id);
  sys.sendMessage(message);
  pubsub.publish("notify", {
    id: uuid.v4(),
    simulatorId: sys.simulatorId,
    station: "Core",
    title: `New Long Range Message`,
    body: ``,
    color: "info"
  });
  App.handleEvent(
    {
      simulatorId: sys.simulatorId,
      component: "LRCommCore",
      title: `New Long Range Message`,
      body: ``,
      color: "info"
    },
    "addCoreFeed"
  );
  pubsub.publish(
    "longRangeCommunicationsUpdate",
    App.systems.filter(s => s.type === "LongRangeComm")
  );
});
App.on("deleteLongRangeMessage", ({ id, message }) => {
  App.systems.find(s => s.id === id).deleteMessage(message);
  pubsub.publish(
    "longRangeCommunicationsUpdate",
    App.systems.filter(s => s.type === "LongRangeComm")
  );
});
App.on(
  "updateLongRangeDecodedMessage",
  ({ id, messageId, decodedMessage, a, f }) => {
    App.systems
      .find(s => s.id === id)
      .updateDecodedMessage(id, messageId, decodedMessage, a, f);
    pubsub.publish(
      "longRangeCommunicationsUpdate",
      App.systems.filter(s => s.type === "LongRangeComm")
    );
  }
);
App.on("updateLongRangeComm", ({ longRangeComm }) => {
  const lr = App.systems.find(s => s.id === longRangeComm.id);
  lr.update(longRangeComm);
  if (longRangeComm.locked) {
    pubsub.publish("notify", {
      id: uuid.v4(),
      simulatorId: lr.simulatorId,
      station: "Core",
      title: `Interception Signal Locked`,
      body: ``,
      color: "info"
    });
    App.handleEvent(
      {
        simulatorId: lr.simulatorId,
        title: `Interception Signal Locked`,
        body: null,
        component: "InterceptionCore",
        color: "info"
      },
      "addCoreFeed"
    );
  }
  pubsub.publish(
    "longRangeCommunicationsUpdate",
    App.systems.filter(s => s.type === "LongRangeComm")
  );
});

@@ -688,24 +688,25 @@
 er%0A      );%0A
+%0A
     if (crew
@@ -802,94 +802,27 @@
 st s
-im = App.simulators.find(
+tation
 s =
-%3E
  s
-.id === system.simulatorId);%0A      const stations = sim
+imulator
 .sta
@@ -1156,24 +1156,24 @@
 ge Queued%60,%0A
-
             
@@ -1267,24 +1267,500 @@
 %7D%0A      %7D);%0A
+    %7D else %7B%0A      const stations = simulator.stations.filter(s =%3E%0A        s.cards.find(c =%3E c.component === %22CommDecoding%22)%0A      );%0A      stations.forEach(s =%3E %7B%0A        if (s.name !== sender) %7B%0A          pubsub.publish(%22notify%22, %7B%0A            id: uuid.v4(),%0A            simulatorId: system.simulatorId,%0A            station: s.name,%0A            title: %60New Long Range Message%60,%0A            body: %60From $%7Bsender%7D%60,%0A            color: %22info%22%0A          %7D);%0A        %7D%0A      %7D);%0A
     %7D%0A    pu

Add crew notifications for long range comm and sensors