export var timespans = {
  oneSecond: 1000,
  oneMinute: 60 * 1000,
  oneHour: 60 * 60 * 1000,
  oneDay: 24 * 60 * 60 * 1000,
  oneWeek: 7 * 24 * 60 * 60 * 1000,
  oneMonth: 31 * 24 * 60 * 60 * 1000 };

export function formatAmount(amount, currency, decimals) {
  if (amount) {
    let isNegative = (amount < 0);
    let s = Math.abs(amount).toFixed(decimals);
    if (Math.abs(amount) < 0.01) {
      s = Math.abs(amount).toPrecision(2);
    }
    let pos = s.indexOf('.');
    let wholeNumber = (pos >= 0) ? s.substring(0, pos) : s;
    let decimal = (pos >= 0) ? s.substring(pos) : '';
    s = '';
    while (wholeNumber.length > 0) {
      //if (s.length > 0 && wholeNumber!='-') s = ',' + s; //Don't add a comma if the only thing left to add to the string is the minus sign
      if (s.length > 0) s = ',' + s;
      if (wholeNumber.length >= 3) {
        s = wholeNumber.substring(wholeNumber.length - 3) + s;
        wholeNumber = wholeNumber.substring(0, wholeNumber.length - 3);
      } else {
        s = wholeNumber + s;
        wholeNumber = '';
      }
    }
    s = s + decimal;
    let sign = isNegative ? '-' : '';
    switch (currency) {
      case 'GBP':
        s = sign + '£' + s;
        break;
      case 'GBp':
        s = sign + s + 'p';
        break;
      case 'USD':
        s = sign + 'U$' + s;
        break;
      case 'AUD':
        s = sign + 'A$' + s;
        break;
      case 'EUR':
        s = sign + '€' + s;
        break;
      default:
        s = sign + currency + s;
    }
    return s;
  } else {
    return '';
  }
}

export function getProfitOrLoss(BasePrice, CurrentPrice, ShowPercent) {
  //let priceMovement = (CurrentPrice - BasePrice).toFixed(2);
  let priceMovement = (CurrentPrice - BasePrice).autoDP();
  let percentMovement = (((CurrentPrice / BasePrice) - 1) * 100).toFixed(1) + '%';
  if (percentMovement[0] == '-') percentMovement = percentMovement.substring(1);
  return '<span class="' + (CurrentPrice < BasePrice ? 'loss' : 'profit') + '">' + (CurrentPrice < BasePrice ? '' : '+') + priceMovement + (ShowPercent ? ' (' + percentMovement + ')' : '') + '</span>';
}

export function generateAxisBreaks(data, maxGapSize, breakSize) {
  let breaks = [];
  if (data.length > 1) {
    let previousX = data[0][0];

    for (let i = 1; i < data.length; i++) {
      if (data[i][0] != null && data[i][1] != null) {
        let currentX = data[i][0];
        if (currentX - previousX > maxGapSize) {
          let newBreak = {
            from: previousX,
            to: currentX,
            breakSize: breakSize
          };
          breaks.push(newBreak);
        }
        previousX = currentX;
      }
    }
  }
  return breaks;
}

export function getPercentCell(backgroundColor, cellWidth, valuePercent, label) {
  let labelText = label ? label : (valuePercent.toFixed(1) + '%');
  let barWidth = ((valuePercent / 100) * cellWidth);
  return '<td>' +
    '<div class="pcBackground" style="background-color: ' + backgroundColor + '; width:' + barWidth.toFixed(0) + 'px;">&nbsp;</div>' +
    '<div class="pcLabel" style="width:' + cellWidth + 'px">' + labelText + '</div>' +
    '</td>';
}
