function bytesToInt(bytes, numberOfBytes)
{
  let buffer = [];
  if(Array.isArray(bytes))
  {
    buffer = bytes.slice(0);
    if(numberOfBytes != undefined)
    {
      buffer = bytes.slice(bytes.length - numberOfBytes);
    }
  }
  else buffer.push(bytes);
  
  //var decimal = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
  let l = (buffer.length - 1) * 8;
  
  let decimal = 0;
  for(let i = 0; i < buffer.length; i++)
  {  
  	var s = buffer[i] << l;
    if(l < 8) s = buffer[i]
  	decimal = decimal + s;
    
    l = l - 8;

  }
  
  return decimal;
}

function resizeArray(arr, newSize, defaultValue) {
    while(newSize > arr.length)
        arr.push(defaultValue);
    arr.length = newSize;
}

longToByteArray = function(/*long*/long) {
    // we want to represent the input as a 8-bytes array
    var byteArray = [0, 0, 0, 0, 0, 0, 0, 0];
  
    for ( var index = 0; index < byteArray.length; index ++ ) {
        var byte = long & 0xff;
        byteArray [ index ] = byte;
        long = (long - byte) / 256 ;
    }
  
    return byteArray;
  };

function addDays(date, days) {
    var result = new Date(date);
    result.setDate(result.getDate() + days);
    return result;
}

/*
sleep(2000).then(() => {
  // Do something after the sleep!

  
});
*/

function sleep (time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

function isEmptyObject( obj ) {
  for ( var name in obj ) {
      return false;
  }
  return true;
}

function convertUTCDateToLocalDate(date) {
  var newDate = new Date(date);
  newDate.setMinutes(date.getMinutes() + date.getTimezoneOffset());
  return newDate;
}

function addZeroBefore(n) {
    return (n < 10 ? '0' : '') + n;
}

var convertBase = function () {

    function convertBase(baseFrom, baseTo) {
        return function (num) {
            return parseInt(num, baseFrom).toString(baseTo);
  
        };
    }
  
    // binary to decimal
    convertBase.bin2dec = convertBase(2, 10);
  
    // binary to hexadecimal
    convertBase.bin2hex = convertBase(2, 16);
  
    // decimal to binary
    convertBase.dec2bin = convertBase(10, 2);
  
    // decimal to hexadecimal
    convertBase.dec2hex = convertBase(10, 16);
  
    // hexadecimal to binary
    convertBase.hex2bin = convertBase(16, 2);
  
    // hexadecimal to decimal
    convertBase.hex2dec = convertBase(16, 10);
  
    return convertBase;
  }();

module.exports = {
    bytesToInt,
    longToByteArray,
    addDays,
    addZeroBefore,
    resizeArray,
    isEmptyObject,
    sleep,
    convertUTCDateToLocalDate
}