/**
 * print the information for an individual chart box
 *
 * find and print a given individuals information for a pedigree chart
 *
 * @param WT_Individual $person The person to print
 * @param int           $style  the style to print the box in, 1 for smaller boxes, 2 for larger boxes
 * @param int           $count  on some charts it is important to keep a count of how many boxes were printed
 * @param string        $personcount
 */
function print_pedigree_person($person, $style = 1, $count = 0, $personcount = "1")
{
    global $GEDCOM;
    global $SHOW_HIGHLIGHT_IMAGES, $bwidth, $bheight, $PEDIGREE_FULL_DETAILS, $SHOW_PEDIGREE_PLACES;
    global $TEXT_DIRECTION, $DEFAULT_PEDIGREE_GENERATIONS, $OLD_PGENS, $talloffset, $PEDIGREE_LAYOUT;
    global $chart_style, $box_width, $generations, $show_spouse, $show_full;
    global $CHART_BOX_TAGS, $SHOW_LDS_AT_GLANCE, $PEDIGREE_SHOW_GENDER;
    global $SEARCH_SPIDER;
    if ($style != 2) {
        $style = 1;
    }
    if (empty($show_full)) {
        $show_full = 0;
    }
    if (empty($PEDIGREE_FULL_DETAILS)) {
        $PEDIGREE_FULL_DETAILS = 0;
    }
    if (!isset($OLD_PGENS)) {
        $OLD_PGENS = $DEFAULT_PEDIGREE_GENERATIONS;
    }
    if (!isset($talloffset)) {
        $talloffset = $PEDIGREE_LAYOUT;
    }
    // NOTE: Start div out-rand()
    if (!$person) {
        echo "<div id=\"out-", Uuid::uuid4(), "\" class=\"person_boxNN\" style=\"width: ", $bwidth, "px; height: ", $bheight, "px; overflow: hidden;\">";
        echo '<br>';
        echo '</div>';
        return;
    }
    $pid = $person->getXref();
    if ($count == 0) {
        $count = rand();
    }
    $lbwidth = $bwidth * 0.75;
    if ($lbwidth < 150) {
        $lbwidth = 150;
    }
    $tmp = array('M' => '', 'F' => 'F', 'U' => 'NN');
    $isF = $tmp[$person->getSex()];
    $personlinks = '';
    $icons = '';
    $genderImage = '';
    $BirthDeath = '';
    $birthplace = '';
    $outBoxAdd = '';
    $showid = '';
    $iconsStyleAdd = 'float:right;';
    if ($TEXT_DIRECTION == 'rtl') {
        $iconsStyleAdd = 'float:left;';
    }
    $boxID = $pid . '.' . $personcount . '.' . $count . '.' . Uuid::uuid4();
    $mouseAction4 = " onclick=\"expandbox('" . $boxID . "', {$style}); return false;\"";
    if ($person->canShowName()) {
        if (empty($SEARCH_SPIDER)) {
            //-- draw a box for the family popup
            // NOTE: Start div I.$pid.$personcount.$count.links
            $personlinks .= '<ul class="person_box' . $isF . '">';
            $personlinks .= '<li><a href="pedigree.php?rootid=' . $pid . '&amp;show_full=' . $PEDIGREE_FULL_DETAILS . '&amp;PEDIGREE_GENERATIONS=' . $OLD_PGENS . '&amp;talloffset=' . $talloffset . '&amp;ged=' . rawurlencode($GEDCOM) . '"><b>' . WT_I18N::translate('Pedigree') . '</b></a></li>';
            if (array_key_exists('googlemap', WT_Module::getActiveModules())) {
                $personlinks .= '<li><a href="module.php?mod=googlemap&amp;mod_action=pedigree_map&amp;rootid=' . $pid . '&amp;ged=' . WT_GEDURL . '"><b>' . WT_I18N::translate('Pedigree map') . '</b></a></li>';
            }
            if (WT_USER_GEDCOM_ID && WT_USER_GEDCOM_ID != $pid) {
                $personlinks .= '<li><a href="relationship.php?show_full=' . $PEDIGREE_FULL_DETAILS . '&amp;pid1=' . WT_USER_GEDCOM_ID . '&amp;pid2=' . $pid . '&amp;show_full=' . $PEDIGREE_FULL_DETAILS . '&amp;pretty=2&amp;followspouse=1&amp;ged=' . WT_GEDURL . '"><b>' . WT_I18N::translate('Relationship to me') . '</b></a></li>';
            }
            $personlinks .= '<li><a href="descendancy.php?rootid=' . $pid . '&amp;show_full=' . $PEDIGREE_FULL_DETAILS . '&amp;generations=' . $generations . '&amp;box_width=' . $box_width . '&amp;ged=' . rawurlencode($GEDCOM) . '"><b>' . WT_I18N::translate('Descendants') . '</b></a></li>';
            $personlinks .= '<li><a href="ancestry.php?rootid=' . $pid . '&amp;show_full=' . $PEDIGREE_FULL_DETAILS . '&amp;chart_style=' . $chart_style . '&amp;PEDIGREE_GENERATIONS=' . $OLD_PGENS . '&amp;box_width=' . $box_width . '&amp;ged=' . rawurlencode($GEDCOM) . '"><b>' . WT_I18N::translate('Ancestors') . '</b></a></li>';
            $personlinks .= '<li><a href="compact.php?rootid=' . $pid . '&amp;ged=' . rawurlencode($GEDCOM) . '"><b>' . WT_I18N::translate('Compact tree') . '</b></a></li>';
            if (function_exists("imagettftext")) {
                $personlinks .= '<li><a href="fanchart.php?rootid=' . $pid . '&amp;PEDIGREE_GENERATIONS=' . $OLD_PGENS . '&amp;ged=' . rawurlencode($GEDCOM) . '"><b>' . WT_I18N::translate('Fan chart') . '</b></a></li>';
            }
            $personlinks .= '<li><a href="hourglass.php?rootid=' . $pid . '&amp;show_full=' . $PEDIGREE_FULL_DETAILS . '&amp;chart_style=' . $chart_style . '&amp;PEDIGREE_GENERATIONS=' . $OLD_PGENS . '&amp;box_width=' . $box_width . '&amp;ged=' . rawurlencode($GEDCOM) . '&amp;show_spouse=' . $show_spouse . '"><b>' . WT_I18N::translate('Hourglass chart') . '</b></a></li>';
            if (array_key_exists('tree', WT_Module::getActiveModules())) {
                $personlinks .= '<li><a href="module.php?mod=tree&amp;mod_action=treeview&amp;ged=' . WT_GEDURL . '&amp;rootid=' . $pid . '"><b>' . WT_I18N::translate('Interactive tree') . '</b></a></li>';
            }
            foreach ($person->getSpouseFamilies() as $family) {
                $spouse = $family->getSpouse($person);
                if ($spouse) {
                    $personlinks .= '<li>';
                    $personlinks .= '<a href="' . $family->getHtmlUrl() . '"><b>' . WT_I18N::translate('Family with spouse') . '</b></a><br>';
                    $personlinks .= '<a href="' . $spouse->getHtmlUrl() . '">' . $spouse->getFullName() . '</a>';
                    $personlinks .= '</li>';
                    $personlinks .= '<li><ul>';
                }
                foreach ($family->getChildren() as $child) {
                    $personlinks .= '<li><a href="' . $child->getHtmlUrl() . '">';
                    $personlinks .= $child->getFullName();
                    $personlinks .= '</a></li>';
                }
                $personlinks .= '</ul></li>';
            }
            $personlinks .= '</ul>';
            // NOTE: Start div out-$pid.$personcount.$count
            if ($style == 1) {
                $outBoxAdd .= " class=\"person_box{$isF} person_box_template style1\" style=\"width: " . $bwidth . "px; height: " . $bheight . "px; z-index:-1;\"";
            } else {
                $outBoxAdd .= " class=\"person_box{$isF} person_box_template style0\"";
            }
            // NOTE: Zoom
            if (!$show_full) {
                $outBoxAdd .= $mouseAction4;
            } else {
                $icons .= "<a href=\"#\"" . $mouseAction4 . " id=\"iconz-{$boxID}\" class=\"icon-zoomin\" title=\"" . WT_I18N::translate('Zoom in/out on this box.') . "\"></a>";
                $icons .= '<div class="itr"><a href="#" class="icon-pedigree"></a><div class="popup">' . $personlinks . '</div></div>';
            }
        } else {
            if ($style == 1) {
                $outBoxAdd .= "class=\"person_box{$isF}\" style=\"width: " . $bwidth . "px; height: " . $bheight . "px; overflow: hidden;\"";
            } else {
                $outBoxAdd .= "class=\"person_box{$isF}\" style=\"overflow: hidden;\"";
            }
            // NOTE: Zoom
            if (!$SEARCH_SPIDER) {
                $outBoxAdd .= $mouseAction4;
            }
        }
    } else {
        if ($style == 1) {
            $outBoxAdd .= "class=\"person_box{$isF} person_box_template style1\" style=\"width: " . $bwidth . "px; height: " . $bheight . "px;\"";
        } else {
            $outBoxAdd .= "class=\"person_box{$isF} person_box_template style0\"";
        }
    }
    //-- find the name
    $name = $person->getFullName();
    $shortname = $person->getShortName();
    if ($SHOW_HIGHLIGHT_IMAGES) {
        $thumbnail = $person->displayImage();
    } else {
        $thumbnail = '';
    }
    //-- find additional name, e.g. Hebrew
    $addname = $person->getAddName();
    if ($PEDIGREE_SHOW_GENDER && $show_full) {
        $genderImage = " " . $person->getSexImage('small', "box-{$boxID}-gender");
    }
    // Here for alternate name2
    if ($addname) {
        $addname = "<br><span id=\"addnamedef-{$boxID}\" class=\"name1\"> " . $addname . "</span>";
    }
    if ($SHOW_LDS_AT_GLANCE && $show_full) {
        $addname = ' <span class="details$style">' . get_lds_glance($person) . '</span>' . $addname;
    }
    if ($show_full && $person->canShow()) {
        $opt_tags = preg_split('/\\W/', $CHART_BOX_TAGS, 0, PREG_SPLIT_NO_EMPTY);
        // Show BIRT or equivalent event
        foreach (explode('|', WT_EVENTS_BIRT) as $birttag) {
            if (!in_array($birttag, $opt_tags)) {
                $event = $person->getFirstFact($birttag);
                if ($event) {
                    $BirthDeath .= $event->summary();
                    break;
                }
            }
        }
        // Show optional events (before death)
        foreach ($opt_tags as $key => $tag) {
            if (!preg_match('/^(' . WT_EVENTS_DEAT . ')$/', $tag)) {
                $event = $person->getFirstFact($tag);
                if (!is_null($event)) {
                    $BirthDeath .= $event->summary();
                    unset($opt_tags[$key]);
                }
            }
        }
        // Show DEAT or equivalent event
        foreach (explode('|', WT_EVENTS_DEAT) as $deattag) {
            $event = $person->getFirstFact($deattag);
            if ($event) {
                $BirthDeath .= $event->summary();
                if (in_array($deattag, $opt_tags)) {
                    unset($opt_tags[array_search($deattag, $opt_tags)]);
                }
                break;
            }
        }
        // Show remaining optional events (after death)
        foreach ($opt_tags as $tag) {
            $event = $person->getFirstFact($tag);
            if ($event) {
                $BirthDeath .= $event->summary();
            }
        }
    }
    // Output to template
    $classfacts = '';
    if ($show_full) {
        require WT_THEME_DIR . 'templates/personbox_template.php';
    } else {
        require WT_THEME_DIR . 'templates/compactbox_template.php';
    }
}