<?php

function getLoopDayName($index, $reverse) {
    $_map = array(
        'JAN', 'FEB', 'MAR',
        'APR', 'MAY', 'JUN',
        'JUL', 'AUG', 'SEP',
        'OCT', 'NOV', 'DEC',
        'YEZ'
    );

    if ($reverse) {
        return $_map[13 - $index];
    }
    return $_map[$index - 1];
}

function getLoopDayIndex($position) {
    return $position;    
}

function getLoopYear($_toneData) {
    $wave = $_toneData['wave'];
    $tone = $_toneData['tone'];

    if ($wave < 0) {
        return 0;
    }

    if ($wave < 23) {
        return $wave;
    }

    if ($wave == 23) {
        // moon 222,555 mi
        if ($tone == 1) return 23;
        if ($tone == 2) return 24;
        if ($tone >= 3 && $tone <= 6) return 25;
        $bump = $tone - 4;
        return $wave + $bump;
    }

    return $wave + 9;
}


function getLoopDayPosition($tone) {
    return $tone;
}

function getWhen($loopDate, $currentDate) {

    $is_toda = $loopDate->format('m-d-Y') === $currentDate->format('m-d-Y');
    $is_past = !$is_toda && $loopDate < $currentDate; 
    $is_future = !$is_past && !$is_toda;

    if ($is_past) return '404';
    if ($is_toda) return '1212';        
    if ($is_future) return '333';

}

function getToneData($gregorianDate) {
    $baseDate = DateTime::createFromFormat('m-d-Y', '12-12-2023'); 
    $inputDate = DateTime::createFromFormat('m-d-Y', $gregorianDate);

    $dayOutOfTime = "02-29-2024";
    if ($inputDate->format('m-d-Y') == $dayOutOfTime) {
        return null;
    }

    $lionsGate = "10-08-2024";
    if ($inputDate->format('m-d-Y') == $lionsGate) {
        return null;
    }

    $toneNumber = NULL;
    $interval = $inputDate->diff($baseDate);
    $daysDifference = $interval->days;
    if ($inputDate > DateTime::createFromFormat('m-d-Y', $dayOutOfTime)) {
        $daysDifference -= 1;
    }
    $toneNumber = ($daysDifference + 1) % 13; 
    if ($toneNumber == 0) {
        $toneNumber = 13;
    }

    // Calculate the wave based on the first day of each tone cycle
    $firstDayOfTone = $daysDifference % 13 === 0;
    $wave = $firstDayOfTone ? ($daysDifference / 13) + 1 : ceil($daysDifference / 13);

    $wave = $wave - 1;  // start at 0

    return array(
        'tone' => $toneNumber, 
        'wave' => $wave
    ); 
}

function generateKalada($startDate, $numDays) {
    $_kalada = array();
    $currentDate = new DateTime();
    $startHere = DateTime::createFromFormat('m-d-Y', $startDate);

    $kount = 0;

    for ($i = 0; $i < $numDays; $i++) {
        $loopDate = clone $startHere;
        $loopDate->modify("+$i day");
        $toneData = getToneData($loopDate->format('m-d-Y'));
    
        if ($toneData === null) {

            $kount ++;

            $_kalada[] = array(
                'kount' => $kount,
                'datetime' => $loopDate, 
                'date' => $loopDate->format('m-d-Y'),
                'when' => getWhen($loopDate, $currentDate),
                'tone' => null,                
                'position' => null,
                'LY' => null,
                'dayname' => null,
            );
            continue;
        }

        $tone = $toneData['tone'];
        $position = getLoopDayPosition($tone);

        $LY = getLoopYear($toneData);
        
        $loopDayIndex = getLoopDayIndex($position);
        $reverse = ($LY == 14);
        $loopDayName = getLoopDayName($loopDayIndex, $reverse);

        if ($LY > 0) {
            $kount ++;
        }

        $_kalada[] = array(
            'kount' => $kount,
            'datetime' => $loopDate,             
            'date' => $loopDate->format('m-d-Y'),
            'when' => getWhen($loopDate, $currentDate),
            'tone' => $tone,            
            'position' => $position,
            'LY' => $LY,
            'dayname' => $loopDayName,
            'wave' => $toneData['wave'],
        );
    }

    return $_kalada;
}


function formatWhenKaji($zsynk) {
    if ($zsynk == '404') return '🏆';
    if ($zsynk == '1212') return "<a class='kaji' href='footbook'>🐸</a>";
    if ($zsynk == '333') return '🐇';
}        

function formatMMDay($date) {
    $inputDate = DateTime::createFromFormat('m-d-Y', $date);
    $month = $inputDate->format('m');
    $day = $inputDate->format('d');
    $year = $inputDate->format('Y');
    
    $mm = FALSE;
    if ($month == '05') {
        $mm = TRUE;
        $mmday = $day;
    }
    elseif ($month == '06') {
        $month = '05';
        $mmday = sprintf('%02d', $day + 31);
        $mm = TRUE;
    } elseif ($month > '06' && $year == 2024) {
        $mmday = sprintf('%02d', $day + 31 * ($month - 6 + 1));
        $mm = TRUE;

        // if (($month == 7 && $day >= 25) || 
        //         $month > 7) {
        //     $mmday --;
        // }

        // if ($month == 7 && $day == 25) {
        //     $mm = FALSE;
        // }
    }

    if ($mm === FALSE) return "";

    if ($mmday < 25) {
        return "";
    }
    
    if ($mmday > 99) {
        return "";
    }

    if ($mmday < 86) {
        return "mm+$mmday";
    }

    if ($mmday >= 86) {
        $mmday = 99 - (intval($mmday) - 86);
        return "mm+$mmday";
    }
}

function printKalada($_kalada) {

    $DROP = "";
    $DROP .= '<table style="font-size: 1.3em" border="1">';
    
    $_kalada = array_reverse($_kalada);

    foreach ($_kalada as $_da) {

        if (!isset($_da['tone'])) {
            $loopCode = "";
            $position = "";
        }
        else {
            $year = sprintf('%02d', $_da['LY']);
            $loopCode = "LY" . $year . ':' . $_da['dayname'];
            $position = sprintf('%02d', $_da['position']);
        }

        $date = formatMMDay($_da['date']);
        $whenKaji = formatWhenKaji($_da['when']);

        $style = "";
        if ($_da['LY'] % 2 == 0) {
            $style .= 'background-color: #ff1987; color: white;';
        }
        else {
            $style .= 'background-color: black; color: #ff1987;';            
        }

        // $style .= $_da['LY'];

        if ($_da['position'] == null) {
            $style = 'background-color: red; color: white;';
        }
        if ($_da['when'] == '404') {
            $style .= 'text-decoration: line-through;';
        }
        if ($_da['when'] == 1212) {
            $style = 'background-color: yellow; color: black';
        }

        $DOOT = new DateTime('2024-07-25 00:00:00');
        $THIS86 = new DateTime('2024-08-06 23:59:59');
        $LGATE = "&nbsp;🦁&nbsp;🐬&nbsp;";

        if ($_da['datetime'] >= $DOOT && $_da['datetime'] <= $THIS86) {
            $loopCode = $LGATE;
        }

        $kount = sprintf('%03d', $_da['kount']);

        $d = DateTime::createFromFormat('m-d-Y', $_da['date']);
        $date1260 = $d->format('M-d');

        if ($date1260 == "Dec-21") {
            $style .= 'background-color: blue; color: white;';
        }

        if ($date1260 == "Jul-25") { 
            $style .= 'background-color: red; color: white;';
        }

        if ($date1260 == "Oct-08") { 
            $style .= 'background-color: red; color: white;';
            $loopCode = $LGATE . "🛑";
        }

        $HIDE_FUTURE_LOOP = TRUE;
        if ($HIDE_FUTURE_LOOP) {
            if ($_da['when'] == 333) {
                $loopCode = "";
            }
        }

        $DROP .= '<tr style="' . $style . '">';
        $DROP .= "<td>" . $kount . "</td>";
        $DROP .= '<td style="font-size: 0.9em">' . $date . '</td>';
        $DROP .= '<td style="text-decoration: line-through">' . $date1260 . "</td>";
        $DROP .= '<td>' . $whenKaji. '</td>';        
        $DROP .= '<td>' . $loopCode . '</td>';
        $DROP .= '<td>' . $position. '</td>';
        $DROP .= '</tr>' . PHP_EOL;
    }

    $DROP .= '</table>';
    return $DROP;
}

$startDate = "12-12-2023";
$TEXAS_CENTRAL = 'America/Chicago';
$DAYS_OUTTA_TIME = 1;
$LOOPS_TO_SHOW = 26;
$DA_KOUNT = $LOOPS_TO_SHOW * 13 + $DAYS_OUTTA_TIME;

$BODY =<<<HTML
<pre>
<h2>the:L00P</h2>

HTML;

date_default_timezone_set($TEXAS_CENTRAL);
$_kalada = generateKalada($startDate, $DA_KOUNT);
$BODY .= printKalada($_kalada);

$BODY .=<<<HTML


202X ☠️ 808
Gregorian Loop kalada
<a href="loopy?PH3=1">loopy.PH3</a>
</pre>
HTML;


reflekt:
require_once "$ZS1/hhana4.print.php";