import { Howl } from "howler";
import { getDayPhase } from "../World/DayNightCycle";
import { clampNumber, mapRange } from "../Utils/Math.utils";
import { NumberCue } from "../Utils/DataTypes.utils";
import { UIGameSound } from "../UIPipes/UIPipes";

export let globalSoundVolume = 1.0;
export let musicSoundVolume = 0.3;
export let ambientSoundVolume = 1.0;

UIGameSound.subscribe((s)=>{
    globalSoundVolume = s.master;
    musicSoundVolume = s.music;
    ambientSoundVolume = s.ambient;
});

export let ambientNight = new Howl({
    src: ['assets/sound/night_ambient.mp3'],
    loop: true,
    volume: globalSoundVolume * ambientSoundVolume
});

export let ambientDay = new Howl({
    src: ['assets/sound/day_ambient.mp3'],
    loop: true,
    volume: globalSoundVolume * ambientSoundVolume
});

export let ambientMusic = new Howl({
    src: ['assets/sound/df_music/df_espagnole.mp3'],
    loop: true,
    volume: globalSoundVolume * musicSoundVolume
});

let dayAmbientSoundCue = new NumberCue(
    [
        0,
        0,
        1.0,
        1.0,
        0.8,
        0.1,
        0,
    ],
    [0.0, 0.15, 0.4, 0.75, 0.8, 0.9, 1.0],
    "cos"
);

let nightAmbientSoundCue = new NumberCue(
    [
        1.0,
        1.0,
        0.0,
        0.0,
        0.2,
        0.9,
        1.0,
    ],
    [0.0, 0.15, 0.4, 0.75, 0.8, 0.9, 1.0],
    "cos"
);

export function handleSounds(tick) {
    let p = getDayPhase();
    ambientDay.volume(dayAmbientSoundCue.getValueAt(p) * globalSoundVolume * ambientSoundVolume * 0.6);
    ambientNight.volume(nightAmbientSoundCue.getValueAt(p) * globalSoundVolume * ambientSoundVolume * 0.6);
    ambientMusic.volume(globalSoundVolume * musicSoundVolume);
}

