/**
 * @mergeTarget
 * @module Src/Controllers
 */

import { CoreErrorCodes } from "@lisio/lisio-engine";
import { GoogleTranslationOptions } from "@lisio/lisio-profils";

/**
 * Class representing a statistics controller.\
 * It aims to centralize the logic of send statistics to server.\
 * If something want to send statistics to the server it has to use this class.\
 * To execute his responsability this class does :
 *  * Send to server when an user is created
 */
class StatisticsController {
  /**
   * Private attribute to store statistics server URL
   * @source
   */
  private _serverUrl: string;

  /**
   * @static
   * Private attribute to store instance of {@link StatisticsController | StatisticsController}
   * @source
   */
  private _originOfMessages: string;

  /**
   * Private attribute to store instance of {@link StatisticsController | StatisticsController}
   * @source
   */
  private static _current: StatisticsController;

  /**
   * @static
   * Getter for attribute {@link _current | _current}
   * @returns Returns _current attribute
   * @source
   */
  public static get current(): StatisticsController {
    return StatisticsController._current;
  }

  /**
   * Public method called everytime a user is created
   * @returns Returns nothing
   * @source
   */
  public incrementAddUserStatistics(): void {
    const fetchOptions: RequestInit = {
      headers: {
        "Content-Type": "application/json",
      },
      mode: "no-cors",
      method: "POST",
      body: JSON.stringify({
        col: "newUser",
        nd: this._originOfMessages,
      }),
    };
    fetch(this._serverUrl, fetchOptions).catch((error) => {
      console.log(error);
    });
  }

  /**
   * Public method called everytime a google translation lang is activated
   * @param {GoogleTranslationOptions} lang - Lang activated
   * @returns Returns nothing
   * @source
   */
  public addGoogleTransTracking(lang: GoogleTranslationOptions): void {
    const fetchOptions: RequestInit = {
      headers: {
        "Content-Type": "application/json",
      },
      mode: "no-cors",
      method: "POST",
      body: JSON.stringify({
        domain: this._originOfMessages,
        lang: lang.replace("_", "-"),
      }),
    };
    fetch("https://lisio-solution.com/gtt.php", fetchOptions).catch((error) => {
      console.log(error);
    });
  }

  /**
   * Constructor of class {@link StatisticsController | StatisticsController}
   * @param {string} serverUrl - URL of statistics server
   * @param {string} originOfMessages - Origin of client script
   * @throws If singleton already initialize.\
   * See {@link https://env-preprod-docs.lisio.fr/lisio-engine/enums/Src_Core.CoreErrorCodes.html#SINGLETON_NOT_UNIQUE | CoreErrorCodes.SINGLETON_NOT_UNIQUE}
   * @source
   */
  constructor(serverUrl: string, originOfMessages: string) {
    if (StatisticsController._current == undefined) {
      this._serverUrl = serverUrl;
      this._originOfMessages = originOfMessages;
      StatisticsController._current = this;
    } else {
      throw new Error(
        `Code : ${CoreErrorCodes.SINGLETON_NOT_UNIQUE}. Singleton already initialize`,
      );
    }
  }
}

export { StatisticsController };
