const lib = require('lib'); const _ = require('underscore'); // const countWord = require('../statistics/count-word.js');

/**

*/ module.exports = async (language = “en”, source=“wikipedia”, context) => {

let randomSentenceResponse = await randomSentenceErrorProne(language, source, context);

let blackedOutDict = await lib[`${context.service.identifier}.black-out-random-word`]({sentence: randomSentenceResponse.rs.result});
let randomWordsFromArticle = await lib[`${context.service.identifier}.random-words`]({
  similarTo: blackedOutDict.termNormal
});

let choices = [];

_.each(randomWordsFromArticle, (randomWord) => {
  choices.push({correctAnswer: false, word: randomWord.word});
});

choices.push({correctAnswer: true, word: blackedOutDict.blackedOutWord});

choices = _.shuffle(choices);

let response = {
  articleTitle: randomSentenceResponse.p.title, 
  wikipediaId: randomSentenceResponse.p.wikipediaId, 
  sentence: blackedOutDict, 
  choices: choices,
  surroundingSentences: randomSentenceResponse.rs.surroundingSentences
};

return response;

}

async function randomSentenceErrorProne(language, source, context) {

var text;
var page;
if (source === 'wikipedia') {
  page = await lib[`${context.service.identifier}.random-wikipedia-page`]({language: language});
  text = page.text;
} else {
  page = {title: "Random texts from scan functionality (earliest alpha lel)"}
  const ocrQuery = lib.koma['ocr-query']['@dev'];

  text = await ocrQuery['random-ocr-text']();
}

let randomSentence = await lib[`${context.service.identifier}.random-sentence`]({text: text});

if(randomSentence.error === true) {
  return randomSentenceErrorProne(language, source, context);
} else {
  return {p: page, rs: randomSentence};
}

}