<?php
/**
 * Custom theme functions.
 *
 * This file contains hook functions attached to theme hooks.
 *
 * @package Prime Beauty Salon
 */

if( ! function_exists( 'prime_beauty_salon_site_branding' ) ) :
/**
* Site Branding
*
* @since 1.0.0
*/
function prime_beauty_salon_site_branding() { ?>
  <?php
  
  $prime_beauty_salon_button_text  = prime_beauty_salon_get_option( 'prime_beauty_salon_header_donate_btn_text' );
  $prime_beauty_salon_button_url  = prime_beauty_salon_get_option( 'prime_beauty_salon_header_donate_btn_url' );
  $prime_beauty_salon_sticky_header = prime_beauty_salon_get_option('prime_beauty_salon_sticky_header');

  ?>
  <div id="header-box" class="header-main" data-sticky="<?php echo $prime_beauty_salon_sticky_header; ?>">
    <div class="site-branding">
      <div class="logo" >
    <?php if (has_custom_logo()) { ?>
        <div class="site-logo"style="max-width: <?php echo esc_attr(get_theme_mod('logo_width', 'auto')); ?>;">
            <?php the_custom_logo(); ?>
        </div>
    <?php } else { ?>
        <div id="site-identity">
      
          <?php if (get_theme_mod('prime_beauty_salon_show_site_title', false)) : ?>
            <h1 class="site-title" style="font-size: <?php echo esc_attr(get_theme_mod('prime_beauty_salon_site_title_size', '30')); ?>px;">
              <a href="<?php echo esc_url(home_url('/')); ?>" rel="home"><?php bloginfo('name'); ?></a>
            </h1>
          <?php endif; ?>

          <?php if (get_theme_mod('show_site_description', false)) : ?>
              <p class="site-description"><?php bloginfo('description'); ?></p>
          <?php endif; ?>
        </div><!-- #site-identity -->
    <?php } ?>
  </div>
    </div>  <!-- .site-branding -->

    <div class="header-right">
      <div class="site-menu">
          <nav id="site-navigation" class="main-navigation" role="navigation" aria-label="Primary Menu">
              <button type="button" class="menu-toggle">
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
              </button>

              <?php
              wp_nav_menu( array(
                  'theme_location' => 'primary',
                  'menu_id'        => 'primary-menu',
                  'menu_class'     => 'nav-menu',
                  'fallback_cb'    => 'prime_beauty_salon_primary_navigation_fallback',
              ) );
              ?>
          </nav><!-- #site-navigation -->
          <?php if ( $prime_beauty_salon_button_text || $prime_beauty_salon_button_url ){?>
            <div class="donate-btn">
              <a href="<?php echo esc_url($prime_beauty_salon_button_url);?>"><?php echo esc_html($prime_beauty_salon_button_text);?><i class="fas fa-calendar-alt"></i></a>
            </div>
          <?php } ?>
      </div>
    </div>
  </div>
    
<?php }
endif;
add_action( 'prime_beauty_salon_action_header', 'prime_beauty_salon_site_branding', 10 );

if ( ! function_exists( 'prime_beauty_salon_footer_top_section' ) ) :

  /**
   * Top  Footer 
   *
   * @since 1.0.0
   */
function prime_beauty_salon_footer_top_section() {
    $prime_beauty_salon_footer_widgets_setting = get_theme_mod('prime_beauty_salon_footer_widget_columns', '4');

    // Get Customizer Settings
    $prime_beauty_salon_footer_bg_color = get_theme_mod('footer_bg_color', '#282828'); // Default color is white
    $prime_beauty_salon_footer_background_image = get_theme_mod('prime_beauty_salon_footer_bg_image'); // Background image

    if ($prime_beauty_salon_footer_widgets_setting) { ?>
        <div class="footer-widgets-area section-gap <?php echo 'footer-' . esc_attr($prime_beauty_salon_footer_widgets_setting); ?>" style="background-color: <?php echo esc_attr($prime_beauty_salon_footer_bg_color); ?>; background-image: url('<?php echo esc_url($prime_beauty_salon_footer_background_image); ?>'); background-repeat: no-repeat; background-size: cover;">
            <!-- Widget area starting from here -->
            <div class="wrapper <?php echo 'footer-cols-' . $prime_beauty_salon_footer_widgets_setting; ?>">
                <?php
                for ($i = 1; $i <= $prime_beauty_salon_footer_widgets_setting; $i++) {
                    if (is_active_sidebar('footer-' . $i)) {
                ?>
                        <div class="custom-widget">
                            <?php dynamic_sidebar('footer-' . $i); ?>
                        </div>
                <?php
                    }
                }

                // Output default widgets if no widget is added
                for ($i = 1; $i <= $prime_beauty_salon_footer_widgets_setting; $i++) {
                    if (!is_active_sidebar('footer-' . $i)) {
                        switch ($i) {
                            case 1:
                                ?>
                                <div class="custom-widget wow bounceInUp center delay-1000" data-wow-duration="2s">
                                    <!-- Archive -->
                                    <aside id="archive" class="widget widget_archive" role="complementary" aria-label="<?php esc_attr_e('secondsidebar', 'prime-beauty-salon'); ?>">
                                        <h2 class="widget-title"><?php esc_html_e('Archive List', 'prime-beauty-salon'); ?></h2>
                                        <ul>
                                            <?php wp_get_archives('type=monthly'); ?>
                                        </ul>
                                    </aside>
                                </div>
                                <?php
                                break;
                            case 2:
                                ?>
                                <div class="custom-widget wow bounceInUp center delay-1000" data-wow-duration="2s">
                                    <!-- Recent Posts -->
                                    <aside id="recent-posts" class="widget widget_recent_posts" role="complementary" aria-label="<?php esc_attr_e('thirdsidebar', 'prime-beauty-salon'); ?>">
                                        <h2 class="widget-title"><?php esc_html_e('Recent Posts', 'prime-beauty-salon'); ?></h2>
                                        <ul>
                                            <?php
                                            $args = array(
                                                'post_type'      => 'post',
                                                'posts_per_page' => 5,
                                            );
                                            $prime_beauty_salon_recent_posts = new WP_Query($args);

                                            while ($prime_beauty_salon_recent_posts->have_posts()) : $prime_beauty_salon_recent_posts->the_post();
                                            ?>
                                                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                                            <?php endwhile; ?>
                                            <?php wp_reset_postdata(); ?>
                                        </ul>
                                    </aside>
                                </div>
                                <?php
                                break;
                            case 3:
                                ?>
                                <div class="custom-widget wow bounceInUp center delay-1000" data-wow-duration="2s">
                                    <!-- Categories -->
                                    <aside id="categories" class="widget widget_categories" role="complementary" aria-label="<?php esc_attr_e('fourthsidebar', 'prime-beauty-salon'); ?>">
                                        <h2 class="widget-title"><?php esc_html_e('Categories', 'prime-beauty-salon'); ?></h2>
                                        <ul>
                                            <?php
                                            $args = array(
                                                'title_li' => '',
                                            );
                                            wp_list_categories($args);
                                            ?>
                                        </ul>
                                    </aside>
                                </div>
                                <?php
                                break;
                            case 4:
                                ?>
                                <div class="custom-widget wow bounceInUp center delay-1000" data-wow-duration="2s">
                                    <!-- Tags Widget -->
                                    <aside id="tags" class="widget widget_tags" role="complementary" aria-label="<?php esc_attr_e('fifthsidebar', 'prime-beauty-salon'); ?>">
                                        <h2 class="widget-title"><?php esc_html_e('Tags', 'prime-beauty-salon'); ?></h2>
                                        <div class="tag-cloud">
                                            <?php wp_tag_cloud(); ?>
                                        </div>
                                    </aside>
                                </div>
                                <?php
                                break;
                            default:
                                break;
                        }
                    }
                }
                ?>
            </div>
        </div> <!-- widget area starting from here -->
    <?php }
}
endif;
add_action('prime_beauty_salon_action_footer', 'prime_beauty_salon_footer_top_section', 10);

if ( ! function_exists( 'prime_beauty_salon_footer_section' ) ) :

  /**
   * Footer copyright
   *
   * @since 1.0.0
   */
  function prime_beauty_salon_footer_section() { ?>
    <?php
    $prime_beauty_salon_footer_setting  = prime_beauty_salon_get_option( 'prime_beauty_salon_footer_setting' );
     if ( $prime_beauty_salon_footer_setting ){ ?>
      <div class="site-info">
        <?php 
          $prime_beauty_salon_copyright_footer = prime_beauty_salon_get_option('prime_beauty_salon_copyright_text'); 
          if ( ! empty( $prime_beauty_salon_copyright_footer ) ) {
            $prime_beauty_salon_copyright_footer = wp_kses_data( $prime_beauty_salon_copyright_footer );
          }
        ?>
        <div class="wrapper">
          <span class="copy-right"><?php echo esc_html($prime_beauty_salon_copyright_footer);?></span>
        </div><!-- .wrapper --> 
      </div> <!-- .site-info -->
    <?php } ?>

    <?php
    $scroll_top  = prime_beauty_salon_get_option( 'prime_beauty_salon_header_donate_scroll_to_top' ,true);
     if ( $scroll_top ){ ?>
        <a id="button"><i class="<?php echo esc_attr(get_theme_mod('prime_beauty_salon_scroll_icon','fas fa-arrow-up')); ?>"></i></a>
    <?php } ?>
  <?php }

endif;
add_action( 'prime_beauty_salon_action_footer', 'prime_beauty_salon_footer_section', 20 );

if ( ! function_exists( 'prime_beauty_salon_footer_sidebar_class' ) ) :
  /**
   * Count the number of footer sidebars to enable dynamic classes for the footer
   *
   * @since prime_beauty_salon 0.1
   */
  function prime_beauty_salon_footer_sidebar_class() {
    $data = array();
    $prime_beauty_salon_active_sidebar = array();
      $count = 0;

      if ( is_active_sidebar( 'footer-1' ) ) {
        $prime_beauty_salon_active_sidebar[]   = 'footer-1';
          $count++;
      }

      if ( is_active_sidebar( 'footer-2' ) ){
        $prime_beauty_salon_active_sidebar[]   = 'footer-2';
          $count++;
    }

      if ( is_active_sidebar( 'footer-3' ) ){
        $prime_beauty_salon_active_sidebar[]   = 'footer-3';
          $count++;
      }

      if ( is_active_sidebar( 'footer-4' ) ){
        $prime_beauty_salon_active_sidebar[]   = 'footer-4';
          $count++;
      }

      $class = '';

      switch ( $count ) {
          case '1':
            $class = 'col-1';
            break;
          case '2':
            $class = 'col-2';
            break;
          case '3':
            $class = 'col-3';
            break;
          case '4':
            $class = 'col-4';
            break;
          case '0':
            $class = 'col-4';
            break;
      }

    $data['active_sidebar'] = $prime_beauty_salon_active_sidebar;
    $data['class']        = $class;

      return $data;
  }
endif;

if( ! function_exists( 'prime_beauty_salon_banner_header' ) ) :
    /**
     * Page Header
     */
    function prime_beauty_salon_banner_header() {
        $prime_beauty_salon_custom_header_image = get_theme_mod('prime_beauty_salon_custom_header_image');
        $prime_beauty_salon_show_page_title = get_theme_mod('prime_beauty_salon_show_page_title', true);

        // Check if it's the front page and not the home page
        if (is_front_page() && !is_home()) {
            return;
        }

        // Get the default header image if no custom header image is set
        $prime_beauty_salon_header_image = $prime_beauty_salon_custom_header_image ? $prime_beauty_salon_custom_header_image : get_header_image();

        // If it's a singular post/page and has a featured image, use it
        if (is_singular() && has_post_thumbnail()) {
            $prime_beauty_salon_header_image = get_the_post_thumbnail_url(get_the_ID(), 'full');
        }

        // Determine if the header image should be shown
        $prime_beauty_salon_background_style = '';
        if (!empty($prime_beauty_salon_header_image)) {
            $prime_beauty_salon_background_style = "style='background-image: url(" . esc_url($prime_beauty_salon_header_image) . ");'";
        }
        ?>

        <div id="page-site-header" class="<?php echo esc_attr($prime_beauty_salon_header_image ? 'show-header-image' : ''); ?> <?php echo esc_attr($prime_beauty_salon_show_page_title ? 'show-page-title' : ''); ?>" <?php echo $prime_beauty_salon_background_style; ?>>
            <div class="overlay"></div>
            <header class="page-header">
                <div class="wrapper">
                    <?php if ($prime_beauty_salon_show_page_title) prime_beauty_salon_banner_title(); ?>
                </div><!-- .wrapper -->
            </header>
        </div><!-- #page-site-header -->
        <div id="content" class="wrapper section-gap">
        <?php
    }
    add_action('prime_beauty_salon_banner_header', 'prime_beauty_salon_banner_header', 10);
endif;


if( ! function_exists( 'prime_beauty_salon_banner_title' ) ) :
/**
 * Page Header
*/
function prime_beauty_salon_banner_title(){ 
    if ( ( is_front_page() && is_home() ) || is_home() ){ 
        $prime_beauty_salon_your_latest_posts_title = prime_beauty_salon_get_option( 'prime_beauty_salon_your_latest_posts_title' );?>
        <h2 class="page-title"><?php echo esc_html($prime_beauty_salon_your_latest_posts_title); ?></h2><?php
    }

    if( is_singular() ) {
        the_title( '<h2 class="page-title">', '</h2>' );
    }       

    if( is_archive() ){
        the_archive_description( '<div class="archive-description">', '</div>' );
        the_archive_title( '<h2 class="page-title">', '</h2>' );
    }

    if( is_search() ){ ?>
      <h2 class="page-title"><?php echo esc_html( 'Search Results for: ', 'prime-beauty-salon' ) . get_search_query(); ?></h2>
    <?php }
    
    if( is_404() ) {
        echo '<h2 class="page-title">' . esc_html__( 'Error 404', 'prime-beauty-salon' ) . '</h2>';
    }
}
endif;

if ( ! function_exists( 'prime_beauty_salon_posts_tags' ) ) :
  /**
   * Prints HTML with meta information for the current post-date/time and author.
   */
  function prime_beauty_salon_posts_tags() {
    // Hide category and tag text for pages.
    if ( 'post' === get_post_type() && has_tag() ) { ?>
      <div class="tags-links">
        <?php /* translators: used between list items, there is a space after the comma */
        $prime_beauty_salon_tags = get_the_tags();
        if ( $prime_beauty_salon_tags ) {

          foreach ( $prime_beauty_salon_tags as $tag ) {
            echo '<span><a href="' . esc_url( get_tag_link( $tag->term_id ) ) .'">' . esc_html( $tag->name ) . '</a></span>'; // WPCS: XSS OK.
          }
        } ?>
      </div><!-- .tags-links -->
    <?php } 
  }
endif;