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

const sendUpdate = throttle(() => {
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
}, 300);

App.on("chargePhaserBeam", ({ id, beamId }) => {
  const sys = App.systems.find(s => s.id === id);
  sys.updateBeamState(beamId, "charging");
  //sys.updateBeamCharge(beamId, 1);
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
});
App.on("dischargePhaserBeam", ({ id, beamId }) => {
  const sys = App.systems.find(s => s.id === id);
  sys.updateBeamState(beamId, "discharging");
  //sys.updateBeamCharge(beamId, 0);
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
});
App.on("firePhaserBeam", ({ id, beamId }) => {
  const sys = App.systems.find(s => s.id === id);
  sys.fireBeam(beamId);
  pubsub.publish("notify", {
    id: uuid.v4(),
    simulatorId: sys.simulatorId,
    station: "Core",
    type: "Phasers",
    title: `Phasers Firing`,
    body: "",
    color: "info"
  });
  // App.handleEvent(
  //   {
  //     simulatorId: sys.simulatorId,
  //     title: `Phasers Firing`,
  //     component: "PhaserCore",
  //     body: null,
  //     color: "danger"
  //   },
  //   "addCoreFeed"
  // );
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
});
App.on("stopPhaserBeams", ({ id }) => {
  const sys = App.systems.find(s => s.id === id);
  sys && sys.stopBeams();
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
});
App.on("phaserArc", ({ id, beamId, arc }) => {
  App.systems.find(s => s.id === id).updateArc(arc);
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
});
App.on("setPhaserBeamCharge", ({ id, beamId, charge, noUpdate }) => {
  App.systems.find(s => s.id === id).updateBeamCharge(beamId, charge);
  sendUpdate();
});
App.on("setPhaserBeamHeat", ({ id, beamId, heat, noUpdate }) => {
  App.systems.find(s => s.id === id).updateBeamHeat(beamId, heat);
  sendUpdate();
});
App.on("coolPhaserBeam", ({ id, beamId }) => {
  const sys = App.systems.find(s => s.id === id);
  sys && sys.coolBeam(beamId);
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
});
App.on("applyPhaserCoolant", ({ id, beamId }) => {
  const phaser = App.systems.find(s => s.id === id);
  const beam = phaser.beams.find(b => b.id === beamId);
  phaser.setCoolant(Math.min(1, Math.max(0, phaser.coolant - 0.005)));
  beam.setHeat(Math.min(1, Math.max(0, beam.heat - 0.01)));
  if (phaser.coolant === 0 || beam.heat === 0) phaser.coolBeam(null);
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
});
App.on("setPhaserBeamCount", ({ id, beamCount }) => {
  App.systems.find(s => s.id === id).setBeams(beamCount);
  pubsub.publish(
    "phasersUpdate",
    App.systems.filter(s => s.type === "Phasers")
  );
});

@@ -962,32 +962,112 @@
 reBeam(beamId);%0A
+  const beam = sys.beams.find(b =%3E b.id === beamId);%0A  if (beam.charge %3E 0) %7B%0A  
   pubsub.publish
@@ -1083,16 +1083,18 @@
 , %7B%0A    
+  
 id: uuid
@@ -1100,16 +1100,18 @@
 d.v4(),%0A
+  
     simu
@@ -1140,16 +1140,18 @@
 Id,%0A    
+  
 station:
@@ -1163,16 +1163,18 @@
 e%22,%0A    
+ 
+ 
 type: %22P
@@ -1182,24 +1182,26 @@
 asers%22,%0A    
+ 
+ 
 title: %60Phas
@@ -1213,24 +1213,26 @@
 iring%60,%0A    
+  
 body: %22%22,%0A  
@@ -1229,16 +1229,18 @@
 dy: %22%22,%0A
+  
     colo
@@ -1253,16 +1253,18 @@
 fo%22%0A
+  
   %7D);%0A  
 // A
@@ -1263,228 +1263,9 @@
 ;%0A  
-// App.handleEvent(%0A  //   %7B%0A  //     simulatorId: sys.simulatorId,%0A  //     title: %60Phasers Firing%60,%0A  //     component: %22PhaserCore%22,%0A  //     body: null,%0A  //     color: %22danger%22%0A  //   %7D,%0A  //   %22addCoreFeed%22%0A  // );
+%7D
 %0A  p

Fix an issue where firing phasers when the charge is 0 triggers a notification.