/**
* @module googleAnalytics;
* @requires module:truffle-config
* @requires module:universal-analytics
* @requires module:nanoid
* @requires module:inquirer
* @requires module:../version
*/
const Config = require("truffle-config");
const userConfig = Config.getUserConfig();
const ua = require('universal-analytics');
const nanoid = require('nanoid');
const inquirer = require("inquirer");

const version = require("../version");

//set truffleAnalyticsId depending on whether version is bundled
let appVersion;
let truffleAnalyticsId;

if (version.bundle) {
  appVersion = "v " + version.bundle;
  truffleAnalyticsId = "UA-83874933-6";
} else {
  appVersion = "(unbundled) " + "version " + version.core;
  truffleAnalyticsId = "UA-83874933-7";
}


const analyticsInquiry = [
  {
    type : "list",
    name : "analyticsInquiry",
    message : "Would you like to enable analytics for your Truffle projects? Doing so will allow us to make sure Truffle is working as expected and address any bugs more efficiently.",
    choices: ["Yes, enable analytics",
     "No, do not enable analytics"],
  }
];
const analyticsDisable = [
  {
    type: "confirm",
    name: "analyticsDisable",
    message: "Analytics are currently enabled. Would you like to disable them?",
    default: false
  }
];
const analyticsEnable = [
   {
    type: 'confirm',
    name: 'analyticsEnable',
    message: "Analytics are currently disabled. Would you like to enable them?",
    default: false
  }
];
  
const googleAnalytics = {
  /**
  * set user-level unique id
  */
  setUserId: function() {
    if(!userConfig.get('uniqueId')) {
      let userId =  nanoid();
      userConfig.set({ 'uniqueId': userId });
    } 
  },
  /**
  * set user-level options for analytics
  * @param {bool} analyticsBool
  * @param {Object} userConfig
  */
  setAnalytics: function(analyticsBool, done) {
    if(analyticsBool === true) {
      this.setUserId();
      userConfig.set({ 'enableAnalytics': true, 'analyticsSet': true, 'analyticsMessageDateTime': Date.now() });
    } else {
      userConfig.set({ 'enableAnalytics': false, 'analyticsSet': true, 'analyticsMessageDateTime': Date.now() });
    }
    return true;   
  },
  /**
  * prompt user to determine values for user-level analytics config options
  * @param {Object} userConfig
  */
  setUserConfigViaPrompt: async function() {
    if(!userConfig.get('analyticsSet') && process.stdin.isTTY === true) {
      let answer = await inquirer.prompt(analyticsInquiry);
      if(answer.analyticsInquiry === analyticsInquiry[0].choices[0]) {
        this.setAnalytics(true);
      } else {
        this.setAnalytics(false);
      }
    } else if (userConfig.get('analyticsSet') && userConfig.get('enableAnalytics') && process.stdin.isTTY === true) {
      let answer = await inquirer.prompt(analyticsDisable);
      if(answer.analyticsDisable) {
        this.setAnalytics(false); 
      } else {
        this.setAnalytics(true);
      }
    } else if(userConfig.get('analyticsSet') && !userConfig.get('enableAnalytics') && process.stdin.isTTY === true) {
      let answer = await inquirer.prompt(analyticsEnable);
      if(answer.analyticsEnable) {
        this.setAnalytics(true);
      } else {
        this.setAnalytics(false);
      }
    } else {
      return true;
    }
    return true;
  },
  /**
  * check user-level config to see if user has enabled analytics
  * @returns {bool}
  */
  checkIfAnalyticsEnabled: function() {
    if(userConfig.get("enableAnalytics")) {
      return true;
    } else {
      return false;
    }
  },

  /**
  * set data that will be the same in future calls
  * @returns {Object} visitor
  */
  setPersistentAnalyticsData: function() {
    let visitor = false;
    if(this.checkIfAnalyticsEnabled() === true) {
      let visitor = ua(truffleAnalyticsId);
      let userId = userConfig.get("uniqueId");
      visitor.set("uid", userId);  
      return visitor;
    }
  },

  /**
  * send event to Google Analytics
  * @param {Object}
  */
  sendAnalyticsEvent: function(eventObject, callback) {
    let visitor = this.setPersistentAnalyticsData();
    if(eventObject["el"]) {
      eventObject["el"] = eventObject["el"] + " (" + appVersion + ")";
    } else {
      eventObject["el"] = appVersion;
    }
    if(visitor) {
      visitor.event(eventObject, function(err) {
      });
    }
    return true;
  }
}

process.on('message', function(eventObject) {
  googleAnalytics.sendAnalyticsEvent(eventObject);
  
  setTimeout(function(){
    process.exit(0);
  }, 5000);
});

module.exports = googleAnalytics;

@@ -972,16 +972,24 @@
 ted and 
+help us 
 address 
@@ -1933,32 +1933,41 @@
 serConfig.set(%7B 
+%0A        
 'enableAnalytics
@@ -1966,32 +1966,40 @@
 nalytics': true,
+%0A       
  'analyticsSet':
@@ -1996,32 +1996,40 @@
 yticsSet': true,
+%0A       
  'analyticsMessa
@@ -2043,32 +2043,38 @@
 ime': Date.now()
+%0A     
  %7D);%0A    %7D else 
@@ -2097,16 +2097,24 @@
 ig.set(%7B
+%0A       
  'enable
@@ -2131,16 +2131,24 @@
 : false,
+%0A       
  'analyt
@@ -2162,16 +2162,25 @@
 : true, 
+%0A        
 'analyti
@@ -2209,16 +2209,22 @@
 te.now()
+%0A     
  %7D);%0A   
@@ -3326,39 +3326,8 @@
   %7D 
-else %7B%0A      return true;%0A    %7D
 %0A   

reword truffle config explanation and reformart user analytics object