import { Moment } from 'moment';

enum TimeUnit {
  SECOND = 'second',
  MINUTE = 'MINUTE',
  HOUR = 'HOUR',
  DAY = 'DAY',
}

const unitMapping = {
  [TimeUnit.SECOND]: '秒',
  [TimeUnit.MINUTE]: '分',
  [TimeUnit.HOUR]: '小时',
  [TimeUnit.DAY]: '天',
};

const unitConversion = {
  [TimeUnit.SECOND]: 1 * 1000,
  [TimeUnit.MINUTE]: 1 * 60 * 1000,
  [TimeUnit.HOUR]: 1 * 60 * 60 * 1000,
  [TimeUnit.DAY]: 1 * 60 * 60 * 24 * 1000,
};

export const intervalOption = [
  {
    id: 1,
    unit: TimeUnit.SECOND,
    linkage: [{ id: 1, unit: TimeUnit.SECOND }],
  },
  {
    id: 5,
    unit: TimeUnit.SECOND,
    linkage: [{ id: 1, unit: TimeUnit.SECOND }],
  },
  {
    id: 10,
    unit: TimeUnit.SECOND,
    linkage: [{ id: 1, unit: TimeUnit.SECOND }],
  },
  {
    id: 15,
    unit: TimeUnit.SECOND,
    linkage: [{ id: 1, unit: TimeUnit.SECOND }],
  },
  {
    id: 30,
    unit: TimeUnit.SECOND,
    linkage: [{ id: 1, unit: TimeUnit.SECOND }],
  },
  {
    id: 1,
    unit: TimeUnit.MINUTE,
    linkage: [
      { id: 1, unit: TimeUnit.SECOND },
      { id: 5, unit: TimeUnit.SECOND },
    ],
  },
  {
    id: 2,
    unit: TimeUnit.MINUTE,
    linkage: [
      { id: 1, unit: TimeUnit.SECOND },
      { id: 5, unit: TimeUnit.SECOND },
      { id: 10, unit: TimeUnit.SECOND },
      { id: 15, unit: TimeUnit.SECOND },
    ],
  },
  {
    id: 5,
    unit: TimeUnit.MINUTE,
    linkage: [
      { id: 1, unit: TimeUnit.SECOND },
      { id: 5, unit: TimeUnit.SECOND },
      { id: 10, unit: TimeUnit.SECOND },
      { id: 15, unit: TimeUnit.SECOND },
      { id: 30, unit: TimeUnit.SECOND },
    ],
  },
  {
    id: 10,
    unit: TimeUnit.MINUTE,
    linkage: [
      { id: 5, unit: TimeUnit.SECOND },
      { id: 10, unit: TimeUnit.SECOND },
      { id: 15, unit: TimeUnit.SECOND },
      { id: 30, unit: TimeUnit.SECOND },
      { id: 1, unit: TimeUnit.MINUTE },
    ],
  },
  {
    id: 15,
    unit: TimeUnit.MINUTE,
    linkage: [
      { id: 5, unit: TimeUnit.SECOND },
      { id: 10, unit: TimeUnit.SECOND },
      { id: 15, unit: TimeUnit.SECOND },
      { id: 30, unit: TimeUnit.SECOND },
      { id: 1, unit: TimeUnit.MINUTE },
      { id: 2, unit: TimeUnit.MINUTE },
    ],
  },
  {
    id: 30,
    unit: TimeUnit.MINUTE,
    linkage: [
      { id: 5, unit: TimeUnit.SECOND },
      { id: 10, unit: TimeUnit.SECOND },
      { id: 15, unit: TimeUnit.SECOND },
      { id: 30, unit: TimeUnit.SECOND },
      { id: 1, unit: TimeUnit.MINUTE },
      { id: 2, unit: TimeUnit.MINUTE },
    ],
  },
  {
    id: 1,
    unit: TimeUnit.HOUR,
    linkage: [
      { id: 10, unit: TimeUnit.SECOND },
      { id: 15, unit: TimeUnit.SECOND },
      { id: 30, unit: TimeUnit.SECOND },
      { id: 1, unit: TimeUnit.MINUTE },
      { id: 2, unit: TimeUnit.MINUTE },
      { id: 5, unit: TimeUnit.MINUTE },
    ],
  },
  {
    id: 2,
    unit: TimeUnit.HOUR,
    linkage: [
      { id: 15, unit: TimeUnit.SECOND },
      { id: 30, unit: TimeUnit.SECOND },
      { id: 1, unit: TimeUnit.MINUTE },
      { id: 2, unit: TimeUnit.MINUTE },
      { id: 5, unit: TimeUnit.MINUTE },
      { id: 10, unit: TimeUnit.MINUTE },
      { id: 15, unit: TimeUnit.MINUTE },
    ],
  },
  {
    id: 5,
    unit: TimeUnit.HOUR,
    linkage: [
      { id: 1, unit: TimeUnit.MINUTE },
      { id: 2, unit: TimeUnit.MINUTE },
      { id: 5, unit: TimeUnit.MINUTE },
      { id: 10, unit: TimeUnit.MINUTE },
      { id: 15, unit: TimeUnit.MINUTE },
      { id: 30, unit: TimeUnit.MINUTE },
    ],
  },
  {
    id: 10,
    unit: TimeUnit.HOUR,
    linkage: [
      { id: 2, unit: TimeUnit.MINUTE },
      { id: 5, unit: TimeUnit.MINUTE },
      { id: 10, unit: TimeUnit.MINUTE },
      { id: 15, unit: TimeUnit.MINUTE },
      { id: 30, unit: TimeUnit.MINUTE },
      { id: 1, unit: TimeUnit.HOUR },
    ],
  },
  {
    id: 12,
    unit: TimeUnit.HOUR,
    linkage: [
      { id: 2, unit: TimeUnit.MINUTE },
      { id: 5, unit: TimeUnit.MINUTE },
      { id: 10, unit: TimeUnit.MINUTE },
      { id: 15, unit: TimeUnit.MINUTE },
      { id: 30, unit: TimeUnit.MINUTE },
      { id: 1, unit: TimeUnit.HOUR },
    ],
  },
  {
    id: 1,
    unit: TimeUnit.DAY,
    linkage: [
      { id: 5, unit: TimeUnit.MINUTE },
      { id: 10, unit: TimeUnit.MINUTE },
      { id: 15, unit: TimeUnit.MINUTE },
      { id: 30, unit: TimeUnit.MINUTE },
      { id: 1, unit: TimeUnit.HOUR },
      { id: 2, unit: TimeUnit.HOUR },
    ],
  },
  {
    id: 7,
    unit: TimeUnit.DAY,
    linkage: [
      { id: 30, unit: TimeUnit.MINUTE },
      { id: 1, unit: TimeUnit.HOUR },
      { id: 2, unit: TimeUnit.HOUR },
      { id: 5, unit: TimeUnit.HOUR },
      { id: 10, unit: TimeUnit.HOUR },
      { id: 12, unit: TimeUnit.HOUR },
      { id: 1, unit: TimeUnit.DAY },
    ],
  },
  {
    id: 30,
    unit: TimeUnit.DAY,
    linkage: [
      { id: 2, unit: TimeUnit.HOUR },
      { id: 5, unit: TimeUnit.HOUR },
      { id: 10, unit: TimeUnit.HOUR },
      { id: 12, unit: TimeUnit.HOUR },
      { id: 1, unit: TimeUnit.DAY },
    ],
  },
].map((item) => {
  return {
    value: item.id * unitConversion[item.unit],
    label: item.id + unitMapping[item.unit],
    linkage: item.linkage.map((item) => {
      return {
        value: item.id * unitConversion[item.unit],
        label: item.id + unitMapping[item.unit],
      };
    }),
  };
});

const rangeIntervalOption = [
  {
    id: 90,
    unit: TimeUnit.DAY,
    linkage: [
      { id: 5, unit: TimeUnit.HOUR },
      { id: 10, unit: TimeUnit.HOUR },
      { id: 12, unit: TimeUnit.HOUR },
      { id: 1, unit: TimeUnit.DAY },
      { id: 7, unit: TimeUnit.DAY },
    ],
  },
  {
    id: 180,
    unit: TimeUnit.DAY,
    linkage: [
      { id: 10, unit: TimeUnit.HOUR },
      { id: 12, unit: TimeUnit.HOUR },
      { id: 1, unit: TimeUnit.DAY },
      { id: 7, unit: TimeUnit.DAY },
    ],
  },
  {
    id: 360,
    unit: TimeUnit.DAY,
    linkage: [
      { id: 1, unit: TimeUnit.DAY },
      { id: 7, unit: TimeUnit.DAY },
      { id: 30, unit: TimeUnit.DAY },
    ],
  },
].map((item) => {
  return {
    value: item.id * unitConversion[item.unit],
    label: item.id + unitMapping[item.unit],
    linkage: item.linkage.map((item) => {
      return {
        value: item.id * unitConversion[item.unit],
        label: item.id + unitMapping[item.unit],
      };
    }),
  };
});

/**
 * @description
 * @param {number} value
 * @returns
 */
export function getPacketIntervalByValue(value: number) {
  return intervalOption.find((item) => item.value === value)?.linkage || [];
}

export function getPacketIntervalByRange(
  [start, end] = [null, null] as [Nullable<Moment>, Nullable<Moment>]
) {
  if (start && end) {
    const value = end.diff(start, 'ms');
    let options: { value: number; label: string }[] = [];
    for (const item of [...intervalOption, ...rangeIntervalOption]) {
      if (item.value <= value) continue;
      if (item.value >= value) {
        options = item.linkage;
        break;
      }
    }
    return options;
  }
  return [];
}
