/* 
 Theme Name:   CAMP SUMMER USA
 Theme URI:    
 Description:  
 Author:       junglemaster
 Author URI:   https://campsummersusa.es
 Template:     hello-elementor
 Version:      1.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html

 /* == Add your own styles below this line ==
--------------------------------------------*/

function camp_activity_cloud_shortcode() {

    // Taxonomy slug
    $taxonomy = 'activity-type';

    // Camp Counsellor Jobs CPT slug
    $post_type = 'camp-counsellor-jobs';

    // Get all top-level parent terms
    $parents = get_terms(array(
        'taxonomy'   => $taxonomy,
        'hide_empty' => false,
        'parent'     => 0,
    ));

    if (is_wp_error($parents) || empty($parents)) {
        return '';
    }

    // Store all child activities here before sorting
    $activities = array();

    // Loop through parent categories
    foreach ($parents as $parent) {

        // Get colours from parent category
        $background_colour = get_term_meta(
            $parent->term_id,
            'colour',
            true
        );

        $font_colour = get_term_meta(
            $parent->term_id,
            'font_colour',
            true
        );

        // Fallback colours
        $background_colour = !empty($background_colour)
            ? $background_colour
            : '#eeeeee';

        $font_colour = !empty($font_colour)
            ? $font_colour
            : '#333333';

        // Get all direct child terms
        $children = get_terms(array(
            'taxonomy'   => $taxonomy,
            'hide_empty' => false,
            'parent'     => $parent->term_id,
        ));

        if (is_wp_error($children) || empty($children)) {
            continue;
        }

        // Loop through child terms
        foreach ($children as $child) {

            // Get SVG icon from JetEngine media field
            $icon_id = get_term_meta(
                $child->term_id,
                'icon',
                true
            );

            $icon_url = '';

            if (!empty($icon_id)) {

                // Handle array values
                if (is_array($icon_id)) {
                    $icon_id = reset($icon_id);
                }

                // Convert attachment ID to URL
                if (is_numeric($icon_id)) {
                    $icon_url = wp_get_attachment_url($icon_id);
                } else {
                    // Already a URL
                    $icon_url = $icon_id;
                }
            }

            // Find the job assigned to this Activity Type
            $jobs = get_posts(array(
                'post_type'      => $post_type,
                'posts_per_page' => 1,
                'post_status'    => 'publish',
                'tax_query'      => array(
                    array(
                        'taxonomy' => $taxonomy,
                        'field'    => 'term_id',
                        'terms'    => $child->term_id,
                    ),
                ),
            ));

            // Get job URL
            $job_url = '';

            if (!empty($jobs)) {
                $job_url = get_permalink($jobs[0]->ID);
            }

            // Add everything to the activities array
            $activities[] = array(
                'name'              => $child->name,
                'icon_url'          => $icon_url,
                'job_url'           => $job_url,
                'background_colour' => $background_colour,
                'font_colour'       => $font_colour,
            );
        }
    }

    // Sort ALL activities alphabetically by name
    usort($activities, function($a, $b) {
        return strcasecmp($a['name'], $b['name']);
    });

    // Start cloud
    $output = '<div class="camp-activity-cloud">';

    // Output sorted activities
    foreach ($activities as $activity) {

        // If a job exists, make it clickable
        if (!empty($activity['job_url'])) {

            $output .= '<a 
                href="' . esc_url($activity['job_url']) . '"
                class="activity-cloud-item"
                style="
                    --activity-bg: ' . esc_attr($activity['background_colour']) . ';
                    --activity-colour: ' . esc_attr($activity['font_colour']) . ';
                "
            >';

        } else {

            // No job found - display without a link
            $output .= '<span 
                class="activity-cloud-item activity-cloud-no-link"
                style="
                    --activity-bg: ' . esc_attr($activity['background_colour']) . ';
                    --activity-colour: ' . esc_attr($activity['font_colour']) . ';
                "
            >';
        }

        // SVG icon using CSS mask
        if (!empty($activity['icon_url'])) {

            $output .= '<span 
                class="activity-cloud-icon"
                style="--icon-url: url(\'' . esc_url($activity['icon_url']) . '\');"
                aria-hidden="true"
            ></span>';
        }

        // Activity name
        $output .= '<span class="activity-cloud-name">'
            . esc_html($activity['name'])
            . '</span>';

        // Close link/span
        if (!empty($activity['job_url'])) {
            $output .= '</a>';
        } else {
            $output .= '</span>';
        }
    }

    $output .= '</div>';

    return $output;
}

add_shortcode(
    'camp_activity_cloud',
    'camp_activity_cloud_shortcode'
);