import type { SensoryBackground, TimerParticle } from '@/shared/types/classroomTimer';

export function formatTimerTime(seconds: number): string {
  const minutes = Math.floor(seconds / 60);
  const remainingSeconds = seconds % 60;

  return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
}

export function getTimerProgress(totalSeconds: number, remainingSeconds: number): number {
  return totalSeconds > 0 ? remainingSeconds / totalSeconds : 0;
}

export function getTimerUrgencyColor(
  isFinished: boolean,
  progress: number,
  background: SensoryBackground,
): string {
  if (isFinished) {
    return 'text-red-400 animate-pulse';
  }

  if (progress <= 0.1) {
    return 'text-red-300';
  }

  if (progress <= 0.25) {
    return 'text-amber-300';
  }

  return background.textColor;
}

export function clampNumber(value: number, min: number, max: number): number {
  return Math.max(min, Math.min(max, value));
}

export function parseBoundedInteger(value: string, min: number, max: number): number {
  const parsedValue = Number.parseInt(value, 10);
  return clampNumber(Number.isNaN(parsedValue) ? min : parsedValue, min, max);
}

export function createTimerParticles(count: number, maxSize: number, minSize: number): readonly TimerParticle[] {
  return Array.from({ length: count }, (_, index) => ({
    id: index,
    width: Math.random() * maxSize + minSize,
    height: Math.random() * maxSize + minSize,
    left: Math.random() * 100,
    top: Math.random() * 100,
    animationDelay: Math.random() * 10,
    animationDuration: Math.random() * 10 + 10,
  }));
}
