// composables/useThermalData.ts
import { ref, computed } from 'vue';

interface ThermalPoint {
  timeIndex: number;
  valueIndex: number;
  intensity: number;
}

export function useThermalData() {
  // 模拟数据生成（替换为实际API）
  const generateSimulatedData = (): ThermalPoint[] => {
    const data: ThermalPoint[] = [];
    const timePoints = 100; // 时间点数量
    const valueRange = 80;  // 数值范围 -40 到 +40

    for (let t = 0; t < timePoints; t++) {
      for (let v = 0; v < valueRange; v++) {
        // 模拟中心亮带效果
        const centerValue = 40; // 中心位置
        const distance = Math.abs(v - centerValue);
        const baseIntensity = Math.exp(-distance / 10); // 高斯分布

        // 添加噪声和动态变化
        const noise = (Math.random() - 0.5) * 0.2;
        const timeWave = Math.sin(t * 0.1) * 0.3;

        const intensity = Math.max(0, baseIntensity + noise + timeWave);

        if (intensity > 0.1) { // 过滤低强度点
          data.push({
            timeIndex: t * 20, // 转换为毫秒
            valueIndex: v - 40, // 转换为 -40 到 +40
            intensity
          });
        }
      }
    }

    return data;
  };

  const thermalData = ref<ThermalPoint[]>(generateSimulatedData());

  // 计算范围
  const timeRange = computed(() => {
    const times = thermalData.value.map(d => d.timeIndex);
    return Array.from(new Set(times)).sort((a, b) => a - b);
  });

  const valueRange = computed(() => {
    const values = thermalData.value.map(d => d.valueIndex);
    return Array.from(new Set(values)).sort((a, b) => a - b);
  });

  // 转换为 ECharts 需要的格式 [x, y, value]
  const chartData = computed(() =>
    thermalData.value.map(item => [
      item.timeIndex,
      item.valueIndex,
      item.intensity * 80 - 60 // 缩放到 -60 到 +20 范围
    ])
  );

  // 更新数据（模拟实时更新）
  const updateData = () => {
    const newData = generateSimulatedData();
    thermalData.value = newData;
  };

  return {
    thermalData: chartData,
    timeRange,
    valueRange,
    updateData
  };
}
