export const DEFAULT_WAVEFORM_PEAK_COUNT = 100;
const MIN_DISPLAY_WAVEFORM_PEAKS = 80;
const MAX_DISPLAY_WAVEFORM_PEAKS = 120;

export const getDefaultWaveformPeakCount = (): number => {
  const configured = Number(process.env.AUDIO_WAVEFORM_PEAKS);
  if (
    Number.isInteger(configured) &&
    configured >= MIN_DISPLAY_WAVEFORM_PEAKS &&
    configured <= MAX_DISPLAY_WAVEFORM_PEAKS
  ) {
    return configured;
  }

  return DEFAULT_WAVEFORM_PEAK_COUNT;
};

const clampPeak = (value: number): number => Math.max(0, Math.min(100, Math.round(value)));

const fallbackWaveform = (samples: number): number[] =>
  Array.from({ length: samples }, (_, index) => {
    const wave = Math.sin((index / Math.max(1, samples - 1)) * Math.PI * 6);
    const accent = index % 9 === 0 ? 18 : index % 5 === 0 ? 8 : 0;
    return clampPeak(34 + wave * 18 + accent);
  });

const scaleToRange = (values: number[]): number[] => {
  if (!values.length) {
    return [];
  }

  const sorted = [...values].sort((left, right) => left - right);
  const max = sorted[sorted.length - 1] ?? 0;
  if (max <= 0) {
    return values.map((value) => clampPeak(value));
  }

  const percentileIndex = Math.max(0, Math.min(sorted.length - 1, Math.floor(sorted.length * 0.95)));
  const scaleMax = Math.max(sorted[percentileIndex] ?? max, 1);

  return values.map((value) => clampPeak((Math.min(value, scaleMax) / scaleMax) * 100));
};

const readPcmSample = (
  buffer: Buffer,
  offset: number,
  bitsPerSample: number,
  audioFormat: number,
): number => {
  if (audioFormat === 3) {
    if (bitsPerSample === 32 && offset + 4 <= buffer.length) {
      return Math.max(-1, Math.min(1, buffer.readFloatLE(offset)));
    }
    if (bitsPerSample === 64 && offset + 8 <= buffer.length) {
      return Math.max(-1, Math.min(1, buffer.readDoubleLE(offset)));
    }
    return 0;
  }

  switch (bitsPerSample) {
    case 8:
      return (buffer.readUInt8(offset) - 128) / 128;
    case 16:
      return buffer.readInt16LE(offset) / 32768;
    case 24: {
      if (offset + 3 > buffer.length) {
        return 0;
      }
      let value = buffer.readUIntLE(offset, 3);
      if (value & 0x800000) {
        value |= 0xff000000;
      }
      return value / 8388608;
    }
    case 32:
      return buffer.readInt32LE(offset) / 2147483648;
    default:
      return 0;
  }
};

const generateWaveformPeaksFromWavBuffer = (buffer: Buffer, samples: number): number[] | null => {
  if (buffer.length < 44 || buffer.toString('ascii', 0, 4) !== 'RIFF' || buffer.toString('ascii', 8, 12) !== 'WAVE') {
    return null;
  }

  let offset = 12;
  let audioFormat = 0;
  let channels = 0;
  let bitsPerSample = 0;
  let dataOffset = 0;
  let dataSize = 0;

  while (offset + 8 <= buffer.length) {
    const chunkId = buffer.toString('ascii', offset, offset + 4);
    const chunkSize = buffer.readUInt32LE(offset + 4);
    const chunkDataOffset = offset + 8;

    if (chunkId === 'fmt ' && chunkDataOffset + 16 <= buffer.length) {
      audioFormat = buffer.readUInt16LE(chunkDataOffset);
      channels = buffer.readUInt16LE(chunkDataOffset + 2);
      bitsPerSample = buffer.readUInt16LE(chunkDataOffset + 14);
    } else if (chunkId === 'data') {
      dataOffset = chunkDataOffset;
      dataSize = Math.min(chunkSize, buffer.length - chunkDataOffset);
    }

    offset = chunkDataOffset + chunkSize + (chunkSize % 2);
  }

  const bytesPerSample = bitsPerSample / 8;
  const frameSize = bytesPerSample * channels;
  if (
    !dataOffset ||
    !dataSize ||
    !channels ||
    !Number.isInteger(bytesPerSample) ||
    frameSize <= 0 ||
    (audioFormat !== 1 && audioFormat !== 3)
  ) {
    return null;
  }

  const frameCount = Math.floor(dataSize / frameSize);
  if (frameCount <= 0) {
    return null;
  }

  const bucketSize = Math.max(1, Math.ceil(frameCount / samples));
  const peaks: number[] = [];

  for (let bucketStart = 0; bucketStart < frameCount; bucketStart += bucketSize) {
    const bucketEnd = Math.min(frameCount, bucketStart + bucketSize);
    let sumSquares = 0;
    let count = 0;

    for (let frameIndex = bucketStart; frameIndex < bucketEnd; frameIndex += 1) {
      const frameOffset = dataOffset + frameIndex * frameSize;
      for (let channel = 0; channel < channels; channel += 1) {
        const sampleOffset = frameOffset + channel * bytesPerSample;
        const value = readPcmSample(buffer, sampleOffset, bitsPerSample, audioFormat);
        sumSquares += value * value;
        count += 1;
      }
    }

    peaks.push(count ? Math.sqrt(sumSquares / count) * 100 : 0);
  }

  return normalizeWaveformPeaks(peaks, samples);
};

export const normalizeWaveformPeaks = (
  input: unknown[] | undefined,
  targetCount = getDefaultWaveformPeakCount(),
): number[] => {
  const samples =
    Number.isInteger(targetCount) &&
    targetCount >= MIN_DISPLAY_WAVEFORM_PEAKS &&
    targetCount <= MAX_DISPLAY_WAVEFORM_PEAKS
      ? targetCount
      : DEFAULT_WAVEFORM_PEAK_COUNT;

  if (!input?.length) {
    return fallbackWaveform(samples);
  }

  const cleaned = input
    .map((value) => (typeof value === 'number' ? value : Number(value)))
    .filter((value) => Number.isFinite(value))
    .map((value) => clampPeak(Math.abs(value)));

  if (!cleaned.length) {
    return fallbackWaveform(samples);
  }

  if (cleaned.length === samples) {
    return scaleToRange(cleaned);
  }

  if (cleaned.length === 1) {
    return Array.from({ length: samples }, () => cleaned[0]);
  }

  const resampled: number[] = [];
  const sourceMaxIndex = cleaned.length - 1;
  const targetMaxIndex = samples - 1;

  for (let i = 0; i < samples; i += 1) {
    const sourceIndex = targetMaxIndex === 0 ? 0 : (i / targetMaxIndex) * sourceMaxIndex;
    const leftIndex = Math.floor(sourceIndex);
    const rightIndex = Math.min(sourceMaxIndex, Math.ceil(sourceIndex));
    const ratio = sourceIndex - leftIndex;
    const interpolated = cleaned[leftIndex] * (1 - ratio) + cleaned[rightIndex] * ratio;
    resampled.push(interpolated);
  }

  return scaleToRange(resampled);
};

export const generateWaveformPeaksFromBuffer = (
  buffer: Buffer,
  samples = getDefaultWaveformPeakCount(),
): number[] => {
  if (!buffer.length) {
    return normalizeWaveformPeaks([], samples);
  }

  const wavPeaks = generateWaveformPeaksFromWavBuffer(buffer, samples);
  if (wavPeaks) {
    return wavPeaks;
  }

  const chunkSize = Math.max(1, Math.ceil(buffer.length / samples));
  const peaks: number[] = [];

  for (let offset = 0; offset < buffer.length; offset += chunkSize) {
    const chunk = buffer.subarray(offset, Math.min(buffer.length, offset + chunkSize));
    let total = 0;
    let localPeak = 0;

    for (const byte of chunk) {
      const centered = Math.abs(byte - 128);
      total += centered;
      localPeak = Math.max(localPeak, centered);
    }

    const average = chunk.length ? total / chunk.length : 0;
    peaks.push(Math.max(average, localPeak * 0.65));
  }

  return normalizeWaveformPeaks(peaks, samples);
};

export const generateWaveformPeaksFromSeed = (
  seed: string,
  samples = getDefaultWaveformPeakCount(),
): number[] => {
  const source = seed.trim() || 'audio';
  let hash = 2166136261;

  for (let i = 0; i < source.length; i += 1) {
    hash ^= source.charCodeAt(i);
    hash = Math.imul(hash, 16777619);
  }

  const peaks: number[] = [];
  let state = hash >>> 0;
  for (let i = 0; i < samples; i += 1) {
    state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
    const base = 18 + (state % 65);
    const accent = i % 6 === 0 ? 18 : i % 3 === 0 ? 10 : 0;
    peaks.push(base + accent);
  }

  return normalizeWaveformPeaks(peaks, samples);
};
