import { parseDateToStr } from '@/utils/date-utils';
import { parseSizeFromBits, parseSizeFromKbs } from '@/utils/parse-utils';
import { ref } from 'vue';

type FDType = {
  /**时间 */
  lineXTime: string[];
  /**上行 N3 */
  lineYUp: number[];
  /**下行 N6 */
  lineYDown: number[];
  /**容量 */
  cap: number;
};

/**UPF-流量数据 */
export const upfFlowData = ref<FDType>({
  lineXTime: [],
  lineYUp: [],
  lineYDown: [],
  cap: 0,
});

/**UPF-流量数据 数据解析 */
export function upfFlowParse(data: Record<string, string>) {
  upfFlowData.value.lineXTime.push(parseDateToStr(+data['timeGroup']));
  const upN3 = parseSizeFromKbs(+data['UPF.03'], 5);
  upfFlowData.value.lineYUp.push(upN3[0]);
  const downN6 = parseSizeFromKbs(+data['UPF.06'], 5);
  upfFlowData.value.lineYDown.push(downN6[0]);
  upfFlowData.value.cap += 1;
  // 超过 25 弹出
  if (upfFlowData.value.cap > 25) {
    upfFlowData.value.lineXTime.shift();
    upfFlowData.value.lineYUp.shift();
    upfFlowData.value.lineYDown.shift();
    upfFlowData.value.cap -= 1;
  }
  // UPF-总流量数0天 当天24小时
  upfTFParse('0', {
    up: upfTotalFlow.value['0'].up + +data['UPF.03'],
    down: upfTotalFlow.value['0'].down + +data['UPF.06'],
  });
}

type TFType = {
  /**上行 N3 */
  up: number;
  upFrom: string;
  /**下行 N6 */
  down: number;
  downFrom: string;
  /**请求标记 */
  requestFlag: boolean;
};

/**UPF-总流量数 */
export const upfTotalFlow = ref<Record<string, TFType>>({
  '0': {
    up: 0,
    upFrom: '0 B',
    down: 0,
    downFrom: '0 B',
    requestFlag: false,
  },
  '7': {
    up: 0,
    upFrom: '0 B',
    down: 0,
    downFrom: '0 B',
    requestFlag: false,
  },
  '30': {
    up: 0,
    upFrom: '0 B',
    down: 0,
    downFrom: '0 B',
    requestFlag: false,
  },
});

/**UPF-总流量数 数据解析 */
export function upfTFParse(day: string, data: Record<string, number>) {
  let { up, down } = data;
  upfTotalFlow.value[day] = {
    up: up,
    upFrom: parseSizeFromBits(up),
    down: down,
    downFrom: parseSizeFromBits(down),
    requestFlag: false,
  };
}

/**UPF-总流量数 选中 */
export const upfTFActive = ref<string>('0');

/**属性复位 */
export function upfTotalFlowReset() {
  upfFlowData.value = {
    lineXTime: [],
    lineYUp: [],
    lineYDown: [],
    cap: 0,
  };
  for (const key of Object.keys(upfTotalFlow.value)) {
    upfTotalFlow.value[key] = {
      up: 0,
      upFrom: '0 B',
      down: 0,
      downFrom: '0 B',
      requestFlag: false,
    };
  }
  upfTFActive.value = '0';
}
