// cqi 状态样式
export const getCqiStatusStyle = (data: string | number) => {
  const value = Number(data);
  let style = {
    color: '#F24F4F',
    background: 'rgba(242, 79, 79, 0.2)',
  };
  if (value > 95) {
    style = {
      color: '#57EF58',
      background: 'rgba(39, 211, 134, 0.2)',
    };
  } else if (85 < value && value < 95) {
    style = {
      color: '#447FF0',
      background: 'rgba(130, 173, 255, 0.2)',
    };
  } else if (60 < value && value < 85) {
    style = {
      color: '#FFA81A',
      background: 'rgba(255, 188, 130, 0.2)',
    };
  }
  return style;
};

/**
 * 指数状态颜色
 */
export const getColorByIndex = (val: number) => {
  if (val > 95) {
    return 'rgb(87, 239, 88)';
  }
  if (val < 95 && val > 85) {
    return '#0680f4';
  }
  if (val < 85) {
    return '#ffa250';
  }
};

// cqi 状态文本
export const getCqiStatusTexts = (data: string | number) => {
  const value = Number(data);

  let obj = {
    text: '应急态',
    desc: '发生重大应急事件',
  };
  if (value > 95) {
    obj = {
      text: '良好态',
      desc: '运行良好',
    };
  } else if (85 < value && value < 95) {
    obj = {
      text: '正常态',
      desc: '运行正常',
    };
  } else if (60 < value && value < 85) {
    obj = {
      text: '预警态',
      desc: '运行随时会出现问题',
    };
  }
  return obj;
};

// cqi lineCharts

export const getCqiLineChartStyles = (data: string | number) => {
  const value = Number(data);
  let style = {
    lineColor: '#F24F4F',
    areaColorOffset: 'rgba(242, 79, 79, 0)',
    areaColorOffset1: 'rgba(242, 79, 79, 0.36)',
  };
  if (value > 95) {
    style = {
      lineColor: '#57EF58',
      areaColorOffset: 'rgba(87, 239, 88, 0)',
      areaColorOffset1: 'rgba(87, 239, 88, 0.36)',
    };
  } else if (85 < value && value < 95) {
    style = {
      lineColor: '#447FF0',
      areaColorOffset: 'rgba(130, 173, 255, 0)',
      areaColorOffset1: 'rgba(130, 173, 255, 0.36)',
    };
  } else if (60 < value && value < 85) {
    style = {
      lineColor: '#FFA81A',
      areaColorOffset: 'rgba(255, 188, 130, 0)',
      areaColorOffset1: 'rgba(255, 188, 130, 0.36)',
    };
  }
  return style;
};

// cqi显示问题
export const cqiDisplay = (cqi: string | number) => {
  const value = Number(cqi);
  return isNaN(value) ? '--' : value;
};

const document: any = window.document;
// 展开/全屏
export function requestFullScreen(element: any) {
  let requestMethod =
    element.requestFullscreen ||
    element.webkitRequestFullscreen ||
    element.msRequestFullscreen ||
    element.mozRequestFullScreen;
  if (requestMethod) {
    requestMethod.call(element);
  }
}
// 退出/全屏
export function exitFullScreen() {
  let exitMethod =
    document.exitFullscreen ||
    document.webkitExitFullscreen ||
    document.msExitFullscreen ||
    document.mozCancelFullScreen;
  if (exitMethod) {
    exitMethod.call(document);
  }
}
// 判断是否全屏
export function isFullscreenElement() {
  let isFull =
    document.fullscreenElement ||
    document.webkitFullscreenElement ||
    document.msFullscreenElement ||
    document.mozFullScreenElement;
  return !!isFull;
}
export const getWarnTotal = (data: any) => {
  if (!data) return;
  const ycz = data?.find((item: any) => item?.status === '已解除')?.num ?? 0;
  const czz = data?.find((item: any) => item?.status === '未处理')?.num ?? 0;
  return {
    ycz,
    czz,
    total: ycz + czz,
  };
};
//接口返回预警
export const getState = (num: number) => {
  switch (num) {
    case 1:
      return '一般风险';
    case 2:
      return '黄色风险';
    case 3:
      return '红色风险';
    case 4:
      return '橙色风险';
    default:
      return '正常';
  }
};
/**
 * 时间转换
 */
export const getTime = (second: number) => {
  if (!second || second < 0) return;
  const days = Math.floor(second / 86400);
  const hours = Math.floor((second % 86400) / 3600);
  const minutes = Math.floor(((second % 86400) % 3600) / 60);
  const seconds = Math.floor(((second % 86400) % 3600) % 60);
  if (days > 0) {
    return `${days}天`;
  }
  if (hours > 0) {
    return `${hours}小时`;
  }
  if (minutes > 0) {
    return `${minutes}分钟`;
  }
};
export function formatNumber(
  num: number,
  unit?: { one: string; myriad: string },
) {
  if (!num)
    return {
      num: 0,
      unit: unit?.one ?? '件',
    };
  num = Number(num);
  if (num == 0 || (num > 0 && num < 10000)) {
    return {
      num: num + '',
      unit: unit?.one ?? '件',
    };
  } else {
    return {
      num: (num / 10000).toFixed(2) + '',
      unit: unit?.myriad ?? '万件',
    };
  }
}

export function convertUnits(value: string | number): { num: number; unit: string } {
  const numericValue = typeof value === 'string' ? parseFloat(value) : value; // 将字符串转换为数字

  if (isNaN(numericValue)) {
     return {
      num: 0,
      unit: '件'
     }
  }

  if (numericValue < 10000) {
      return { num: numericValue, unit: '件' }; // 小于 10000，单位为件
  } else {
      const convertedValue = numericValue / 10000; // 转换为万
      // 判断是否为整数
      // console.log('isInteger', Number.isInteger(convertedValue))
      // console.log(Number.isInteger(convertedValue) ? Math.floor(convertedValue) : parseFloat(convertedValue.toFixed(2)))
      return {
          num: Number.isInteger(convertedValue) ? Math.floor(convertedValue) : parseFloat(convertedValue.toFixed(2)),
          unit: '万件'
      }; // 如果是整数，不保留小数位
  }
}
