import { BC_VIEWPORT, BC_WORLD, PRNG } from "../GlobalVariables/GlobalVariables";
import { UIGameTimePipe } from "../UIPipes/UIPipes";
import { RGBColor, RGBCue } from "../Utils/DataTypes.utils";

let timeElapsed = PRNG() * 37992648739;

// ex: scaling = 10; 1 game minute = 6 real seconds;
let timeScaling = 100.0;

const gameSecondsInGameMinute = 60;
const gameMinutesInGameHour = 60;
const gameHoursInGameDay = 24;
const gameDaysInGameWeek = 7;
const gameWeeksInGameMonth = 4;
const gameMonthsInGameYear = 12;

// let dayColor = new RGBColor(255, 255, 255);
let currentColor = new RGBColor(0, 0, 0);

let dayColorsCue = new RGBCue(
    [
        new RGBColor(1, 22, 46),
        new RGBColor(1, 66, 109),
        new RGBColor(230, 109, 109),
        new RGBColor(255, 255, 255),
        new RGBColor(255, 255, 192),
        new RGBColor(230, 134, 77),
        new RGBColor(1, 66, 109),
        new RGBColor(1, 22, 46),
    ],
    [0.0, 0.15, 0.2, 0.4, 0.8, 0.85, 0.9, 1.0],
    "cos"
);

export function handleDayNightCycle(tick) {
    timeElapsed += (tick.deltaMS / 1000) * timeScaling;
    UIGameTimePipe.update((s) => {
        s.gameTime = getPrettyGameTimeNow();
        s.gameSecondsPass = Math.floor(timeElapsed);
    });

    currentColor = dayColorsCue.getColorAt(getDayPhase());
    BC_VIEWPORT.tint = currentColor.toNumber();
}

export function getDayPhase() {
    return (
        (Math.floor(timeElapsed) % (gameSecondsInGameMinute * gameMinutesInGameHour * gameHoursInGameDay)) /
        (gameSecondsInGameMinute * gameMinutesInGameHour * gameHoursInGameDay)
    );
}

export function getPrettyGameTimePassed() {
    let secondsPass = Math.floor(timeElapsed);
    let minutesPass = Math.floor(secondsPass / gameSecondsInGameMinute);
    let hoursPass = Math.floor(minutesPass / gameMinutesInGameHour);
    let daysPass = Math.floor(hoursPass / gameHoursInGameDay);
    let weeksPass = Math.floor(daysPass / gameDaysInGameWeek);
    let monthsPass = Math.floor(weeksPass / gameWeeksInGameMonth);
    let yearsPass = Math.floor(monthsPass / gameMonthsInGameYear);
    return {
        seconds: secondsPass,
        minutes: minutesPass,
        hours: hoursPass,
        days: daysPass,
        weeks: weeksPass,
        months: monthsPass,
        years: yearsPass,
    };
}

export function getPrettyGameTimeNow() {
    let passed = getPrettyGameTimePassed();
    return {
        seconds: passed.seconds % gameSecondsInGameMinute,
        minutes: passed.minutes % gameMinutesInGameHour,
        hours: passed.hours % gameHoursInGameDay,
        days: passed.days % gameDaysInGameWeek,
        weeks: passed.weeks % gameWeeksInGameMonth,
        months: passed.months % gameMonthsInGameYear,
        years: passed.years,
    };
}

export function getGameTimeElapsed() {
    return timeElapsed;
}
