<?php
/**
 * Debug Functions for DACdb Plus Plugin
 *
 * @package    DACdb Plus
 * @author     Member Minder Pro, LLC
 * @copyright  2023 Member Minder Pro, LLC
 * @license    GPL-2.0+
 * @link       https://github.com/memberminderpro/dacdbplus
 *
 * This file contains debugging functions used throughout the DACdb Plus plugin
 * to assist in development and troubleshooting.
 */

// SECTION - Debug Output Function
/**
 * Outputs debug information for DACdb Plus functions
 *
 * @param string $function_name    Name of the function being debugged
 * @param array  $atts            Shortcode attributes
 * @param array  $options         Plugin options
 * @param array  $additional_vars Additional variables to display
 * @param object $xml             XML object to format and display
 * 
 * @return void
 */
function dacdb_debug($function_name, $atts, $options, $additional_vars, $xml)
{
    // ANCHOR - Debug Check
    if (!$GLOBALS['fDebug']) {
        return; // Exit if debugging is not enabled
    }

    // ANCHOR - Function Name Output
    echo "<h5>function <em><strong>$function_name</strong></em> called:</h5>";

    // ANCHOR - Options Display
    echo '<h4 class="drawer">Options</h4><pre class="panel">';
    foreach ($options as $key => $value) {
        echo "<dt>\$options['$key']</dt><dd>$value</dd>";
    }
    echo '</pre>';

    // ANCHOR - Shortcode Attributes Display
    if (count($atts) > 0) {
        echo '<h4 class="drawer">Shortcode Attributes</h4><pre class="panel">';
        foreach ($atts as $key => $value) {
            echo "<dt>[\$atts] $key</dt><dd>$value</dd>";
        }
        echo '</pre>';
    }

    // ANCHOR - Additional Variables Display
    if (count($additional_vars) > 0) {
        echo '<h4 class="drawer">Named Variables</h4><pre class="panel">';
        foreach ($additional_vars as $key => $value) {
            // NOTE - Special handling for XML formatting
            if ($key == 'xml') {
                echo "<dt>\$xml</dt><dd>";
                // TODO - Implement XML formatting and display code
                echo "</dd>";
            } else {
                echo "<dt>\$$key</dt><dd>$value</dd>";
            }
        }
        echo '</pre>';
    }

    // ANCHOR - XML Output Display
    if (count($xml) > 0) {
        echo '<h4 class="drawer">XML Output</h4><div class="panel">';
        foreach ($xml->element as $content) {
            $dom = new DOMDocument();
            $dom->preserveWhiteSpace = false; // Don't preserve whitespaces
            $dom->formatOutput = true; // Format output with indentation and line breaks
            $dom->loadXML($content->asXML()); // Load the XML string from SimpleXMLElement
            $formattedXml = $dom->saveXML(); // Save the formatted XML to a string
            echo '<pre>' . htmlspecialchars($formattedXml) . '</pre>';
        }
        echo '</div>';
    }
}

// SECTION - Shortcode Debug Function
/**
 * Debugs shortcode usage on a page
 *
 * @param object $post Post object containing the content
 * @param array  $atts Shortcode attributes to analyze
 * 
 * @return string Formatted debug information about the shortcode
 */
function dacdb_debug_shortcode($post, $atts)
{
    $pagedata = ''; // Initialize the variable to store debugging information

    // ANCHOR - Shortcode Existence Check
    if (has_shortcode($post->post_content, 'dacdb_plus')) {
        // NOTE - Skip 'show' parameter in iteration
        foreach ($atts as $param => $value) {
            if ($param !== 'show') {
                // REVIEW - Consider if additional parameter information should be displayed
                // $pagedata .= "Parameter: $param with value: $value<br>";
            }
        }

        // ANCHOR - Shortcode Display
        $shortcode_name = 'dacdb_plus';
        $pagedata .= "<code>[$shortcode_name";
        foreach ($atts as $param => $value) {
            $pagedata .= " $param=\"$value\"";
        }
        $pagedata .= "]</code>";
    } else {
        $pagedata .= "<code>[dacdb_plus]</code> shortcode not found on the page";
    }

    return $pagedata;
}