const { MD5 } = require('./md5.js');
const { networkInterfaces } = require('os');

class ErrorToServiceHandler
{
    constructor() {    
        this.previousValues = {};

        this.projects_id = undefined;

        const nets = networkInterfaces();
        this.ipAddresses = Object.create(null); // Or just '{}', an empty object

        for (const name of Object.keys(nets)) {
            for (const net of nets[name]) {
                // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
                if (net.family === 'IPv4' && !net.internal) {
                    if (!this.ipAddresses[name]) {
                      this.ipAddresses[name] = [];
                    }
                    this.ipAddresses[name].push(net.address);
                }
            }
        }

        //console.log(this.ipAddresses);
        
    }

    setProjectsId(projects_id)
    {
      this.projects_id = projects_id;
    }

    processMessage(message, seconds, message_type)
    {
      if(message_type == undefined) message_type = "error_message";
      if(Array.isArray(message)) message = message.join(', ');

      let key = MD5(message);
      let timestamp = new Date().getTime();

      //keep in memory - default value is 1h
      if (seconds === undefined) seconds = 60*60;

      if(!this.previousValues.hasOwnProperty(key))
      {
          this.previousValues[key] = {ts: timestamp, duration: seconds};
      }

      let diff = (timestamp - this.previousValues[key].ts);
      if(diff < this.previousValues[key].duration*1000) return false;

      this.previousValues[key].ts = timestamp;

      return true;
    }

    sendMessageToService(message, seconds, message_type)
    {

        let f = this.processMessage(message, seconds, message_type);
        if(!f) return;
      
        /*
        //-------------
        if(message_type == undefined) message_type = "error_message";
        if(Array.isArray(message)) message = message.join(', ');

        let key = MD5(message);
        let timestamp = new Date().getTime();

        //keep in memory
        if (seconds === undefined) seconds = 60*60;

        if(!this.previousValues.hasOwnProperty(key))
        {
            this.previousValues[key] = {ts: timestamp, duration: seconds};
        }

        let diff = (timestamp - this.previousValues[key].ts);
        if(diff < this.previousValues[key].duration*1000) return;

        this.previousValues[key].ts = timestamp;
        */

        //-------------------------

        //send to service
        
        let dataToInfoSender = {id: this.projects_id};

        //js_error || error_message
        dataToInfoSender[message_type] = message;
        dataToInfoSender.ipAddresses = this.ipAddresses;

        console.log("ErrorToServiceHandler------------------------>send to service", dataToInfoSender);

        //TODO UGLY!!!
        // if error occures too early FLOW.GLOBALs.settings.project_id is still undefined
        // if(this.projects_id === undefined) this.projects_id = FLOW.GLOBALS.settings.project_id;
        if(this.projects_id === undefined) return; 

        /*
        if(this.projects_id === undefined)
        {
          console.log("this.projects_id is undefined");
          return;
        }
        */

        RESTBuilder.make(function(builder) {
          builder.method('POST');
          builder.post(dataToInfoSender);
          builder.url('http://192.168.252.2:8004/sentmessage');
  
          builder.callback(function(err, response, output) {
            console.log("process.on error send", err, response, output, dataToInfoSender);
          });
        });

        
    }
}

module.exports =  ErrorToServiceHandler;