<?php

require_once __DIR__ . '/../Include/Config.php';
require_once __DIR__ . '/../Include/PageInit.php';

use ChurchCRM\dto\SystemConfig;
use ChurchCRM\dto\SystemURLs;
use ChurchCRM\Service\FinancialService;
use ChurchCRM\model\ChurchCRM\Base\ListOptionQuery;
use ChurchCRM\model\ChurchCRM\Base\Person2group2roleP2g2rQuery;
use ChurchCRM\model\ChurchCRM\GroupQuery;
use ChurchCRM\model\ChurchCRM\Map\PersonTableMap;
use ChurchCRM\Reports\PdfAttendance;
use ChurchCRM\Utils\InputUtils;
use ChurchCRM\Utils\LoggerUtils;

$iGroupID = InputUtils::legacyFilterInput($_GET['GroupID']);
$aGrp = explode(',', $iGroupID);
$nGrps = count($aGrp);
LoggerUtils::getAppLogger()->debug("Group ID = {$iGroupID}");

$iFYID = (int) InputUtils::legacyFilterInput($_GET['FYID'], 'int');

$tFirstSunday = InputUtils::legacyFilterInput($_GET['FirstSunday']);
$tLastSunday = InputUtils::legacyFilterInput($_GET['LastSunday']);
$tAllRoles = InputUtils::legacyFilterInput($_GET['AllRoles'], 'int');
$withPictures = InputUtils::legacyFilterInput($_GET['withPictures'], 'int');

LoggerUtils::getAppLogger()->debug("All roles = {$tAllRoles}");

$tNoSchool1 = InputUtils::legacyFilterInputArr($_GET, 'NoSchool1');
$tNoSchool2 = InputUtils::legacyFilterInputArr($_GET, 'NoSchool2');
$tNoSchool3 = InputUtils::legacyFilterInputArr($_GET, 'NoSchool3');
$tNoSchool4 = InputUtils::legacyFilterInputArr($_GET, 'NoSchool4');
$tNoSchool5 = InputUtils::legacyFilterInputArr($_GET, 'NoSchool5');
$tNoSchool6 = InputUtils::legacyFilterInputArr($_GET, 'NoSchool6');
$tNoSchool7 = InputUtils::legacyFilterInputArr($_GET, 'NoSchool7');
$tNoSchool8 = InputUtils::legacyFilterInputArr($_GET, 'NoSchool8');

$iExtraStudents = (int)InputUtils::legacyFilterInputArr($_GET, 'ExtraStudents', 'int');
$iExtraTeachers = (int)InputUtils::legacyFilterInputArr($_GET, 'ExtraTeachers', 'int');

$dFirstSunday = strtotime($tFirstSunday);
$dLastSunday = strtotime($tLastSunday);

$dNoSchool1 = strtotime($tNoSchool1);
$dNoSchool2 = strtotime($tNoSchool2);
$dNoSchool3 = strtotime($tNoSchool3);
$dNoSchool4 = strtotime($tNoSchool4);
$dNoSchool5 = strtotime($tNoSchool5);
$dNoSchool6 = strtotime($tNoSchool6);
$dNoSchool7 = strtotime($tNoSchool7);
$dNoSchool8 = strtotime($tNoSchool8);

// Reformat the dates to get standardized text representation
$tFirstSunday = date('Y-m-d', $dFirstSunday);
$tLastSunday = date('Y-m-d', $dLastSunday);

$tNoSchool1 = date('Y-m-d', $dNoSchool1);
$tNoSchool2 = date('Y-m-d', $dNoSchool2);
$tNoSchool3 = date('Y-m-d', $dNoSchool3);
$tNoSchool4 = date('Y-m-d', $dNoSchool4);
$tNoSchool5 = date('Y-m-d', $dNoSchool5);
$tNoSchool6 = date('Y-m-d', $dNoSchool6);
$tNoSchool7 = date('Y-m-d', $dNoSchool7);
$tNoSchool8 = date('Y-m-d', $dNoSchool8);

// Instantiate the class and build the report.
$yTitle = 20;
$yTeachers = $yTitle + 6;
$nameX = 10;
$epd = 3;

$pdf = new PdfAttendance();

for ($i = 0; $i < $nGrps; $i++) {
    $iGroupID = $aGrp[$i];

    if ($i > 0) {
        $pdf->addPage();
    }
    //Get the data on this group
    $group = GroupQuery::create()->findOneById($iGroupID);
    
    // Skip if group not found
    if ($group === null) {
        LoggerUtils::getAppLogger()->warning("ClassAttendance: Group with ID $iGroupID not found");
        continue;
    }

    $FYString = FinancialService::formatFiscalYear($iFYID);

    $reportHeader = str_pad($group->getName(), 95) . $FYString;

    // Build the teacher string -- first teachers, then the liaison
    $teacherString = gettext('Teachers') . ': ';
    $bFirstTeacher = true;
    $iTeacherCnt = 0;
    $iMaxTeachersFit = 4;
    $iStudentCnt = 0;

    $groupRoleMemberships = Person2group2roleP2g2rQuery::create()
            ->joinWithPerson()
            ->orderBy(PersonTableMap::COL_PER_LASTNAME)
            ->_and()->orderBy(PersonTableMap::COL_PER_FIRSTNAME)
            ->findByGroupId($iGroupID);

    if ($tAllRoles != 1) {
        $liaisonString = '';

        foreach ($groupRoleMemberships as $groupRoleMembership) {
            $person = $groupRoleMembership->getPerson();
            $family = $person->getFamily();

            $homePhone = '';
            if (!empty($family)) {
                $homePhone = $family->getHomePhone();
            }

            $groupRole = ListOptionQuery::create()->filterById($group->getRoleListId())->filterByOptionId($groupRoleMembership->getRoleId())->findOne();

            $lst_OptionName = $groupRole->getOptionName();

            if ($lst_OptionName === 'Teacher') {
                // Make an array of teachers while we're here
                $aTeachers[$iTeacherCnt] = $person;
                if (!$bFirstTeacher) {
                    $teacherString .= ', ';
                }
                $teacherString .= $person->getFullName();
                $bFirstTeacher = false;

                // Use main photo (Photo::PHOTO_WIDTH x Photo::PHOTO_HEIGHT PNG) - no thumbnail needed
                $aTeachersIMG[$iTeacherCnt++] = str_replace(SystemURLs::getDocumentRoot(), '', $person->getPhoto()->getPhotoURI());
            } elseif ($lst_OptionName === 'Student') {
                $aStudents[$iStudentCnt] = $person;

                // Use main photo (Photo::PHOTO_WIDTH x Photo::PHOTO_HEIGHT PNG) - no thumbnail needed
                $aStudentsIMG[$iStudentCnt++] = $person->getPhoto()->getPhotoURI();
            } elseif ($lst_OptionName == gettext('Liaison')) {
                $liaisonString .= gettext('Liaison') . ': ' . $person->getFullName() . ' ' . $homePhone . ' ';
            }
        }

        if ($iTeacherCnt < $iMaxTeachersFit) {
            $teacherString .= '  ' . $liaisonString;
        }

        $pdf->SetFont('Times', 'B', 12);

        $y = $yTeachers;
        $pdf->writeAt($nameX, $y, $teacherString);
        $y += 4;

        if ($iTeacherCnt >= $iMaxTeachersFit) {
            $pdf->writeAt($nameX, $y, $liaisonString);
            $y += 4;
        }

        $y = $pdf->drawAttendanceCalendar(
            $nameX,
            $y + 6,
            $aStudents,
            gettext('Students'),
            $iExtraStudents,
            $tFirstSunday,
            $tLastSunday,
            $tNoSchool1,
            $tNoSchool2,
            $tNoSchool3,
            $tNoSchool4,
            $tNoSchool5,
            $tNoSchool6,
            $tNoSchool7,
            $tNoSchool8,
            $reportHeader,
            $aStudentsIMG,
            $withPictures
        );

        // We start a new page
        if ($y > $yTeachers + 10) {
            $pdf->addPage();
        }

        $y = $yTeachers;
        $pdf->drawAttendanceCalendar(
            $nameX,
            $y + 6,
            $aTeachers,
            gettext('Teachers'),
            $iExtraTeachers,
            $tFirstSunday,
            $tLastSunday,
            $tNoSchool1,
            $tNoSchool2,
            $tNoSchool3,
            $tNoSchool4,
            $tNoSchool5,
            $tNoSchool6,
            $tNoSchool7,
            $tNoSchool8,
            '',
            $aTeachersIMG,
            $withPictures
        );
    } else {
        // Print all roles on the attendance sheet
        $iStudentCnt = 0;

        unset($aStudents);

        foreach ($groupRoleMemberships as $groupRoleMembership) {
            $person = $groupRoleMembership->getPerson();

            $aStudents[$iStudentCnt] = $groupRoleMembership->getPerson();
            // Use main photo (Photo::PHOTO_WIDTH x Photo::PHOTO_HEIGHT PNG) - no thumbnail needed
            $aStudentsIMG[$iStudentCnt++] = $person->getPhoto()->getPhotoURI();
        }

        $pdf->SetFont('Times', 'B', 12);

        $y = $yTeachers;

        $y = $pdf->drawAttendanceCalendar(
            $nameX,
            $y + 6,
            $aStudents,
            gettext('All Members'),
            $iExtraStudents + $iExtraTeachers,
            $tFirstSunday,
            $tLastSunday,
            $tNoSchool1,
            $tNoSchool2,
            $tNoSchool3,
            $tNoSchool4,
            $tNoSchool5,
            $tNoSchool6,
            $tNoSchool7,
            $tNoSchool8,
            $reportHeader,
            $aStudentsIMG,
            $withPictures
        );
    }
}

if (SystemConfig::getIntValue('iPDFOutputType') === 1) {
    $pdf->Output('ClassAttendance' . date(SystemConfig::getValue('sDateFilenameFormat')) . '.pdf', 'D');
} else {
    $pdf->Output();
}
