<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package United Blog
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function united_blog_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	return $classes;
}
add_filter( 'body_class', 'united_blog_body_classes' );

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function united_blog_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'united_blog_pingback_header' );

/**
 * Minor header styles/scripts that need to run before other styles/scripts
 */
function united_blog_head_scripts()
{
    ?>
    <script type="text/javascript">
        (function() {
            const storageKey = 'theme-preference';
            const theme = {
                value: getColorPreference()
            };

            function getColorPreference() {
                try {
                    if (localStorage.getItem(storageKey)) {
                        return localStorage.getItem(storageKey);
                    } else {
                        return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
                    }
                } catch (e) {
                    console.error('Could not access localStorage', e);
                    return 'light'; // Default to light theme if localStorage is not available
                }
            }

            function setPreference() {
                try {
                    localStorage.setItem(storageKey, theme.value);
                } catch (e) {
                    console.error('Could not save to localStorage', e);
                }
                reflectPreference();
            }

            function reflectPreference() {
                document.firstElementChild.setAttribute("data-theme", theme.value);
                const toggleButtons = document.querySelectorAll(".site-button-mode");

                toggleButtons.forEach(function(button) {
                    button.setAttribute("aria-label", `Switch to ${theme.value === 'light' ? 'dark' : 'light'} mode`);
                    button.setAttribute("data-theme", theme.value);
                });
            }

            // Initial theme application
            reflectPreference();

            window.addEventListener('load', function() {
                reflectPreference();

                // Attach event listeners to toggle buttons
                const toggleButtons = document.querySelectorAll(".site-button-mode");

                toggleButtons.forEach(function(button) {
                    button.addEventListener("click", function() {
                        theme.value = theme.value === 'light' ? 'dark' : 'light';
                        setPreference();
                    });
                });
            });

            // Sync with system theme changes
            window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function(event) {
                theme.value = event.matches ? 'dark' : 'light';
                setPreference();
            });

        })();
    </script>
    <?php
}

function united_blog_setup_dark_mode() {
    if (united_blog_get_option('show_light_dark_switch')) {
        add_action('wp_head', 'united_blog_head_scripts');
    }
}
add_action('init', 'united_blog_setup_dark_mode');

if (!function_exists('united_blog_dark_mode')) {

    function united_blog_dark_mode()
    {

        if (united_blog_get_option('force_dark_mode') == 1) {
            echo "data-theme='dark'";
        }
    }

}


if (!function_exists('united_blog_placeholder_image')) :
    /**
     * Get placeholder image
     *
     * @since 1.0.0
     */
    function united_blog_placeholder_image()
    {
        $src = get_template_directory_uri() . '/images/placeholder.webp';
        return apply_filters('united_blog_placeholder_image', $src);
    }
endif;


if ( ! function_exists( 'united_blog_get_footer_layouts' ) ) :
	/**
	 * Returns footer layout options.
	 *
	 * @since 1.0.0
	 *
	 * @return array Options array.
	 */
	function united_blog_get_footer_layouts() {
		$options = apply_filters(
			'united_blog_footer_layouts',
			array(
				'footer_layout_1' => array(
					'url'   => get_template_directory_uri() . '/images/customizer/footer-col-4.webp',
					'label' => esc_html__( 'Four Columns', 'united-blog' ),
				),
				'footer_layout_2' => array(
					'url'   => get_template_directory_uri() . '/images/customizer/footer-col-3.webp',
					'label' => esc_html__( 'Three Columns', 'united-blog' ),
				),
				'footer_layout_3' => array(
					'url'   => get_template_directory_uri() . '/images/customizer/footer-col-2.webp',
					'label' => esc_html__( 'Two Columns', 'united-blog' ),
				),
				'footer_layout_4' => array(
					'url'   => get_template_directory_uri() . '/images/customizer/footer-col-2-big-left.webp',
					'label' => esc_html__( 'Two Columns Big Left', 'united-blog' ),
				),
				'footer_layout_5' => array(
					'url'   => get_template_directory_uri() . '/images/customizer/footer-col-3-big-middle.webp',
					'label' => esc_html__( 'Three Columns Big Middle', 'united-blog' ),
				),
				'footer_layout_6' => array(
					'url'   => get_template_directory_uri() . '/images/customizer/footer-col-2-big-right.webp',
					'label' => esc_html__( 'Two Columns Big Right', 'united-blog' ),
				),
				'footer_layout_7' => array(
					'url'   => get_template_directory_uri() . '/images/customizer/footer-col-1.webp',
					'label' => esc_html__( 'Single Column', 'united-blog' ),
				),
			)
		);
		return $options;
	}
endif;

