'use strict';

const Promise = require('promise');
const translatorService = require('../../clients/translator/MsftTranslator');

/**
 * @typedef {type: string, coordinates: number[][], properties: {edges: string[], messageid: string, createdtime: string, sentiment: number, title: string, originalSources: string[], sentence: string, language: string, source: string, properties: {retweetCount: number, fatalaties: number, userConnecionCount: number, actor1: string, actor2: string, actor1Type: string, actor2Type: string, incidentType: string, allyActor1: string, allyActor2: string, title: string, link: string, originalSources: string[]}, fullText: string}} Feature
 */

/**
 * @param {site: string, originalSource: string, coordinates: number[], mainTerm: string, filteredEdges: string[], langCode: string, limit: number, offset: number, fromDate: string, toDate: string, sourceFilter: string[], fulltextTerm: string} args
 * @returns {Promise.<{runTime: string, type: string, bbox: number[], features: Feature[]}>}
 */
function byLocation(args, res) { // eslint-disable-line no-unused-vars
}

/**
 * @param {site: string, originalSource: string, bbox: number[], mainTerm: string, filteredEdges: string[], langCode: string, limit: number, offset: number, fromDate: string, toDate: string, sourceFilter: string[], fulltextTerm: string} args
 * @returns {Promise.<{runTime: string, type: string, bbox: number[], features: Feature[]}>}
 */
function byBbox(args, res) { // eslint-disable-line no-unused-vars
}

/**
 * @param {site: string, originalSource: string, filteredEdges: string[], langCode: string, limit: number, offset: number, fromDate: string, toDate: string, sourceFilter: string[], fulltextTerm: string} args
 * @returns {Promise.<{runTime: string, type: string, bbox: number[], features: Feature[]}>}
 */
function byEdges(args, res) { // eslint-disable-line no-unused-vars
}

/**
 * @param {{site: string, messageId: string, dataSources: string[], langCode: string}} args
 * @returns {Promise.<Feature>}
 */
function event(args, res) { // eslint-disable-line no-unused-vars
}

/**
 * @param {{sentence: string, fromLanguage: string, toLanguage: string}} args
 * @returns {Promise.<{originalSentence: string, translatedSentence: string}>}
 */
function translate(args, res) { // eslint-disable-line no-unused-vars
  return new Promise((resolve, reject) => {
    translatorService.translate(args.sentence, args.fromLanguage, args.toLanguage)
          .then(result => resolve({ translatedSentence: result.translatedSentence, originalSentence: args.sentence }))
          .catch(err => reject(err));
  });
}

/**
 * @param {{words: string[], fromLanguage: string, toLanguage: string}} args
 * @returns {Promise.<{words: Array<{originalSentence: string, translatedSentence: string}>}>}
 */
function translateWords(args, res) { // eslint-disable-line no-unused-vars
  return new Promise((resolve, reject) => {
    translatorService.translateSentenceArray(args.words, args.fromLanguage, args.toLanguage)
          .then(result => resolve({ words: result.translatedSentence }))
          .catch(err => reject(err));
  });
}

module.exports = {
  byLocation: byLocation,
  byBbox: byBbox,
  byEdges: byEdges,
  event: event,
  translate: translate,
  translateWords: translateWords
};

@@ -121,16 +121,184 @@
 lator');
+%0Aconst cassandraConnector = require('../../clients/cassandra/CassandraConnector');%0Aconst featureServiceClient = require('../../clients/locations/FeatureServiceClient');
 %0A%0A/**%0A *
@@ -2243,32 +2243,1532 @@
  no-unused-vars%0A
+  const eventById = 'SELECT * FROM fortis.events WHERE id = ?';%0A%0A  return new Promise((resolve, reject) =%3E %7B%0A    const eventId = args && args.messageId;%0A    if (!eventId) %7B%0A      reject('No event id to fetch specified');%0A      return;%0A    %7D%0A%0A    cassandraConnector.executeQuery(eventById, %5BeventId%5D)%0A    .then(rows =%3E %7B%0A      if (rows.length %3E 1) %7B%0A        reject(%60Got more $%7Brows.length%7D events with id $%7BeventId%7D%60);%0A        return;%0A      %7D%0A%0A      const ev = rows%5B0%5D;%0A      featureServiceClient.fetchById(ev.detectedplaceids %7C%7C %5B%5D)%0A      .then(features =%3E %7B%0A        resolve(%7B%0A          type: ev.pipeline,%0A          coordinates: features.map(feature =%3E feature.bbox),%0A          properties: %7B%0A            edges: ev.detectedkeywords,%0A            messageid: ev.externalid,%0A            createdtime: ev.event_time,%0A            sentiment: ev.computedfeatures && ev.computedfeatures.sentiment &&%0A              ev.computedfeatures.sentiment.pos_avg %3E ev.computedfeatures.sentiment.neg_avg%0A              ? ev.computedfeatures.sentiment.pos_avg - ev.computedfeatures.sentiment.neg_avg + 0.6%0A              : ev.computedfeatures.sentiment.neg_avg - ev.computedfeatures.sentence.pos_avg,%0A            title: ev.title,%0A            originalSources: ev.pipeline &&%0A              %5Bev.pipeline%5D,%0A            language: ev.eventlangcode,%0A            source: ev.sourceurl,%0A            fullText: ev.messagebody%0A          %7D%0A        %7D);%0A      %7D)%0A      .catch(err =%3E reject(err));%0A    %7D)%0A    .catch(err =%3E reject(err));%0A  %7D);%0A
 %7D%0A%0A/**%0A * @param

Implement MessageSchema.event resolver