/*!
 * Copyright (c) 2015-2019 Cisco Systems, Inc. See LICENSE file.
 */

import {WebexPlugin} from '@webex/webex-core';
import {deprecated} from '@webex/common';

import Batcher from './batcher';
import ClientMetricsBatcher from './client-metrics-batcher';
import CallDiagnosticEventsBatcher from './call-diagnostic-events-batcher';

const Metrics = WebexPlugin.extend({
  children: {
    batcher: Batcher,
    clientMetricsBatcher: ClientMetricsBatcher,
    callDiagnosticEventsBatcher: CallDiagnosticEventsBatcher
  },

  namespace: 'Metrics',

  @deprecated('Metrics#sendUnstructured() is deprecated; please use Metrics#submit()')
  sendUnstructured(key, value) {
    return this.submit(key, value);
  },

  submit(key, value) {
    return this.batcher.request(Object.assign({key}, value));
  },

  /**
   * This corresponds to #sendSemiStructured() in the deprecated metrics handler
   * @param {string} eventName
   * @param {Object} props
   * @param {string} preLoginId
   * @returns {Object} HttpResponse object
   */
  submitClientMetrics(eventName, props, preLoginId) {
    const payload = {metricName: eventName};

    if (props.tags) {
      payload.tags = props.tags;
    }
    if (props.fields) {
      payload.fields = props.fields;
    }
    if (props.type) {
      payload.type = props.type;
    }
    if (props.context) {
      payload.context = props.context;
    }

    payload.timestamp = Date.now();

    if (preLoginId) {
      const _payload = {
        metrics: [payload]
      };

      // Do not batch these because pre-login events occur during onboarding, so we will be partially blind
      // to users' progress through the reg flow if we wait to persist pre-login metrics for people who drop off because
      // their metrics will not post from a queue flush in time
      return this.postPreLoginMetric(_payload, preLoginId);
    }

    return this.clientMetricsBatcher.request(payload);
  },

  /**
   * Issue request to alias a user's pre-login ID with their CI UUID
   * @param {string} preLoginId
   * @returns {Object} HttpResponse object
   */
  aliasUser(preLoginId) {
    return this.request({
      method: 'POST',
      api: 'metrics',
      resource: 'clientmetrics',
      headers: {
        'x-prelogin-userid': preLoginId
      },
      body: {},
      qs: {
        alias: true
      }
    });
  },

  postPreLoginMetric(payload, preLoginId) {
    return this.webex.credentials.getClientToken().then((token) =>
      this.request({
        method: 'POST',
        api: 'metrics',
        resource: 'clientmetrics-prelogin',
        headers: {
          authorization: token.toString(),
          'x-prelogin-userid': preLoginId
        },
        body: payload
      }));
  },

  submitCallDiagnosticEvents(payload) {
    const event = {
      type: 'diagnostic-event',
      eventPayload: payload
    };

    return this.callDiagnosticEventsBatcher.request(event);
  }
});

export default Metrics;
