<?php
add_action('xews_lite_top_header','xews_lite_site_branding', 10 );
add_action('xews_lite_top_header','xews_lite_top_left_header', 20 );
add_action('xews_lite_bottom_header','xews_lite_header_nav', 10 );
add_action('xews_lite_bottom_header','xews_lite_header_search', 20 );
add_action('xews_lite_main_header','xews_lite_header_structure_controller', 10 );

$defaults 			        = xews_lite_customizer_defaults();
$xews_lite_darkmode_enable  = get_theme_mod('xews_lite_darkmode_enable', $defaults['xews_lite_darkmode_enable']);
if( true == $xews_lite_darkmode_enable ){
    add_action( 'wp_head', 'xews_lite_head_scripts' );
}

function xews_lite_site_branding(){
    ?>
        
        <div class="site-branding">
            <?php
            if ( has_custom_logo() ) {
                the_custom_logo();
            }
            ?>
            <div class="site-title-desc-wrap">
                <?php
                $theme_name = get_bloginfo( 'name' );

                if ( is_front_page() && is_home() ) :
                    ?>
                <h1 class="site-title">
                    <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
                        <?php echo esc_html( $theme_name ); ?>
                    </a>
                </h1>
                <?php
                else : ?>
                    <p class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php echo esc_html( $theme_name ); ?></a></p>
                    <?php
                endif;
                $xews_lite_description = get_bloginfo( 'description', 'display' );
                if ( $xews_lite_description || is_customize_preview() ) :
                    ?>
                    <p class="site-description"><?php echo esc_html($xews_lite_description);?></p>
                    <?php 
                endif; ?>
            </div>
        </div><!-- .site-branding -->
<?php 
}


function xews_lite_header_nav(){
    $defaults 				        = xews_lite_customizer_defaults();
    $xews_lite_nav_hover_effect 	= get_theme_mod('xews_lite_nav_hover_effect', $defaults['xews_lite_nav_hover_effect']);

    ?>

    <div class="header-main-menu <?php echo esc_attr($xews_lite_nav_hover_effect)?> clear">
        <nav id="site-navigation" class="main-navigation">
            <button class="button is-text" id="mob-toggle-menu-button">
                <span class="button-inner-wrapper">
                    <span class="icon menu-icon"></span>
                </span>
            </button>
            
            <div class="menu-item-inner-wrapper">
                <button class="button btn-menu-close is-text" id="mob-menu-collapse">
                    <i class="fas fa-times"></i>
                </button>

                <?php
                wp_nav_menu( array(
                    'theme_location' => 'menu-1',
                    'menu_id'        => 'primary-menu',
                    'container_class' => 'menu-primary-menu-container'
                ) );
                ?>
            </div>
            <div class="menu-last-focus-item"></div>       
        </nav><!-- #site-navigation -->

    </div>

    <?php 
}


function xews_lite_header_social_icons(){

    

    $social_profiles = array(
        'facebook'  => 'fa-brands fa-facebook-f',
        'instagram' => 'fa-brands fa-instagram',
        'linkedin'  => 'fa-brands fa-linkedin-in',
        'twitter'   => 'fa-brands fa-x-twitter',
    );
    
    
    echo '<div class="social-icon-wrapp">';
    echo '<ul>';
    foreach( $social_profiles as $key => $val ){
        
        $xews_lite_header_icons = get_theme_mod( 'xews_lite_header_icons_'.$key );

        if( $xews_lite_header_icons ){
            wp_print_styles( array('fontawesome') );
        echo '<li>';
            echo  '<a href="'.esc_url($xews_lite_header_icons).'" class="'.esc_attr($key).'">';
            echo  '<i class="'.esc_attr($val).'"></i></a>';
        echo '</li>';
        }

      
    }
    echo '</ul>';
    echo '</div>'; 

}



/**
 * Header search
 */
function xews_lite_header_search(){

    $defaults 			        = xews_lite_customizer_defaults();
    $xews_lite_search_display_enable = get_theme_mod( 'xews_lite_search_display_enable', $defaults['xews_lite_search_display_enable'] );

    if( false == $xews_lite_search_display_enable ){
        return;
    }

    wp_print_styles( array('fontawesome') );
	?>

    <div class="right-wrapp cww-flex">
        <div class="search-wrapper layout-one">
            <button class="search-icon btn-no-effect">
                <i class="fa-solid fa-magnifying-glass active"></i>
                <i class="fas fa-times"></i>
            </button>
            <?php  echo xews_lite_search_content_form(); ?>
        </div>
        <?php xews_lite_dark_toggle(); ?>
    </div>
	<?php 
}



function xews_lite_search_content_form(){
	?>
    <div class="header-search-content layout-one">
        
        <div class="xews-search-form">
            <?php echo get_search_form(); ?>
        </div>
        
        <?php // Ajax search result area ?>
        <div class="xews-ajax-search-area search-content">
        </div>
    </div>
	<?php 
}


function xews_lite_header_date(){

    $defaults 			        = xews_lite_customizer_defaults();
    $xews_lite_date_display_enable   = get_theme_mod( 'xews_lite_date_display_enable', $defaults['xews_lite_date_display_enable'] );
    $xews_lite_date_display_type     = get_theme_mod( 'xews_lite_date_display_type', $defaults['xews_lite_date_display_type'] );

    if( false == $xews_lite_date_display_enable ){
        return;
    }
    wp_print_styles( array('fontawesome') );
    ?>
    <div class="header-date">
        <?php if( $xews_lite_date_display_type == 'date-only' || $xews_lite_date_display_type == 'date-time'){ ?>
            <span class="date">
            <i class="fa-solid fa-calendar-days"></i>
                <?php echo date_i18n("l, F j")?>
            </span>
        <?php } ?>
        
        <?php if( $xews_lite_date_display_type == 'time-only' || $xews_lite_date_display_type == 'date-time'){ ?> 
            <i class="fa-regular fa-clock"></i>
            <span class="time"></span>
        <?php } ?>

    </div>

<?php }

function xews_lite_top_left_header(){
    ?>
    <div class="left-wrapp cww-flex">
    <?php 
    xews_lite_header_social_icons();
    xews_lite_header_date();
    ?>
    </div>
    <?php 
}

function xews_lite_dark_toggle(){
    $defaults 			        = xews_lite_customizer_defaults();
    $xews_lite_darkmode_enable  = get_theme_mod('xews_lite_darkmode_enable', $defaults['xews_lite_darkmode_enable']);
    if( false == $xews_lite_darkmode_enable ){
        return;
    }
    
    wp_print_styles( array('fontawesome') );
    ?>
    <button class="xews-dark-mode-toggle cww-flex">
        <span class="light">
            <svg width="28px" height="28px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 1.25C12.4142 1.25 12.75 1.58579 12.75 2V3C12.75 3.41421 12.4142 3.75 12 3.75C11.5858 3.75 11.25 3.41421 11.25 3V2C11.25 1.58579 11.5858 1.25 12 1.25ZM4.39861 4.39861C4.6915 4.10572 5.16638 4.10572 5.45927 4.39861L5.85211 4.79145C6.145 5.08434 6.145 5.55921 5.85211 5.85211C5.55921 6.145 5.08434 6.145 4.79145 5.85211L4.39861 5.45927C4.10572 5.16638 4.10572 4.6915 4.39861 4.39861ZM19.6011 4.39887C19.894 4.69176 19.894 5.16664 19.6011 5.45953L19.2083 5.85237C18.9154 6.14526 18.4405 6.14526 18.1476 5.85237C17.8547 5.55947 17.8547 5.0846 18.1476 4.79171L18.5405 4.39887C18.8334 4.10598 19.3082 4.10598 19.6011 4.39887ZM12 6.75C9.1005 6.75 6.75 9.1005 6.75 12C6.75 14.8995 9.1005 17.25 12 17.25C14.8995 17.25 17.25 14.8995 17.25 12C17.25 9.1005 14.8995 6.75 12 6.75ZM5.25 12C5.25 8.27208 8.27208 5.25 12 5.25C15.7279 5.25 18.75 8.27208 18.75 12C18.75 15.7279 15.7279 18.75 12 18.75C8.27208 18.75 5.25 15.7279 5.25 12ZM1.25 12C1.25 11.5858 1.58579 11.25 2 11.25H3C3.41421 11.25 3.75 11.5858 3.75 12C3.75 12.4142 3.41421 12.75 3 12.75H2C1.58579 12.75 1.25 12.4142 1.25 12ZM20.25 12C20.25 11.5858 20.5858 11.25 21 11.25H22C22.4142 11.25 22.75 11.5858 22.75 12C22.75 12.4142 22.4142 12.75 22 12.75H21C20.5858 12.75 20.25 12.4142 20.25 12ZM18.1476 18.1476C18.4405 17.8547 18.9154 17.8547 19.2083 18.1476L19.6011 18.5405C19.894 18.8334 19.894 19.3082 19.6011 19.6011C19.3082 19.894 18.8334 19.894 18.5405 19.6011L18.1476 19.2083C17.8547 18.9154 17.8547 18.4405 18.1476 18.1476ZM5.85211 18.1479C6.145 18.4408 6.145 18.9157 5.85211 19.2086L5.45927 19.6014C5.16638 19.8943 4.6915 19.8943 4.39861 19.6014C4.10572 19.3085 4.10572 18.8336 4.39861 18.5407L4.79145 18.1479C5.08434 17.855 5.55921 17.855 5.85211 18.1479ZM12 20.25C12.4142 20.25 12.75 20.5858 12.75 21V22C12.75 22.4142 12.4142 22.75 12 22.75C11.5858 22.75 11.25 22.4142 11.25 22V21C11.25 20.5858 11.5858 20.25 12 20.25Z" />
</svg>
        </span>
        <span class="dark">
            <svg width="28px" height="28px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3.32031 11.6835C3.32031 16.6541 7.34975 20.6835 12.3203 20.6835C16.1075 20.6835 19.3483 18.3443 20.6768 15.032C19.6402 15.4486 18.5059 15.6834 17.3203 15.6834C12.3497 15.6834 8.32031 11.654 8.32031 6.68342C8.32031 5.50338 8.55165 4.36259 8.96453 3.32996C5.65605 4.66028 3.32031 7.89912 3.32031 11.6835Z"
 stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
        </span>
        <span class="light-dark-switch"></span>
    </button>
    <?php
}

function xews_lite_header_structure_controller(){
    $headerImage =  get_header_image() ?  get_header_image() : '';
    $headerClass = $headerImage ? 'has-img': '';

    do_action('xews_lite_top_top_header');
    ?>
    <div class="main-header xews-header-container <?php echo esc_attr($headerClass)?>" style="background-image:url(<?php echo esc_url($headerImage)?>)">
        <div class="container">
            <div class="main-header-xews-wrapper main-header-elem-wrap header-elements-wrap cww-flex">
                <?php  do_action('xews_lite_top_header'); ?>
            </div>
        </div>
    </div>

    <div class="bottom-header xews-header-container">
        <div class="container">
            <div class="bottom-header-xews-wrapper bottom-header-elem-wrap header-elements-wrap cww-flex">
                <?php  do_action('xews_lite_bottom_header'); ?>
            </div>
        </div>
    </div>

    <?php 
    
}


function xews_lite_head_scripts()
{
    ?>
    <script type="text/javascript">
        let xews_lite_storageKey = 'theme-preference';

        let xews_lite_getColorPreference = function () {
            if (localStorage.getItem(xews_lite_storageKey))
                return localStorage.getItem(xews_lite_storageKey);
            else
                return window.matchMedia('(prefers-color-scheme: dark)').matches
                    ? 'dark'
                    : 'light';
        };

        let xews_lite_theme = {
            value: xews_lite_getColorPreference()
        };

        let xews_lite_setPreference = function () {
            localStorage.setItem(xews_lite_storageKey, xews_lite_theme.value);
            xews_lite_reflectPreference();
        };

        let xews_lite_reflectPreference = function () {
            document.firstElementChild.setAttribute("data-theme", xews_lite_theme.value);
            document.querySelector(".xews-dark-mode-toggle")?.setAttribute("aria-label", xews_lite_theme.value);
        };

        // Set early so no page flashes / CSS is made aware
        xews_lite_reflectPreference();

        window.addEventListener('load', function () {
            xews_lite_reflectPreference();
            let toggleBtn = document.querySelector(".xews-dark-mode-toggle");
            if (toggleBtn) {
                toggleBtn.addEventListener("click", function () {
                    xews_lite_theme.value = xews_lite_theme.value === 'light' ? 'dark' : 'light';
                    xews_lite_setPreference();
                });
            }
        });

        // Sync with system changes
        window
            .matchMedia('(prefers-color-scheme: dark)')
            .addEventListener('change', ({matches: isDark}) => {
                xews_lite_theme.value = isDark ? 'dark' : 'light';
                xews_lite_setPreference();
            });
    </script>
    <?php
}