<?php
/**
 * Pawfect Social Media Icons
 *
 * Handles social media icons display functionality.
 *
 * @package Pawfect
 * @since 1.0.0
 */

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

/**
 * Helper function to get social media icons
 *
 * @param array $args Configuration options for icons.
 * @return string HTML output of social icons.
 */
function pawfect_get_social_media_icons($args = array()) {
    // Check if social media is enabled
    if (!get_theme_mod('pawfect_social_media_enable', true)) {
        return '';
    }

    // Get social media icons from customizer repeater (using pawfect_Repeater format)
    $default_icons = json_encode(array(
        array('title' => 'Facebook', 'link' => '#', 'icon_value' => 'fab fa-facebook-f'),
        array('title' => 'Twitter', 'link' => '#', 'icon_value' => 'fab fa-twitter'),
        array('title' => 'Instagram', 'link' => '#', 'icon_value' => 'fab fa-instagram')
    ));
    $social_icons = get_theme_mod('pawfect_social_media_icons', $default_icons);
    $social_icons = json_decode($social_icons, true);

    if (!is_array($social_icons) || empty($social_icons)) {
        return '';
    }

    // Filter out empty URLs and icons
    $social_icons = array_filter($social_icons, function($icon) {
        return !empty($icon['link']) && !empty($icon['icon_value']);
    });

    if (empty($social_icons)) {
        return '';
    }

    // Default arguments
    $defaults = array(
        'container_class' => 'pawfect-social-icons',
        'icon_class' => 'pawfect-social-icon',
        'color' => '#6B7280',
        'hover_color' => '#F4A259',
        'target' => '_blank',
        'show_labels' => false,
        'show_title' => true,
        'icon_size' => ''
    );

    $args = wp_parse_args($args, $defaults);

    // Build CSS classes
    $container_classes = array($args['container_class']);
    $icon_classes = array($args['icon_class']);

    // Add icon size class if specified
    if (!empty($args['icon_size'])) {
        $icon_classes[] = $args['icon_size'];
    }

    // Start building HTML
    $html = '<div class="' . esc_attr(implode(' ', $container_classes)) . '">';

    foreach ($social_icons as $icon) {
        $name = !empty($icon['title']) ? $icon['title'] : '';
        $url = esc_url($icon['link']);
        $icon_class = esc_attr($icon['icon_value']);

        $html .= '<a href="' . $url . '" target="' . esc_attr($args['target']) . '" rel="noopener noreferrer" class="' . esc_attr(implode(' ', $icon_classes)) . '"';

        if (!empty($name) && $args['show_title']) {
            $html .= ' title="' . esc_attr($name) . '" aria-label="' . esc_attr($name) . '"';
        }

        $html .= '>';
        $html .= '<i class="' . $icon_class . '"></i>';

        if ($args['show_labels'] && !empty($name)) {
            $html .= '<span class="pawfect-social-label">' . esc_html($name) . '</span>';
        }

        $html .= '</a>';
    }

    $html .= '</div>';

    return $html;
}

/**
 * Display social media icons (for use in templates)
 *
 * @param array $args Configuration options for icons.
 */
function pawfect_social_media_icons($args = array()) {
    echo wp_kses_post( pawfect_get_social_media_icons($args) );
}
