// import * as Juce from "juce-framework-frontend";
// import reportWebVitals from "../reportWebVitals";

const MidNoteDataReceiver_eventId = "midNoteData";
export default class MidNoteDataReceiver {
  constructor() {
    this.notes = [];
    this.input_pitch = 0;
    this.output_pitch = 0;
    let self = this;
    this.midNoteDataRegistrationId = window.__JUCE__.backend.addEventListener(
      MidNoteDataReceiver_eventId,
      (event) => {
        self.notes = event.notes;
        self.input_pitch = event.input_pitch;
        self.output_pitch = event.output_pitch;
        console.log("in: " + self.input_pitch);
        console.log("out: " + self.output_pitch);
        // reportWebVitals(console.log(event));
        // fetch(Juce.getBackendResourceAddress("midNoteData.json"))
        //   .then((response) => response.text())
        //   .then((text) => {
        //     const data = JSON.parse(text);
        //     self.notes = data.notes;
        //   });
      }
    );
  }

  getNotes() {
    return this.notes;
  }

  frequencytoMidi(frequency) {
    return 69 + 12 * Math.log2(frequency / 440);
  }

  getAutotuneNote() {
    const midi = this.frequencytoMidi(this.output_pitch);
    return Math.round(midi % 12);
  }
  getInputCents() {
    const midi = this.frequencytoMidi(this.input_pitch);
    return Math.round((midi - Math.round(midi)) * 100);
  }
  getOutputCents() {
    const midi = this.frequencytoMidi(this.output_pitch);
    return Math.round((midi - Math.round(midi)) * 100);
  }

  unregister() {
    window.__JUCE__.backend.removeEventListener(this.midNoteDataRegistrationId);
  }
}
