import {
  defaultGardenAudioVibeSettings,
  type GardenAudioChord,
} from '../audio/garden-audio-config';
import { VibeId, type GardenVibeSettings, type VibePreset } from './types';

type ColorReactionSettings = Pick<
  GardenVibeSettings,
  | 'color1ToColor1'
  | 'color1ToColor2'
  | 'color1ToColor3'
  | 'color2ToColor1'
  | 'color2ToColor2'
  | 'color2ToColor3'
  | 'color3ToColor1'
  | 'color3ToColor2'
  | 'color3ToColor3'
>;

const colorReactions = {
  auroraMycelium: {
    color1ToColor1: 1,
    color1ToColor2: 0,
    color1ToColor3: 0,
    color2ToColor1: -1,
    color2ToColor2: 1,
    color2ToColor3: 0,
    color3ToColor1: -1,
    color3ToColor2: -1,
    color3ToColor3: 1,
  },
  velvetObservatory: {
    color1ToColor1: 1,
    color1ToColor2: -1,
    color1ToColor3: -1,
    color2ToColor1: -1,
    color2ToColor2: 1,
    color2ToColor3: -1,
    color3ToColor1: -1,
    color3ToColor2: -1,
    color3ToColor3: 1,
  },
  lichenSignal: {
    color1ToColor1: 0,
    color1ToColor2: -1,
    color1ToColor3: 1,
    color2ToColor1: -1,
    color2ToColor2: 0,
    color2ToColor3: -1,
    color3ToColor1: 1,
    color3ToColor2: -1,
    color3ToColor3: 1,
  },
  tidepoolLantern: {
    color1ToColor1: 0,
    color1ToColor2: 1,
    color1ToColor3: 0,
    color2ToColor1: 0,
    color2ToColor2: 0,
    color2ToColor3: 1,
    color3ToColor1: 1,
    color3ToColor2: 0,
    color3ToColor3: 0,
  },
  paperLanternFog: {
    color1ToColor1: 1,
    color1ToColor2: 1,
    color1ToColor3: 1,
    color2ToColor1: 1,
    color2ToColor2: 1,
    color2ToColor3: 1,
    color3ToColor1: 1,
    color3ToColor2: 1,
    color3ToColor3: 1,
  },
  chromePollen: {
    color1ToColor1: 1,
    color1ToColor2: 0,
    color1ToColor3: 1,
    color2ToColor1: -1,
    color2ToColor2: 1,
    color2ToColor3: 0,
    color3ToColor1: 1,
    color3ToColor2: 0,
    color3ToColor3: 1,
  },
} satisfies Record<string, ColorReactionSettings>;

const musicScales = {
  dorian: [0, 2, 3, 5, 7, 9, 10],
  lydian: [0, 2, 4, 6, 7, 9, 11],
  mixolydian: [0, 2, 4, 5, 7, 9, 10],
  naturalMinor: [0, 2, 3, 5, 7, 8, 10],
} satisfies Record<string, Array<number>>;

const musicProgressions = {
  aurora: [
    { rootOffset: 0, quality: 'sus2' },
    { rootOffset: 7, quality: 'major' },
    { rootOffset: 9, quality: 'minor' },
    { rootOffset: 5, quality: 'sus4' },
  ],
  chrome: [
    { rootOffset: 0, quality: 'major' },
    { rootOffset: 2, quality: 'major' },
    { rootOffset: 7, quality: 'sus2' },
    { rootOffset: 9, quality: 'minor' },
  ],
  lichen: [
    { rootOffset: 0, quality: 'minor' },
    { rootOffset: 5, quality: 'major' },
    { rootOffset: 10, quality: 'major' },
    { rootOffset: 3, quality: 'major' },
  ],
  paperLantern: [
    { rootOffset: 0, quality: 'minor' },
    { rootOffset: 8, quality: 'major' },
    { rootOffset: 5, quality: 'minor' },
    { rootOffset: 10, quality: 'sus4' },
  ],
  tidepool: [
    { rootOffset: 0, quality: 'major' },
    { rootOffset: 10, quality: 'major' },
    { rootOffset: 5, quality: 'sus2' },
    { rootOffset: 9, quality: 'minor' },
  ],
  velvet: [
    { rootOffset: 0, quality: 'minor' },
    { rootOffset: 8, quality: 'major' },
    { rootOffset: 3, quality: 'major' },
    { rootOffset: 5, quality: 'sus4' },
  ],
} satisfies Record<string, Array<GardenAudioChord>>;

export const defaultVibeId = VibeId.AuroraMycelium;

export const vibePresets: Array<VibePreset> = [
  {
    id: VibeId.AuroraMycelium,
    name: 'Aurora Mycelium Copy',
    colors: [
      [251, 210, 94],
      [154, 99, 255],
      [255, 31, 199],
    ],
    backgroundColor: [6, 13, 22],
    settings: {
      ...colorReactions.auroraMycelium,
      backgroundGrainStrength: 0.003,
      brushSize: 8.75,
      clarity: 1,
      decayRateTrails: 973,
      forwardRotationScale: 0.37,
      individualTrailWeight: 0.053000000000000005,
      moveSpeed: 144,
      sensorOffsetAngle: 35,
      sensorOffsetDistance: 52,
      spawnPerPixel: 0.13999999999999999,
      strokeAngleJitterRadians: 0.45,
      turnSpeed: 13,
      turnWhenLost: 0,
    },
    audio: {
      ...defaultGardenAudioVibeSettings,
      idleIntensity: 0.12000000000000002,
      bpm: 60,
      rampUpIntensity: 0.7,
      rampUpTime: 0.14,
      noteLength: 0.8599999999999999,
      notePitchOffset: -2,
      brightness: 0.84,
      scale: musicScales.lydian,
      progression: musicProgressions.aurora,
    },
  },
  {
    id: VibeId.VelvetObservatory,
    name: 'Velvet Observatory Copy',
    colors: [
      [178, 76, 62],
      [2, 174, 255],
      [213, 193, 9],
    ],
    backgroundColor: [7, 4, 22],
    settings: {
      ...colorReactions.velvetObservatory,
      backgroundGrainStrength: 0.005,
      brushSize: 9.75,
      clarity: 1,
      decayRateTrails: 974,
      forwardRotationScale: 0,
      individualTrailWeight: 0.232,
      moveSpeed: 121,
      sensorOffsetAngle: 24,
      sensorOffsetDistance: 17,
      spawnPerPixel: 0.11499999999999999,
      strokeAngleJitterRadians: 0.17,
      turnSpeed: 33,
      turnWhenLost: 0.42,
    },
    audio: {
      ...defaultGardenAudioVibeSettings,
      idleIntensity: 0.24000000000000002,
      bpm: 72,
      rampUpIntensity: 1.42,
      rampUpTime: 0.07,
      noteLength: 0.7,
      notePitchOffset: 0,
      brightness: 0.94,
      scale: musicScales.naturalMinor,
      progression: musicProgressions.velvet,
    },
  },
  {
    id: VibeId.LichenSignal,
    name: 'Lichen Signal',
    colors: [
      [183, 216, 92],
      [65, 166, 128],
      [238, 120, 76],
    ],
    backgroundColor: [0, 0, 0],
    settings: {
      ...colorReactions.lichenSignal,
      backgroundGrainStrength: 0.02,
      brushSize: 6.5,
      clarity: 0.74,
      decayRateTrails: 962,
      forwardRotationScale: 0.3,
      individualTrailWeight: 0.052,
      moveSpeed: 72,
      sensorOffsetAngle: 42,
      sensorOffsetDistance: 54,
      spawnPerPixel: 0.16,
      strokeAngleJitterRadians: 3.14,
      turnSpeed: 44,
      turnWhenLost: 0.92,
    },
    audio: {
      ...defaultGardenAudioVibeSettings,
      idleIntensity: 0.13,
      bpm: 68,
      rampUpIntensity: 1.46,
      rampUpTime: 0.1,
      noteLength: 0.6,
      notePitchOffset: -3,
      brightness: 1.21,
      scale: musicScales.dorian,
      progression: musicProgressions.lichen,
    },
  },
  {
    id: VibeId.TidepoolLantern,
    name: 'Tidepool Lantern',
    colors: [
      [30, 219, 194],
      [61, 118, 255],
      [255, 191, 91],
    ],
    backgroundColor: [4, 18, 29],
    settings: {
      ...colorReactions.tidepoolLantern,
      backgroundGrainStrength: 0.018,
      brushSize: 17,
      clarity: 0.56,
      decayRateTrails: 968,
      forwardRotationScale: 0.38,
      individualTrailWeight: 0.06,
      moveSpeed: 88,
      sensorOffsetAngle: 64,
      sensorOffsetDistance: 46,
      spawnPerPixel: 0.22,
      strokeAngleJitterRadians: 1.8,
      turnSpeed: 66,
      turnWhenLost: 1.05,
    },
    audio: {
      ...defaultGardenAudioVibeSettings,
      idleIntensity: 0.08,
      bpm: 84,
      rampUpIntensity: 0.95,
      rampUpTime: 0.08,
      noteLength: 0.46,
      notePitchOffset: 0,
      brightness: 0.98,
      scale: musicScales.mixolydian,
      progression: musicProgressions.tidepool,
    },
  },
  {
    id: VibeId.PaperLanternFog,
    name: 'Paper Lantern Fog',
    colors: [
      [255, 176, 108],
      [239, 90, 108],
      [128, 213, 184],
    ],
    backgroundColor: [30, 23, 20],
    settings: {
      ...colorReactions.paperLanternFog,
      backgroundGrainStrength: 0.038,
      brushSize: 3.5,
      clarity: 1,
      decayRateTrails: 999,
      forwardRotationScale: 0.24,
      individualTrailWeight: 0.937,
      moveSpeed: 28,
      sensorOffsetAngle: 34,
      sensorOffsetDistance: 66,
      spawnPerPixel: 0.055,
      strokeAngleJitterRadians: 0,
      turnSpeed: 30,
      turnWhenLost: 1.52,
    },
    audio: {
      ...defaultGardenAudioVibeSettings,
      idleIntensity: 0.33,
      bpm: 127,
      rampUpIntensity: 0.66,
      rampUpTime: 0.03,
      noteLength: 0.92,
      notePitchOffset: 10,
      brightness: 1.42,
      scale: musicScales.naturalMinor,
      progression: musicProgressions.paperLantern,
    },
  },
  {
    id: VibeId.ChromePollen,
    name: 'Chrome Pollen',
    colors: [
      [178, 34, 34],
      [255, 214, 48],
      [77, 240, 157],
    ],
    backgroundColor: [7, 12, 11],
    settings: {
      ...colorReactions.chromePollen,
      backgroundGrainStrength: 0.012,
      brushSize: 4.5,
      clarity: 0.1,
      decayRateTrails: 922,
      forwardRotationScale: 0.5,
      individualTrailWeight: 0.026,
      moveSpeed: 86,
      sensorOffsetAngle: 46,
      sensorOffsetDistance: 14,
      spawnPerPixel: 0.36,
      strokeAngleJitterRadians: 3,
      turnSpeed: 34,
      turnWhenLost: 1.35,
    },
    audio: {
      ...defaultGardenAudioVibeSettings,
      idleIntensity: 0.11,
      bpm: 150,
      rampUpIntensity: 2,
      rampUpTime: 0.06,
      noteLength: 1.8,
      notePitchOffset: -12,
      brightness: 0.5,
      scale: musicScales.lydian,
      progression: musicProgressions.chrome,
    },
  },
];
