<script>

export default {
  name: "MapTooltipsMixin",

  data () {
    return {
      tooltipDefaultStyle: { "max-width": "50ex"}
    }
  },

  methods: {

    getTooltip (args) {
      if (args?.layer?.constructor?.tooltip) {
        return args.layer.constructor.tooltip(args);
      } else if (args?.layer?.id == "seqrp" || args?.layer?.id == "seqfp") {
        return this.sequencePointsTooltip(args);
      } else if (args?.layer?.id == "log") {
        return this.eventLogTooltip(args);
      } else if (args?.layer?.id == "pplp" || args?.layer?.id == "pslp") {
        return this.preplotPointsTooltip(args);
      } else if (args?.layer?.id == "ppll" || args?.layer?.id == "psll") {
        return this.preplotLinesTooltip(args);
      } else if (args?.layer?.id == "planl") {
        return this.plannedLinesTooltip(args);
      } else if (args?.layer?.id == "seqrl" || args?.layer?.id == "seqfl") {
        return this.sequenceLinesTooltip(args);
      } else if (args?.layer?.id == "navp") {
        return this.vesselTrackPointsTooltip(args);
      }
    },

    preplotPointsTooltip (args) {
      const p = args?.object;
      if (p) {
        let html = "Preplot<br/>\n";

        if ("sailline" in p) {
          // If there is a "sailline" attribute, this is actually a source line
          // "i" is the source line number, "sailline" the sail line number.
          html += `S${String(p.i).padStart(4, "0")}<br/>\n`;
          html += `V${String(p.sailline).padStart(4, "0")}<br/>\n`;
        } else {
          html += `V${String(p.i).padStart(4, "0")}<br/>\n`;
        }
        html += `P${String(p.j).padStart(4, "0")}<br/>\n`;

        if (p.sailline_ntba) {
          html += `<b>Line <abbr title="Not to be acquired">NTBA</abbr></b><br/>\n`;
        }

        if (p.ntba) {
          html += `<b>Point <abbr title="Not to be acquired">NTBA</abbr></b><br/>\n`;
        }

        return {html, style: this.tooltipDefaultStyle};
      }
    },

    preplotLinesTooltip (args) {
        const p = args?.object?.properties;
        if (p) {
          const lineType = args.layer.id == "psll" ? "Sailline" : "Source line";
          const direction = p.incr ? "▲" : "▼";
          let html = "";

          html += `L${String(p.line).padStart(4, "0")} ${direction}<br/>\n`;
          html += `${lineType}<br/>\n`;

          if (p.ntba) {
            html += "<b>Not to be acquired (NTBA)</b><br/>\n";
          }

          if (p.remarks) {
            html += `<hr/>\n${this.$root.markdown(p.remarks)}\n`;
          }

          return {html, style: this.tooltipDefaultStyle};
        }
    },

    sequenceLinesTooltip (args) {
        let type;
        switch(args.layer.id) {
          case "seqrl":
            type = "Raw";
            break;
          case "seqfl":
            type = "Final";
            break;
        }

        const p = args?.object?.properties;
        if (p) {
          let html = `Sequence ${p.sequence} (${type})<br/>\n`;
          html += `Line <b>${p.line}</b><br/>\n`;
          html += `${p.num_points} points (${p.missing_shots ? (p.missing_shots + " missing") : "None missing"})<br/>\n`;
          html+= `${(p.length??0).toFixed(0)} m ${(p.azimuth??0).toFixed(1)}°<br/>\n`;
          html += `${p.duration}<br/>\n`;
          html += `<b>${p.fsp}</b> @ ${(new Date(p.ts0))?.toISOString()}<br/>\n`;
          html += `<b>${p.lsp}</b> @ ${(new Date(p.ts1))?.toISOString()}<br/>\n`;
          if (p.ntbp) {
            html += "<b>Not to be processed</b><br/>\n";
          } else if (p.pending) {
            html += "<b>Pending</b><br/>\n";
          }
          html += "<hr/><br/>\n";
          html += this.$root.markdown(p.remarks);

          return {html, style: this.tooltipDefaultStyle};
        }
    },

    sequencePointsTooltip (args) {

      const p = args?.object;

      if (p) {

        let html = "";

        if (p.udv == 2) { // FIXME Must change this to something more meaningful
          // This is a shot info record:

          html += `S${p.i.toString().padStart(3, "0")} ${p.j}</br>\n`;
          html += `<small>${new Date(Number(p.ts)).toISOString()}</small><br/>\n`;
          html += `Δi: ${p.εj.toFixed(2)} m / Δj: ${p.εi.toFixed(2)} m<br/>\n`;
          html += `<hr/>\n`;
          if (p.nofire) {
            html += `<b>No fire: ${p.nofire} guns</b><br/>\n`;
          }
          if (p.autofire) {
            html += `<b>Autofire: ${p.autofire} guns</b><br/>\n`;
          }
          html += `P: ${p.press_μ} psi ±${p.press_σ} psi (R=${p.press_R})<br/>\n`;
          html += `Δ: ${p.delta_μ} ms ±${p.delta_σ} ms (R=${p.delta_R})<br/>\n`;
          html += `D: ${p.depth_μ} m ±${p.depth_σ} m (R=${p.depth_R})<br/>\n`;
          html += `F: ${p.fill_μ} ms ±${p.fill_σ} ms (R=${p.fill_R})<br/>\n`;
          html += `W: ${p.delay_μ} ms ±${p.delay_σ} ms (R=${p.delay_R})<br/>\n`;

          return {html, style: this.tooltipDefaultStyle};
        } else if (p.udv == 3) {
          // A final points record

          html += `S${p.i.toString().padStart(3, "0")} ${p.j} (final)</br>\n`;
          html += `<small>${new Date(Number(p.ts)).toISOString()}</small><br/>\n`;
          html += `Δi: ${p.εj.toFixed(2)} m / Δj: ${p.εi.toFixed(2)} m (final−preplot)<br/>\n`;
          html += `δi: ${p.co_j.toFixed(2)} m / δj: ${p.co_i.toFixed(2)} m (final−raw)<br/>\n`;

          return {html, style: this.tooltipDefaultStyle};
        }
      }
      console.log("no tooltip");

    },

    eventLogTooltip (args) {
      const p = args?.object?.properties;
      if (p) {
        let html = "";
        if (p.sequence && p.point) {
          html += `S${p.sequence.toString().padStart(3, "0")} ${p.point}</br>\n`;
        }
        html += `<small>${p.tstamp}</small><br/>\n`;
        html += `<span>${p.remarks}</span>`;
        if (p.labels?.length) {
          html += `</br>\n<small><i>${p.labels.join(", ")}</i></small>`;
        }

        return {html, style: this.tooltipDefaultStyle};
      }
    },

    plannedLinesTooltip (args) {
      const p = args?.object;
      if (p) {
        const duration = `${(p.duration?.hours??0).toString().padStart(2, "0")}:${(p.duration?.minutes??0).toString().padStart(2, "0")}`;
        const Δt = ((new Date(p.ts1)).valueOf() - (new Date(p.ts0)).valueOf()) / 1000; // seconds
        const speed = (p.length / Δt) * 3.6 / 1.852; // knots

        let html = `Planned sequence <b>${p.sequence}</b></br>\n` +
          `Line <b>${p.line}</b> – ${p.name}</br>\n` +
          `${p.num_points} Points</br>\n` +
          `${p.length.toFixed(0)} m ${p.azimuth.toFixed(2)}°</br>\n` +
          `${duration} @ ${speed.toFixed(1)} kt</br>\n` +
          `<b>${p.fsp}</b> @ ${(new Date(p.ts0))?.toISOString()}</br>\n` +
          `<b>${p.lsp}</b> @ ${(new Date(p.ts1))?.toISOString()}`;

          if (p.remarks) {
            html += `</br>\n<hr/>${this.$root.markdown(p.remarks)}`;
          }

        return {html, style: this.tooltipDefaultStyle};
      }
    },

    vesselTrackPointsTooltip (args) {
      const p = args.object;

      if (p) {
        let html = `${p.vesselName}<br/>\n`
          + `${p.tstamp}<br/>\n`
          + `BSP ${(p.speed??0).toFixed(1)} kt CMG ${(p.cmg??0).toFixed(1).padStart(5, "0")}° HDG ${(p.bearing??0).toFixed(1).padStart(5, "0")}° DPT ${(p.waterDepth??0).toFixed(1)} m<br/>\n`
          + `${p.lineStatus}<br/>\n`;

        if (p.guns) {
          console.log(p);
          const pressure = p.guns.map( i => i[11] ); // 11 is gun pressure
          const μpress = d3a.mean(pressure);
          const σpress = d3a.deviation(pressure);

          if (p.lineStatus && p.lineStatus != "offline") {
            html += `${p.lineName}<br/>\n`
              + `S: ${p._sequence} L: ${p._line} P: ${p.shot}<br/>`
              + `Source ${p.src_number} `
              + ((p.trg_mode && p.trg_mode != "external") ? `<b>${p.trg_mode.toUpperCase()} TRIGGER</b> ` : "")
              + `<small>FSID ${p.fsid}</small> <small>mask ${p.mask}</small><br/>\n`
              + `Δ ${(p.avg_delta??0).toFixed(3)} ms ±${(p.std_delta??0).toFixed(3)} ms<br/>\n`
              + `${(μpress??0).toFixed(0)} psi ±${(σpress??0).toFixed(0)} psi / ${(p.volume??0).toFixed(0)} in³<br/>\n`
              + `along ${(p.inline??0).toFixed(1)} m / across ${(p.crossline??0).toFixed(1)} m<br/>\n`;
          } else {
            // Soft start?
            html +=
                `Source ${p.src_number} `
              + ((p.trg_mode && p.trg_mode != "external") ? `<b>${p.trg_mode.toUpperCase()} TRIGGER</b> ` : "")
              + `<small>mask ${p.mask}</small><br/>\n`
              + `Δ ${(p.avg_delta??0).toFixed(3)} ms ±${(p.std_delta??0).toFixed(3)} ms<br/>\n`
              + `${(p.manifold??0).toFixed(0)} psi / ${(p.volume??0).toFixed(0)} in³<br/>\n`
              + `along ${(p.inline??0).toFixed(1)} m / across ${(p.crossline??0).toFixed(1)} m<br/>\n`;
          }
        }

        return {html, style: this.tooltipDefaultStyle};
      }
    },



  }

}
</script>
