'use strict';

import {
  ProvisioningProfileClient, ProvisioningProfileDto, ProvisioningProfileType, ProvisioningProfileUpdateDto
} from '../clients/metaApi/provisioningProfile.client';

/**
 * Implements a provisioning profile entity
 */
export default class ProvisioningProfile {
  
  private _data: ProvisioningProfileDto;
  private _provisioningProfileClient: ProvisioningProfileClient;
  
  /**
   * Constructs a provisioning profile entity
   * @param data provisioning profile data
   * @param provisioningProfileClient provisioning profile REST API client
   */
  constructor(data: ProvisioningProfileDto, provisioningProfileClient: ProvisioningProfileClient) {
    this._data = data;
    this._provisioningProfileClient = provisioningProfileClient;
  }

  /**
   * Returns profile id
   * @return profile id
   */
  get id(): string {
    return this._data._id;
  }

  /**
   * Returns profile name
   * @return profile name
   */
  get name(): string {
    return this._data.name;
  }

  /**
   * Returns profile version. Possible values are 4 and 5
   * @return {Number} profile version
   */
  get version(): number {
    return this._data.version;
  }

  /**
   * Returns profile status. Possible values are new and active
   * @return profile status
   */
  get status(): string {
    return this._data.status;
  }

  /**
   * Returns broker timezone name from Time Zone Database
   * @return broker timezone name
   */
  get brokerTimezone(): string {
    return this._data.brokerTimezone;
  }

  /**
   * Returns broker DST timezone name from Time Zone Database
   * @return broker DST switch timezone name
   */
  get brokerDSTSwitchTimezone(): string {
    return this._data.brokerDSTSwitchTimezone;
  }

  /**
   * Returns provisioning profile type
   * @return provisioning profile type
   */
  get type(): ProvisioningProfileType {
    return this._data.type;
  }

  /**
   * Reloads provisioning profile from API
   * @return promise resolving when provisioning profile is updated
   */
  async reload(): Promise<any> {
    this._data = await this._provisioningProfileClient.getProvisioningProfile(this.id);
  }

  /**
   * Removes provisioning profile. The current object instance should be discarded after returned promise resolves.
   * @return promise resolving when provisioning profile is removed
   */
  remove(): Promise<any> {
    return this._provisioningProfileClient.deleteProvisioningProfile(this.id);
  }

  /**
   * Uploads a file to provisioning profile.
   * @param fileName name of the file to upload. Allowed values are servers.dat for MT5 profile, broker.srv for
   * MT4 profile
   * @param file path to a file to upload or buffer containing file contents
   * @return promise which resolves when the file was uploaded
   */
  uploadFile(fileName: string, file: string | Buffer): Promise<any> {
    return this._provisioningProfileClient.uploadProvisioningProfileFile(this.id, fileName, file);
  }

  /**
   * Updates provisioning profile
   * @param profile provisioning profile update
   * @return promise resolving when provisioning profile is updated
   */
  async update(profile: ProvisioningProfileUpdateDto): Promise<any> {
    await this._provisioningProfileClient.updateProvisioningProfile(this.id, profile);
    await this.reload();
  }

}
