import {
  type ParticleConfig,
  type ParticleEffectType,
  DEFAULT_PARTICLE_CONFIG,
} from "./particle-types";

export interface ParticlePreset {
  id: string;
  name: string;
  type: ParticleEffectType;
  description: string;
  config: ParticleConfig;
  thumbnail?: string;
}

const CONFETTI_COLORS = ["#ff6b6b", "#ffd93d", "#6bcb77", "#4d96ff", "#ff6b9d", "#845ef7"];
const FIRE_COLORS = ["#ff4500", "#ff6b00", "#ffb700", "#ffd700"];
const SPARKLE_COLORS = ["#ffffff", "#fffacd", "#fff8dc", "#ffffe0"];
const DUST_COLORS = ["#a0a0a0", "#808080", "#606060", "#404040"];
const MAGIC_COLORS = ["#9b59b6", "#8e44ad", "#3498db", "#2980b9", "#e91e63"];

export const PARTICLE_PRESETS: ParticlePreset[] = [
  {
    id: "dissolve-soft",
    name: "Soft Dissolve",
    type: "dissolve",
    description: "Gentle dissolve effect with fading particles",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 200,
      speed: 30,
      speedVariance: 20,
      gravity: 10,
      colors: DUST_COLORS,
      size: { min: 2, max: 6 },
      opacity: { start: 0.8, end: 0 },
      lifetime: { min: 1, max: 3 },
      emissionRate: 100,
      fadeIn: 0.1,
      fadeOut: 0.5,
      blendMode: "normal",
    },
  },
  {
    id: "explode-burst",
    name: "Explosion Burst",
    type: "explode",
    description: "Dramatic explosion with fast-moving particles",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 300,
      speed: 400,
      speedVariance: 150,
      gravity: 300,
      colors: FIRE_COLORS,
      size: { min: 3, max: 12 },
      opacity: { start: 1, end: 0 },
      lifetime: { min: 0.5, max: 1.5 },
      emissionRate: 500,
      emissionShape: "circle",
      emissionRadius: 20,
      fadeOut: 0.4,
      blendMode: "add",
    },
  },
  {
    id: "implode-gather",
    name: "Implode Gather",
    type: "implode",
    description: "Particles gather toward the center",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 150,
      speed: 200,
      speedVariance: 50,
      gravity: 0,
      colors: MAGIC_COLORS,
      size: { min: 4, max: 10 },
      opacity: { start: 0, end: 1 },
      lifetime: { min: 0.8, max: 1.5 },
      emissionRate: 80,
      emissionShape: "circle",
      emissionRadius: 300,
      fadeIn: 0.3,
      fadeOut: 0.1,
      blendMode: "add",
    },
  },
  {
    id: "confetti-celebration",
    name: "Celebration Confetti",
    type: "confetti",
    description: "Colorful falling confetti for celebrations",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 200,
      speed: 150,
      speedVariance: 80,
      gravity: 200,
      wind: { x: 30, y: 0, z: 0 },
      turbulence: 50,
      colors: CONFETTI_COLORS,
      size: { min: 6, max: 14 },
      opacity: { start: 1, end: 0.8 },
      lifetime: { min: 2, max: 4 },
      emissionRate: 60,
      emissionShape: "rectangle",
      emissionRadius: 400,
      rotationSpeed: 5,
      fadeOut: 0.3,
      blendMode: "normal",
    },
  },
  {
    id: "dust-ambient",
    name: "Ambient Dust",
    type: "dust",
    description: "Subtle floating dust particles",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 80,
      speed: 15,
      speedVariance: 10,
      gravity: -5,
      turbulence: 20,
      colors: DUST_COLORS,
      size: { min: 1, max: 3 },
      opacity: { start: 0.4, end: 0.1 },
      lifetime: { min: 3, max: 8 },
      emissionRate: 10,
      emissionShape: "rectangle",
      emissionRadius: 500,
      fadeIn: 0.3,
      fadeOut: 0.5,
      blendMode: "normal",
    },
  },
  {
    id: "sparkle-magic",
    name: "Magic Sparkles",
    type: "sparkle",
    description: "Glittering sparkle effect",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 100,
      speed: 20,
      speedVariance: 15,
      gravity: 10,
      colors: SPARKLE_COLORS,
      size: { min: 2, max: 6 },
      opacity: { start: 1, end: 0 },
      lifetime: { min: 0.3, max: 0.8 },
      emissionRate: 80,
      emissionShape: "sphere",
      emissionRadius: 100,
      fadeIn: 0.1,
      fadeOut: 0.5,
      blendMode: "add",
    },
  },
  {
    id: "disintegrate-thanos",
    name: "Disintegration",
    type: "disintegrate",
    description: "Object breaks apart and fades away",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 400,
      speed: 60,
      speedVariance: 40,
      gravity: -30,
      wind: { x: 50, y: -20, z: 0 },
      turbulence: 30,
      colors: ["#a0a0a0", "#808080", "#606060"],
      size: { min: 2, max: 5 },
      opacity: { start: 1, end: 0 },
      lifetime: { min: 1, max: 2.5 },
      emissionRate: 200,
      emissionShape: "rectangle",
      emissionRadius: 200,
      fadeOut: 0.6,
      blendMode: "normal",
    },
  },
  {
    id: "pixelate-retro",
    name: "Pixel Break",
    type: "pixelate",
    description: "Breaks into pixel-like blocks",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 150,
      speed: 100,
      speedVariance: 50,
      gravity: 400,
      colors: ["#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff", "#00ffff"],
      size: { min: 8, max: 16 },
      opacity: { start: 1, end: 0.5 },
      lifetime: { min: 0.5, max: 1.2 },
      emissionRate: 200,
      emissionShape: "rectangle",
      emissionRadius: 150,
      rotationSpeed: 3,
      fadeOut: 0.3,
      blendMode: "normal",
    },
  },
  {
    id: "shatter-glass",
    name: "Glass Shatter",
    type: "shatter",
    description: "Glass-breaking effect with sharp fragments",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 250,
      speed: 300,
      speedVariance: 150,
      gravity: 500,
      colors: ["#ffffff", "#e0e0e0", "#c0c0c0", "#a0a0a0"],
      size: { min: 3, max: 12 },
      opacity: { start: 0.9, end: 0.3 },
      lifetime: { min: 0.4, max: 1 },
      emissionRate: 400,
      emissionShape: "circle",
      emissionRadius: 50,
      rotationSpeed: 8,
      fadeOut: 0.4,
      blendMode: "normal",
    },
  },
  {
    id: "morph-transform",
    name: "Shape Morph",
    type: "morph",
    description: "Transform from one shape to another",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 200,
      speed: 50,
      speedVariance: 30,
      gravity: 0,
      colors: MAGIC_COLORS,
      size: { min: 4, max: 8 },
      opacity: { start: 0.8, end: 0.8 },
      lifetime: { min: 1, max: 2 },
      emissionRate: 100,
      emissionShape: "circle",
      emissionRadius: 150,
      fadeIn: 0.2,
      fadeOut: 0.2,
      blendMode: "add",
    },
  },
  {
    id: "fire-trail",
    name: "Fire Trail",
    type: "dust",
    description: "Fiery trailing particles",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 150,
      speed: 80,
      speedVariance: 40,
      gravity: -100,
      colors: FIRE_COLORS,
      size: { min: 4, max: 12 },
      opacity: { start: 0.9, end: 0 },
      lifetime: { min: 0.3, max: 0.8 },
      emissionRate: 120,
      emissionShape: "circle",
      emissionRadius: 20,
      fadeIn: 0.05,
      fadeOut: 0.6,
      blendMode: "add",
    },
  },
  {
    id: "snow-fall",
    name: "Snowfall",
    type: "dust",
    description: "Gentle falling snow",
    config: {
      ...DEFAULT_PARTICLE_CONFIG,
      particleCount: 100,
      speed: 30,
      speedVariance: 20,
      gravity: 50,
      wind: { x: 20, y: 0, z: 0 },
      turbulence: 30,
      colors: ["#ffffff", "#f0f0f0", "#e8e8e8"],
      size: { min: 2, max: 6 },
      opacity: { start: 0.8, end: 0.3 },
      lifetime: { min: 4, max: 8 },
      emissionRate: 20,
      emissionShape: "rectangle",
      emissionRadius: 600,
      rotationSpeed: 1,
      fadeIn: 0.2,
      fadeOut: 0.4,
      blendMode: "normal",
    },
  },
];

export function getParticlePresetById(id: string): ParticlePreset | undefined {
  return PARTICLE_PRESETS.find((p) => p.id === id);
}

export function getParticlePresetsByType(type: ParticleEffectType): ParticlePreset[] {
  return PARTICLE_PRESETS.filter((p) => p.type === type);
}

export function createEffectFromPreset(
  presetId: string,
  effectId: string,
  clipId: string,
  startTime: number,
  duration: number
): import("./particle-types").ParticleEffect | null {
  const preset = getParticlePresetById(presetId);
  if (!preset) return null;

  return {
    id: effectId,
    clipId,
    type: preset.type,
    startTime,
    duration,
    config: { ...preset.config },
    enabled: true,
  };
}
