<?php
/**
 * Map Display Functions
 *
 * @package DACdbPlus
 * @subpackage Maps
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * Generates a Google Map display for club locations
 *
 * @param array $atts Shortcode attributes
 * @return string HTML and JavaScript for map display
 */
function dacdb_map($atts) {
    global $fDebug, $data_path, $file_path, $options;

    $map = '';

    // Retrieve global variables
    $account_id = $options['dacdb_district'];
    $club_id    = $options['dacdb_club'];
    $style      = isset($atts['style']) ? $atts['style'] : 1;

    // Append debug information if debugging is enabled
    if ($fDebug) {
        $map .= "=================================================================<BR>";
        $map .= "function dacdb_map called<br>";
        $map .= "AccountID=" . $options['dacdb_district'] . "<br>";
        $map .= "ClubID=" . $options['dacdb_club'] . "<br>";
        $map .= "ApiKey=" . $options['map_api'] . "<br>";
        $map .= "Style=" . $style . "<br>";
        $map .= "URL=" . $data_path . 'MapClub.cfm?AccountID=' . $options['dacdb_district'] . '&xsl=ClubData.xsl&callback=showPopUpData&zlevel=0' . "<br>";
        $map .= "URL=" . $data_path . 'MapPopUp.cfm?AccountID=' . $options['dacdb_district'] . '&xsl=ClubData.xsl&callback=showPopUpData&zlevel=0' . "<br>";
        $map .= "=================================================================<BR>";
    } else {
        // Display a message if the Google Map API key is missing
        if ($options['map_api'] == '') {
            $map .= "<div class=\"error-msg\"><i class=\"fa fa-times-circle\"></i>Google Map API Key missing</div>";
        }
    }

    // Generate HTML and JavaScript code for the map
    $map .= '
        <div id="Map"></div>
        <div id="map_canvas" style="min-height:600px;"></div>
        <div id="MapData" style="display:none;"></div>
        <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="  crossorigin="anonymous"></script>
        <script defer type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=' . $options['map_api'] . '"></script>

        <script type="text/javascript">
            var temp = "";
            function showMap(json) {
                console.log(json);
                temp = json.text;
            }
            function showPopUpData(json) {
                document.getElementById(\'MapData\').innerHTML = json.text;
            }
            $(document).ready(function() {
                eval(temp);
                $("#Map").hide();
                $("#MapData").hide();
            });
        </script>
        <script src="' . $data_path . 'MapClub.cfm?AccountID=' . $options['dacdb_district'] . '&xsl=ClubInteractive2.xsl&callback=showMap&zlevel=0" type="text/javascript"></script>
        <script src="' . $data_path . 'MapPopUp.cfm?AccountID=' . $options['dacdb_district'] . '&xsl=ClubData.xsl&callback=showPopUpData&zlevel=0" type="text/javascript"></script>
    ';

    return $map;
}

/**
 * Enqueue necessary styles for the map
 */
function dacdb_map_styles() {
    wp_enqueue_style(
        'dacdb-map-styles', 
        plugins_url('css/map-styles.css', dirname(__FILE__)), 
        array(), 
        MMPDB_PLUGIN_VERSION
    );
}

/**
 * Register map shortcode
 */
function dacdb_register_map_shortcode() {
    add_shortcode('dacdb_map', 'dacdb_map');
}
add_action('init', 'dacdb_register_map_shortcode');

/**
 * Conditionally load map styles when shortcode is present
 */
function dacdb_maybe_load_map_styles() {
    global $post;
    if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'dacdb_map')) {
        add_action('wp_enqueue_scripts', 'dacdb_map_styles');
    }
}
add_action('wp', 'dacdb_maybe_load_map_styles');