<?php
/**
 * Header hooks and functions
 * 
 * @package One Newspaper
 * @since 1.0.0
 */
use OneNewspaper\CustomizerDefault as ONP;

if( ! function_exists( 'one_newspaper_social_icons_html' ) ) :
    /**
     * MARK: Social Icons
     * 
     * @since 1.0.0
     */
    function one_newspaper_social_icons_html() {

        echo '<div class="social-icons-wrap">';

            one_newspaper_customizer_social_icons( '', 4 );

        echo '</div>';

    }
    add_action( 'one_newspaper_social_icons_hook', 'one_newspaper_social_icons_html' );
 endif;

 if( ! function_exists( 'one_newspaper_header_site_branding_part' ) ) :
    /**
     * MARK: Site Logo & Title
     * 
     * @since 1.0.0
     */
    function one_newspaper_header_site_branding_part() {
        echo '<div class="site-branding">';

            $site_description_show_hide = ONP\one_newspaper_get_customizer_option( 'blogdescription_option' );

            the_custom_logo();

            $tag = ( is_front_page() && is_home() ) ? 'h1' : 'p';

            echo '<', $tag, ' class="site-title"><a href="', esc_url( home_url( '/' ) ), '" rel="home">', get_bloginfo( 'name' ), '</a></', $tag, '>';

            $one_newspaper_description = get_bloginfo( 'description', 'display' );

            if ( $site_description_show_hide && ( $one_newspaper_description || is_customize_preview() ) ) echo '<p class="site-description">', $one_newspaper_description, '</p>';

        echo '</div><!-- .site-branding -->';
    }
    add_action( 'one_newspaper_header__site_branding_section_hook', 'one_newspaper_header_site_branding_part' );
 endif;

 if( ! function_exists( 'one_newspaper_header_advertisement_html' ) ) :
    /**
     * MARK: Advertisement
     * 
     * @since 1.0.0
     */
    function one_newspaper_header_advertisement_html() {
        if( ! ONP\one_newspaper_get_customizer_option( 'header_ads_banner_responsive_option' ) ) return;
        $header_ads_banner_image = ONP\one_newspaper_get_customizer_option( 'header_ads_banner_image' );
        $header_ads_banner_image_url = ONP\one_newspaper_get_customizer_option( 'header_ads_banner_image_url' );
        $header_ads_banner_custom_target = ONP\one_newspaper_get_customizer_option( 'header_ads_banner_custom_target' );
        if( ! empty( $header_ads_banner_image ) ) :
        ?>
            <div class="ads-banner">
                <a href="<?php echo esc_url( $header_ads_banner_image_url ); ?>" target="<?php echo esc_html( $header_ads_banner_custom_target ); ?>"><img src="<?php echo wp_get_attachment_url( $header_ads_banner_image ); ?>"></a>
            </div><!-- .ads-banner -->
        <?php
        endif;
     }
    add_action( 'one_newspaper_header_advertisement_hook', 'one_newspaper_header_advertisement_html', 10 );
 endif;

 if( ! function_exists( 'one_newspaper_newsletter_html' ) ) :
    /**
     * MARK: Newsletter
     * 
     * @since 1.0.0
     */
     function one_newspaper_newsletter_html() {
        $newsletter_label = ONP\one_newspaper_get_customizer_option( 'newsletter_label' );
        $newsletter_icon_picker = [ 'type'  => 'none', 'value' => 'fa-solid fa-bell' ];
        $header_newsletter_redirect_href_link = ONP\one_newspaper_get_customizer_option( 'header_newsletter_redirect_href_link' );
        $header_newsletter_hover_animation = ONP\one_newspaper_get_customizer_option( 'header_newsletter_hover_animation' );
        $elementClass = 'newsletter-element animation--' . $header_newsletter_hover_animation;
        ?>
            <div class="<?php echo esc_html( $elementClass ); ?>" <?php if( isset($newsletter_label) && !empty($newsletter_label) ) echo 'title="' . esc_attr( $newsletter_label ) . '"'; ?>>
                <a href="<?php echo esc_url( $header_newsletter_redirect_href_link ); ?>" target="_blank" data-popup="redirect">
                    <?php
                        if( isset( $newsletter_icon_picker['value'] ) && ! empty( $newsletter_icon_picker['value'] ) ) echo '<span class="title-icon"><i class="', esc_attr( $newsletter_icon_picker['value'] ), '"></i></span>';
                        if( $newsletter_label ) echo '<span class="title-text">', esc_html( $newsletter_label ), '</span>';
                    ?>
                </a>
            </div><!-- .newsletter-element -->
        <?php
    }
    add_action( 'one_newspaper_newsletter_hook', 'one_newspaper_newsletter_html' );
 endif;

 if( ! function_exists( 'one_newspaper_random_news_html' ) ) :
    /**
     * MARK: Random News
     * 
     * @since 1.0.0
     */
     function one_newspaper_random_news_html() {
        $random_news_icon_picker = [ 'type'  => 'icon', 'value' => 'fas fa-random' ];
        $random_news_label = ONP\one_newspaper_get_customizer_option( 'random_news_label' );
        $button_url = one_newspaper_get_random_news_url();
        $icon = $random_news_icon_picker['value'];
        ?>
            <div class="random-news-element" <?php if( isset( $random_news_label ) && !empty( $random_news_label ) ) echo 'title="' . esc_attr( $random_news_label ) . '"'; ?>>
                <a href="<?php echo esc_url($button_url); ?>" target="_blank">
                    <?php
                        if( isset( $icon ) && ! empty( $icon ) ) echo '<span class="title-icon"><i class="', esc_attr( $icon ), '"></i></span>';
                        if( $random_news_label) echo '<span class="title-text">' .esc_html( $random_news_label ). '</span>';
                    ?>
                </a>
            </div><!-- .random-news-element -->
        <?php
    }
    add_action( 'one_newspaper_random_news_hook', 'one_newspaper_random_news_html' );
 endif;

 if( ! function_exists( 'one_newspaper_off_canvas_html' ) ) :
    /**
     * MARK: Off Canvas
     * 
     * @since 1.0.0
     */
     function one_newspaper_off_canvas_html() {
        ?>
            <div class="sidebar-toggle-wrap">
                <a class="off-canvas-trigger" href="javascript:void(0);">
                    <div class="one_newspaper_sidetoggle_menu_burger">
                        <span></span>
                        <span></span>
                        <span></span>
                    </div>
                </a>
                <div class="sidebar-toggle hide">
                <span class="off-canvas-close"><i class="fas fa-times"></i></span>
                    <div class="one-newspaper-container">
                    <div class="row">
                        <?php dynamic_sidebar( 'off-canvas-sidebar' ); ?>
                    </div>
                    </div>
                </div>
            </div>
        <?php
    }
    add_action( 'one_newspaper_off_canvas_hook', 'one_newspaper_off_canvas_html' );
 endif;

 if( ! function_exists( 'one_newspaper_primary_menu_html' ) ) :
    /**
     * MARK: Primary Menu
     * 
     * @since 1.0.0
     */
    function one_newspaper_primary_menu_html() {
      ?>
        <nav id="site-navigation" class="main-navigation <?php echo esc_attr( 'hover-effect--' . ONP\one_newspaper_get_customizer_option( 'header_menu_hover_effect' ) ); ?>">
            <button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
                <div id="one_newspaper_menu_burger">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
                <span class="menu_txt"><?php esc_html_e( 'Menu', 'one-newspaper' ); ?></span></button>
            <?php
                wp_nav_menu(
                    array(
                        'theme_location' => 'menu-2',
                        'menu_id'        => 'header-menu',
                    )
                );
            ?>
        </nav><!-- #site-navigation -->
      <?php
    }
    add_action( 'one_newspaper_primary_menu_hook', 'one_newspaper_primary_menu_html' );
 endif;

 if( ! function_exists( 'one_newspaper_header_search_html' ) ) :
   /**
    * MARK: Search
    * 
    * @since 1.0.0
    */
    function one_newspaper_header_search_html() {
        ?>
            <div class="search-wrap">
                <button class="search-trigger search-action">
                    <i class="fas fa-search"></i>
                </button>
                <?php
                    echo '<div class="search-form-wrap hide">', get_search_form(), '</div>';
                    echo '<button class="search-close-btn search-action hide"><i class="fas fa-times"></i></button>';
                ?>
            </div>
        <?php
    }
    add_action( 'one_newspaper_header_search_hook', 'one_newspaper_header_search_html' );
endif;

 if( ! function_exists( 'one_newspaper_custom_button_html' ) ) :
    /**
     * MARK: Custom Button
     * 
     * @since 1.0.0
     */
     function one_newspaper_custom_button_html() {
        $custom_button_link = ONP\one_newspaper_get_customizer_option( 'custom_button_link' );
        $custom_button_label = ONP\one_newspaper_get_customizer_option( 'custom_button_label' );
        $custom_button_icon_picker = [ 'type'  => 'icon', 'value' => 'fab fa-youtube' ];
        ?>
            <a class="header-custom-button" href="<?php echo esc_url( $custom_button_link ); ?>" target="_blank">
                <?php if( $custom_button_icon_picker['type'] == 'icon' && $custom_button_icon_picker['value'] != "fas fa-ban" ) : ?>
                    <span class="icon">
                        <i class="<?php echo esc_attr( $custom_button_icon_picker['value'] ); ?>"></i>
                    </span>
                <?php endif;
                if( $custom_button_label ) :
                ?>
                    <span class="ticker_label_title_string"><?php echo esc_html( $custom_button_label ); ?></span>
                <?php endif; ?>
            </a>
        <?php
    }
    add_action( 'one_newspaper_custom_button_hook', 'one_newspaper_custom_button_html' );
 endif;

 if( ! function_exists( 'one_newspaper_ticker_news_html' ) ) :
    /**
     * MARK: Ticker News
     * 
     * @since 1.0.0
     */
    function one_newspaper_ticker_news_html() {
        $ticker_news_order_by = ONP\one_newspaper_get_customizer_option( 'ticker_news_order_by' );
        $ticker_news_numbers = ONP\one_newspaper_get_customizer_option( 'ticker_news_numbers' );
        $ticker_news_posts = ONP\one_newspaper_get_customizer_option( 'ticker_news_posts' );
        $ticker_news_categories = ONP\one_newspaper_get_customizer_option( 'ticker_news_categories' );

        $orderArray = explode( '-', $ticker_news_order_by );
        $ticker_args = array(
            'order' => esc_html( $orderArray[1] ),
            'orderby' => esc_html( $orderArray[0] ),
            'ignore_sticky_posts'    => true,
            'fields'    =>  'ids'
        );
        $ticker_args['posts_per_page'] = absint( $ticker_news_numbers );
        if( $ticker_news_categories ) $ticker_args['cat'] = one_newspaper_get_categories_for_args( $ticker_news_categories );
        if( $ticker_news_posts ) $ticker_args['post__in'] = one_newspaper_get_post_id_for_args( $ticker_news_posts );

        $elementClass = 'ticker-news-wrap one-newspaper-ticker layout--two';
        if( ! ONP\one_newspaper_get_customizer_option( 'ticker_news_thumbnail_option' ) ) $elementClass .= ' no-feat-img';
        $ticker_news_frontpage = ONP\one_newspaper_get_customizer_option( 'ticker_news_frontpage' );
        $ticker_section_full_width = ONP\one_newspaper_get_customizer_option( 'ticker_section_full_width' );
        $sectionClass[] = 'one-newspaper-section';
        $sectionClass[] = 'one-newspaper-ticker-news';
        $sectionClass[] = 'width-' . one_newspaper_get_section_width_layout_val( 'ticker_news_width_layout' );
        $sectionClass[] = 'full-width--' . ( $ticker_section_full_width ? 'on' : 'off' );
        ?>
            <?php if( $ticker_news_frontpage ) echo '<section id="one-newspaper-ticker-news" class="', esc_attr( implode( ' ', $sectionClass ) ),'"><div class="one-newspaper-container"><div class="row">'; ?>
            <div class="<?php echo esc_attr( $elementClass ); ?>">
                <div class="ticker_label_title ticker-title one-newspaper-ticker-label">
                    <span class="icon"></span>
                    <?php 
                        echo '<span class="ticker_label_title_string">', esc_html__( 'Headlines', 'one-newspaper' ), '</span>';
                    ?>
                </div>
                <div class="one-newspaper-ticker-box">
                    <?php
                        $one_newspaper_direction = 'left';
                        $one_newspaper_dir = 'ltr';
                        if( is_rtl() ){
                            $one_newspaper_direction = 'right';
                            $one_newspaper_dir = 'ltr';
                        }
                    ?>

                    <ul class="ticker-item-wrap" direction="<?php echo esc_attr($one_newspaper_direction); ?>" dir="<?php echo esc_attr($one_newspaper_dir); ?>">
                        <?php get_template_part( 'template-parts/ticker-news/template', 'two', $ticker_args ); ?>
                    </ul>
                </div>
            </div>
            <?php
            if( $ticker_news_frontpage ) echo '</div></div></section>';
    }
    add_action( 'one_newspaper_ticker_news_hook', 'one_newspaper_ticker_news_html', 10 );
 endif;

if( ! function_exists( 'one_newspaper_date_time_html' ) ) :
    /**
     * MARK: Date Time
     * 
     * @since 1.0.0
    */
    function one_newspaper_date_time_html() {
        $time_option = ONP\one_newspaper_get_customizer_option( 'time_option' );
        $date_option = ONP\one_newspaper_get_customizer_option( 'date_option' );
        $display_block = ONP\one_newspaper_get_customizer_option( 'date_time_display_block' );
        $dateTimeClass = 'top-date-time';
        if( $display_block ) $dateTimeClass .= ' block';
        ?>
            <div class="<?php echo esc_attr( $dateTimeClass ); ?>">
                <span class="top-date-time-inner">
                    <?php
                        if( $time_option ) echo '<span class="time">', date( 'H:i:s A' ) ,'</span>';
                        if( $date_option ) echo '<span class="date">', date_i18n( 'l, ' . get_option( 'date_format' ), current_time( 'timestamp' )) ,'</span>';
                    ?>
                </span>
            </div>
        <?php
    }
    add_action( 'one_newspaper_date_time_hook', 'one_newspaper_date_time_html', 10 );
endif;

if( ! function_exists( 'one_newspaper_secondary_menu_html' ) ) :
   /**
    * MARK: Secondary Menu
    * 
    * @since 1.0.0
    */
    function one_newspaper_secondary_menu_html() {
        $nav_classes = 'top-nav-menu main-navigation hover-effect--' . ONP\one_newspaper_get_customizer_option( 'secondary_menu_hover_effect' );
        ?>
            <div class="site-navigation-wrapper secondary-menu">
                <nav id="site-navigation-secondary" class="<?php echo esc_attr( $nav_classes ); ?>">
                    <?php
                        wp_nav_menu([
                            'theme_location'    =>  'menu-1',
                            'menu_id'   =>  'top-menu',
                            'depth' =>  1
                        ]);
                    ?>
                </nav><!-- #site-navigation -->
            </div>
        <?php
    }
   add_action( 'one_newspaper_secondary_menu_hook', 'one_newspaper_secondary_menu_html' );
endif;

if( ! function_exists( 'one_newspaper_get_toggle_button_html' ) ) :
    /**
     * MARK: Toggle Button
     * 
     * @since 1.0.0
     */
    function one_newspaper_get_toggle_button_html() {
        ?>
            <div class="toggle-button-wrapper">
                <button class="canvas-menu-icon"></button>
            </div>
        <?php
    }
endif;

if( ! function_exists( 'one_newspaper_theme_mode_html' ) ) :
    /**
     * MARK: Theme Mode
     * 
     * @since 1.0.0
     */
    function one_newspaper_theme_mode_html() {
        $light_mode_icon_args = ONP\one_newspaper_get_customizer_option( 'theme_mode_light_icon' );
        $dark_mode_icon_args = ONP\one_newspaper_get_customizer_option( 'theme_mode_dark_icon' );
        $light_mode_icon_class = ( array_key_exists( 'value', $light_mode_icon_args ) && is_array( $light_mode_icon_args ) ) ? $light_mode_icon_args['value'] : '';
        $dark_mode_icon_class = ( array_key_exists( 'value', $dark_mode_icon_args ) && is_array( $dark_mode_icon_args ) ) ? $dark_mode_icon_args['value'] : '';
        if( $light_mode_icon_class || $dark_mode_icon_class ) :
            $elementClass = 'mode-toggle-wrap sticky';
            ?>
                <div class="<?php echo esc_attr( $elementClass ); ?>">
                    <span class="mode-toggle">
                        <?php 
                            if( $light_mode_icon_class ) echo '<i class="', esc_attr( $light_mode_icon_args['value'] ), ' light"></i>';
                            if( $dark_mode_icon_class ) echo '<i class="', esc_attr( $dark_mode_icon_args['value'] ), ' dark"></i>';
                        ?>
                    </span>
                </div>
            <?php
        endif;
    }
    add_action( 'one_newspaper_theme_mode_hook', 'one_newspaper_theme_mode_html' );
endif;