import { ref } from 'vue';

export function useHyperspectralDataParser() {
  const parsedData = ref(null);
  const error = ref(null);

  // 解析时间结构
  const parseTimeStruct = (timeData) => {
    if (!timeData) return null;
    return {
      timezone: timeData.time_zone || 0,
      year: timeData.year || 0,
      month: timeData.month || 0,
      day: timeData.day || 0,
      hour: timeData.hour || 0,
      minute: timeData.minute || 0,
      second: timeData.second || 0,
      millisecond: timeData.millisecond || 0
    };
  };

  // 解析设备元数据
  const parseDeviceInfo = (metaData) => {
    if (!metaData || metaData.info_type !== "devinfo") {
      return null;
    }
    return {
      infoType: metaData.info_type,
      sensorId: metaData.sensor_id || '',
      bandNum: metaData.bandnum || 0,
      waveCoeff: {
        a1: metaData.wave_coeff?.a1 || 0,
        a2: metaData.wave_coeff?.a2 || 0,
        a3: metaData.wave_coeff?.a3 || 0,
        a4: metaData.wave_coeff?.a4 || 0
      }
    };
  };

  // 解析光谱数据
  const parseSpectralData = (uint8Array, bands, dataType) => {
    try {
      switch (dataType) {
        case 0x20: // float32
          return new Float32Array(uint8Array.buffer, 0, bands);
        case 0x21: // float64
          return new Float64Array(uint8Array.buffer, 0, bands);
        case 0x10: // uint8
          return new Uint8Array(uint8Array.buffer, 0, bands);
        case 0x11: // int16
          return new Int16Array(uint8Array.buffer, 0, bands);
        case 0x12: // uint16
          return new Uint16Array(uint8Array.buffer, 0, bands);
        case 0x13: // int32
          return new Int32Array(uint8Array.buffer, 0, bands);
        case 0x14: // uint32
          return new Uint32Array(uint8Array.buffer, 0, bands);
        default:
          throw new Error(`Unsupported data type: 0x${dataType.toString(16)}`);
      }
    } catch (e) {
      error.value = `Failed to parse spectral data: ${e.message}`;
      return null;
    }
  };

  // 主解析函数
  const parseHyperspectralData = (rawData, metaData) => {
    try {
      if (!rawData || !rawData.spectral_data || !rawData.bands) {
        throw new Error('Invalid data format: missing required fields');
      }
      const uint8Array = new Uint8Array(rawData.spectral_data);
      const deviceInfo = metaData ? parseDeviceInfo(metaData) : null;
      const result = {
        deviceInfo,
        dataInfo: {
          name: rawData.name || '',
          sensorId: rawData.sensor_id || '',
          fiberId: rawData.fiber_id || 0,
          collectionTime: rawData.collection_time ? parseTimeStruct(rawData.collection_time) : null,
          exposure: rawData.exposure || 0,
          gain: rawData.gain || 0,
          dataType: rawData.data_type || 0,
          pixelSize: rawData.pixel_size || 0,
          groundType: rawData.ground_type || 0,
          validFlag: rawData.valid_flag || 0,
          bands: rawData.bands || 0
        },
        spectralData: parseSpectralData(uint8Array, rawData.bands, rawData.data_type)
      };
      parsedData.value = result;
      return result;
    } catch (e) {
      error.value = `Data parsing error: ${e.message}`;
      return null;
    }
  };

  // 重置状态
  const reset = () => {
    parsedData.value = null;
    error.value = null;
  };

  return {
    parsedData,
    error,
    parseHyperspectralData,
    reset
  };
}