import * as Messages from "/services/messages/messages.js";

let capturerModel;

export function getCapturerModel() {
  if (capturerModel == null) {
    capturerModel = new CapturerModel();
  }
  return capturerModel;
}

class CapturerModel {
  constructor() {}

  async getCapturer(id) {
    if (this.capturers == null) await this.refreshCapturers();
    let capturerToGet;
    this.capturers.forEach((capturer) => {
      if (capturer.ID == id) capturerToGet = capturer;
    });
    return capturerToGet;
  }

  async getCapturerByUserID(userID) {
    if (this.capturers == null) await this.refreshCapturers();
    let capturerToGet;
    this.capturers.forEach((capturer) => {
      if (capturer.UserID == userID) capturerToGet = capturer;
    });
    return capturerToGet;
  }

  async getCapturers() {
    if (this.capturers == null) {
      try {
        const response = await fetch("/api/Capturer/", {
          method: "GET",
          headers: new Headers({
            "XSRF-Token": this.current_user.xsrftoken,
          }),
        });
        if (response.status !== 200) {
          throw new Error(
            `Capturers could not be fetched (status ${response.status})`
          );
        }
        this.capturers = await response.json();
      } catch (e) {
        Messages.Show("is-warning", e.message);
        console.error(e);
      }
    }
    return this.capturers;
  }

  async saveCapturer(method, ID, UserID, Name) {
    try {
      const response = await fetch("/api/Capturer/" + ID, {
        method: method,
        headers: new Headers({
          "XSRF-Token": this.current_user.xsrftoken,
        }),
        body: JSON.stringify({
          ID: ID,
          UserID: UserID,
          Name: Name,
        }),
      });
      if (response.status !== 200) {
        throw new Error(
          `Capturer could not be saved (status ${response.status})`
        );
      }
      this.refreshCapturers();
      return await response.json();
    } catch (e) {
      Messages.Show("is-warning", e.message);
      console.error(e);
      return;
    }
  }

  async deleteCapturer(ID) {
    try {
      const response = await fetch("/api/Capturer/" + ID, {
        method: "delete",
        headers: new Headers({
          "XSRF-Token": this.current_user.xsrftoken,
        }),
      });
      if (response.status !== 200) {
        throw new Error(
          `Capturer could not be deleted (status ${response.status})`
        );
      }
      this.refreshCapturers();
    } catch (e) {
      Messages.Show("is-warning", e.message);
      console.error(e);
    }
  }

  async refreshCapturers() {
    this.capturers = null;
    await this.getCapturers();
  }

  async addCapturerToDesk(CapturerID, DeskRoundID) {
    try {
      const response = await fetch("/api/CapturerDeskRound/", {
        method: "POST",
        headers: new Headers({
          "XSRF-Token": this.current_user.xsrftoken,
        }),
        body: JSON.stringify({
          CapturerID: CapturerID,
          DeskRoundID: DeskRoundID,
        }),
      });
      if (response.status !== 200) {
        throw new Error(
          `DeskRound could not be added to capturer (status ${response.status})`
        );
      }
      await response.json();
      this.refreshCapturers();
    } catch (e) {
      Messages.Show("is-warning", e.message);
      console.error(e);
      return;
    }
  }

  async removeCapturerFromDesk(CapturerID, DeskRoundID) {
    try {
      const response = await fetch("/api/CapturerDeskRound/", {
        method: "delete",
        headers: new Headers({
          "XSRF-Token": this.current_user.xsrftoken,
        }),
        body: JSON.stringify({
          CapturerID: CapturerID,
          DeskRoundID: DeskRoundID,
        }),
      });
      if (response.status !== 200) {
        throw new Error(
          `DeskRound could not be removed from capturer (status ${response.status})`
        );
      }
      this.refreshCapturers();
    } catch (e) {
      Messages.Show("is-warning", e.message);
      console.error(e);
    }
  }
}