/**
 * Copyright © 2022 Anagog Ltd. All rights reserved.
 */

import { NativeModules } from "react-native";
const { JedAIModule } = NativeModules;

export class ApplicationEvent {
  private params: any;

  constructor(identifier: String) {
    this.params = { identifier };
  }

  /**
   * @deprecated
   */
  public get parameters(): Object {
    return JSON.parse(JSON.stringify(this.params));
  }

  /**
   * @deprecated
   */
  public get identifier(): string {
    return this.params['identifier'] || '';
  }

  /**
   * @deprecated
   */
  public set identifier(newIdentifier: string) {
    this.params['identifier'] = newIdentifier;
  }

  /**
   * @deprecated
   */
  public get textParameters(): Object {
    return this.params['textParameters'] || {};
  }

  /**
   * @deprecated
   */
  public get booleanParameters(): Object {
    return this.params['booleanParameters'] || {};
  }

  /**
   * @deprecated
   */
  public get numberParameters(): Object {
    return this.params['numericParameters'] || {};
  }

  /**
   * @deprecated
   */
  public get locationParameters(): Object {
    return this.params['locationParameters'] || {};
  }

  /**
   * Add text parameter to the event.
   * @param name Parameter name.
   * @param value `string` value.
   * @returns Self instance.
   */
  public addTextParameter(name: string, value: string): ApplicationEvent {
    if (!this.params['textParameters']) {
      this.params['textParameters'] = {};
    }
    this.params['textParameters'][name] = value;
    return this;
  }

  /**
   * Add boolean parameter to the event.
   * @param name Parameter name.
   * @param value `boolean` value.
   * @returns Self instance.
   */
  public addBooleanParameter(name: string, value: boolean): ApplicationEvent {
    if (!this.params['booleanParameters']) {
      this.params['booleanParameters'] = {};
    }
    this.params['booleanParameters'][name] = value;
    return this;
  }

  /**
   * Add number parameter to the event.
   * @param name Parameter name.
   * @param value `number` value.
   * @returns Self instance.
   */
  public addNumberParameter(name: string, value: number): ApplicationEvent {
    if (!this.params['numericParameters']) {
      this.params['numericParameters'] = {};
    }
    this.params['numericParameters'][name] = value;
    return this;
  }

  /**
   * Add location parameter to the event.
   * @param name Parameter name.
   * @param latitude Latitude.
   * @param longitude Longitude.
   * @returns Self instance.
   */
  public addLocationParameter(name: string, latitude: number, longitude: number): ApplicationEvent {
    if (!this.params['locationParameters']) {
      this.params['locationParameters'] = {};
    }
    this.params['locationParameters'][name] = { latitude, longitude };
    return this;
  }

  /**
   * Delete parameter from the event by it's name.
   * @param name Name of the parameter that should be removed.
   * @returns Self instance.
   */
  public deleteParameter(name: string): ApplicationEvent {
    if (this.params['textParameters']) {
      delete this.params['textParameters'][name];
    }
    if (this.params['booleanParameters']) {
      delete this.params['booleanParameters'][name];
    }
    if (this.params['numericParameters']) {
      delete this.params['numericParameters'][name];
    }
    if (this.params['locationParameters']) {
      delete this.params['locationParameters'][name];
    }
    return this;
  }

	/**
	 * Fire micromoment from your app.
	 */
  public fire(): Promise<void> {
    return JedAIModule.fireMicromoment(this.params);
  }

}