
import { i18n } from '~/langs';
import { ACC_CARD_TYPE } from '~/const';

export const getListValueWithKey = (list, key, k = 'k', v = 'v') => {
  for (let i = 0, iLen = list.length; i < iLen; i++) {
    const listI = list[i];
    if (listI[k] === key) return listI[v];
  }
  return key;
};

export const getUrlSearchParams = () => {
  const params = new URLSearchParams(location.search);
  const obj = {};
  params.forEach((value, key) => {
    obj[key] = value;
  });
  return obj;
};

export const transFileSize = (srcSize) => {
  if (null == srcSize || srcSize == '') {
    return '0 Bytes';
  }
  const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  srcSize = parseFloat(srcSize);
  const index = Math.floor(Math.log(srcSize) / Math.log(1024));
  const size = (srcSize / Math.pow(1024, index)).toFixed(2);
  return size + ' ' + unitArr[index];
};

export const renderSpecStr = (spec, showPoint) => {
  if (!spec) return '';
  var ngpu = `${spec.ComputeResource}: ${spec.AccCardsNum + '*' + getListValueWithKey(ACC_CARD_TYPE, spec.AccCardType)}`;
  var gpuMemStr = spec.GPUMemGiB != 0 ? `${i18n.t('resourcesManagement.gpuMem')}: ${spec.GPUMemGiB}GB, ` : '';
  var sharedMemStr = spec.ShareMemGiB != 0 ? `, ${i18n.t('resourcesManagement.shareMem')}: ${spec.ShareMemGiB}GB` : '';
  var pointStr = showPoint ? `, ${spec.UnitPrice == 0 ? i18n.t('resourcesManagement.free') : spec.UnitPrice + i18n.t('resourcesManagement.point_hr')}` : '';
  var specStr = `${ngpu}, CPU: ${spec.CpuCores}, ${gpuMemStr}${i18n.t('resourcesManagement.mem')}: ${spec.MemGiB}GB${sharedMemStr}${pointStr}`;
  return specStr;
};

const Minute = 60;
const Hour = 60 * Minute;
const Day = 24 * Hour;
const Week = 7 * Day;
const Month = 30 * Day;
const Year = 12 * Month;

const computeTimeDiff = (diff) => {
  let diffStr = '';
  switch (true) {
    case diff <= 0:
      diff = 0;
      diffStr = i18n.t('timeObj.now');
      break;
    case diff < 2:
      diff = 0;
      diffStr = i18n.t('timeObj.1s');
      break;
    case diff < 1 * Minute:
      diffStr = i18n.t('timeObj.seconds', { msg: Math.floor(diff) });
      diff = 0;
      break;
    case diff < 2 * Minute:
      diff -= 1 * Minute;
      diffStr = i18n.t('timeObj.1m');
      break;
    case diff < 1 * Hour:
      diffStr = i18n.t('timeObj.minutes', { msg: Math.floor(diff / Minute) });
      diff -= diff / Minute * Minute;
      break;
    case diff < 2 * Hour:
      diff -= 1 * Hour;
      diffStr = i18n.t('timeObj.1h');
      break;
    case diff < 1 * Day:
      diffStr = i18n.t('timeObj.hours', { msg: Math.floor(diff / Hour) });
      diff -= diff / Hour * Hour;
      break;
    case diff < 2 * Day:
      diff -= 1 * Day;
      diffStr = i18n.t('timeObj.1d');
      break;
    case diff < 1 * Week:
      diffStr = i18n.t('timeObj.days', { msg: Math.floor(diff / Day) });
      diff -= diff / Day * Day;
      break;
    case diff < 2 * Week:
      diff -= 1 * Week;
      diffStr = i18n.t('timeObj.1w');
      break;
    case diff < 1 * Month:
      diffStr = i18n.t('timeObj.weeks', { msg: Math.floor(diff / Week) });
      diff -= diff / Week * Week;
      break;
    case diff < 2 * Month:
      diff -= 1 * Month;
      diffStr = i18n.t('timeObj.1mon');
      break;
    case diff < 1 * Year:
      diffStr = i18n.t('timeObj.months', { msg: Math.floor(diff / Month) });
      diff -= diff / Month * Month;
      break;
    case diff < 2 * Year:
      diff -= 1 * Year;
      diffStr = i18n.t('timeObj.1y');
      break;
    default:
      diffStr = i18n.t('timeObj.years', { msg: Math.floor(diff / Year) });
      diff -= (diff / Year) * Year;
      break;
  }
  return { diff, diffStr };
};

export const timeSinceUnix = (then, now) => {
  let lbl = 'timeObj.ago';
  let diff = now - then;
  if (then > now) {
    lbl = 'timeObj.from_now';
    diff = then - now;
  }
  if (diff <= 0) {
    return i18n.t('timeObj.now');
  }
  const out = computeTimeDiff(diff);
  return i18n.t(lbl, { msg: out.diffStr });
};
