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

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

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

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

function santamas_dynamic_styles() {
	$banner_image = get_theme_mod( 'santamas_banner_image', get_template_directory_uri() . '/images/city.jpg' );
    $banner_overlay_color = get_theme_mod( 'santamas_banner_overlay_color', 'rgba( 0, 0, 0, 0.1)' );
    $banner_caption_color = get_theme_mod( 'santamas_banner_caption_color', '#FFFFFF' );
    $custom_css = "
            .banner-section{
                background-image: url({$banner_image});
            }";
    $custom_css .= "
            .banner-section:before{
                background: {$banner_overlay_color};
            }";
    $custom_css .= "
            .banner-caption h2,
            .banner-caption p{
                color: {$banner_caption_color};
            }";

    if( !is_page_template('template-home.php') ){
    	$banner_image = get_header_image();
    	$custom_css = "
            .header-banner{
                background-image: url({$banner_image});
            }";
    }
    wp_add_inline_style( 'santamas-style', $custom_css );
}
add_action( 'wp_enqueue_scripts', 'santamas_dynamic_styles' );


add_action( 'santamas_home_section', 'santamas_home_banner' );
add_action( 'santamas_home_section', 'santamas_about_section' );
add_action( 'santamas_home_section', 'santamas_event_countdown' );
add_action( 'santamas_home_section', 'santamas_featured_section' );
add_action( 'santamas_home_section', 'santamas_blog_section' );
add_action( 'santamas_home_section', 'santamas_gallery_section' );

function santamas_home_banner(){
?>
	<div class="banner-section" id="banner-section">
		<?php 
		$banner_title = get_theme_mod('santamas_banner_title');
		$banner_subtitle = get_theme_mod('santamas_banner_sub_title');
		$banner_button_text1 = get_theme_mod('santamas_banner_text1');
		$banner_button_link1 = get_theme_mod('santamas_banner_link1');
		$banner_button_text2 = get_theme_mod('santamas_banner_text2');
		$banner_button_link2 = get_theme_mod('santamas_banner_link2');

		if( $banner_title || $banner_subtitle ){ ?>
			<div class="banner-caption">
				<div class="santamas-container">
				<?php if( $banner_title ){ ?>
					<h2><?php echo esc_html( $banner_title ); ?></h2>
				<?php } ?>

				<?php if( $banner_subtitle ){ ?>
					<p><?php echo esc_html( $banner_subtitle ); ?></p>
				<?php } ?>

				<?php if( $banner_title || $banner_subtitle ){ ?>
					<div class="banner-buttons">
						<?php if( $banner_button_link1 ){ ?>
							<a href="<?php echo esc_url( $banner_button_link1 ) ?>"><?php echo esc_html( $banner_button_text1 ) ?></a>
						<?php } ?>

						<?php if( $banner_button_link2 ){ ?>
							<a href="<?php echo esc_url( $banner_button_link2 ) ?>"><?php echo esc_html( $banner_button_text2 ) ?></a>
						<?php } ?>
					</div>
				<?php } ?>
				</div>
			</div>
		<?php
		} ?>
	</div>
<?php
}

function santamas_event_countdown(){
	$santamas_disable_event = get_theme_mod( 'santamas_disable_event', 'off' );

	if( $santamas_disable_event == 'off' ){
		$santamas_event_info = get_theme_mod('santamas_event_info');
		$santamas_event_date = get_theme_mod('santamas_event_date');
		?>
		<div class="event-countdown-section" id="event-countdown-section">
			<div class="santamas-container santamas-flex">
				<div class="event-info">
					<?php echo esc_html($santamas_event_info); ?>
				</div>

				<div class="event-counter" data-countdown="<?php echo esc_attr($santamas_event_date); ?>">
					
				</div>
			</div>
		</div>
		<?php
	}
}


function santamas_about_section(){
	$santamas_disable_about = get_theme_mod( 'santamas_disable_about', 'off' );

	if( $santamas_disable_about == 'off' ){
	$about_page_id = get_theme_mod('santamas_about_page');
		?>
		<div class="santamas-about-section" id="santamas-about-section">
			<div class="santamas-container santamas-flex">
				<?php
				if( $about_page_id ){
					$args = array(
						'page_id' =>  $about_page_id
					);

					$query = new WP_Query($args);

					if( $query->have_posts() ){
						while( $query->have_posts() ){
							$query->the_post();

							if( has_post_thumbnail() ){
								$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
								echo '<div class="about-section-image">';
								echo '<img src="'. esc_url($image[0]) .'" alt="'. esc_attr( get_the_title() ) .'"/>';
								echo '</div>';
							}
							?>
				
							<div class="about-section-content">
								<h4><?php the_title(); ?></h4>
								<?php the_content(); ?>
								
								<?php 
								$disable_counter = get_theme_mod('santamas_disable_counter', 'off');
								if( $disable_counter != 'on' ){
									?>
									<div class="about-counter">
										<?php 
											for( $i=1; $i < 5; $i++ ){
												$counter_icon = get_theme_mod('santamas_counter_icon'.$i, 'fa fa-bell');
												$counter_title = get_theme_mod('santamas_counter_title'.$i);
												$counter_count = get_theme_mod('santamas_counter_count'.$i);
												
												if( $counter_title || $counter_count ){
												?>
												<div class="counter-block">
													<div class="counter-icon">
														<i class="<?php echo esc_attr($counter_icon); ?>"></i>
													</div>
													
													<div class="counter-content">
														<div class="counter-number">
															<?php echo esc_html($counter_count); ?>
														</div>
														
														<div class="counter-text">
															<?php echo esc_html($counter_title); ?>
														</div>
													</div>
												</div>
												<?php
												}
											}
										?>
									</div>
									<?php
								}
								?>
							</div>
							<?php
						}
					}
				}
				?>
			</div>
		</div>
		<?php
	}
}

function santamas_featured_section(){
	$santamas_disable_featured = get_theme_mod( 'santamas_disable_featured', 'off' );

	if( $santamas_disable_featured == 'off' ){
		?>
		<div class="santamas-featured-section" id="santamas-featured-section">
			<div class="santamas-container">
				<?php 
				$title = get_theme_mod('santamas_featured_title');
				$subtitle = get_theme_mod('santamas_featured_sub_title');
				if($title){
				?>
					<h4 class="section-title"><?php echo esc_html($title); ?></h4>
				<?php 
				}
		
				if($subtitle){
				?>
					<div class="section-subtitle"><?php echo esc_html($subtitle); ?></div>
				<?php 
				} ?>
				
				<div class="featured-box-wrap santamas-flex">
					<?php
					for( $i=1; $i < 4; $i++ ){
						$page_id = get_theme_mod('santamas_featured_page'.$i);
						if( $page_id ){
							$args = array(
								'page_id' =>  $page_id
							);

							$query = new WP_Query($args);

							if( $query->have_posts() ){
								while( $query->have_posts() ){
									$query->the_post();
									?>
										<div class="featured-box">
										<?php
										if( has_post_thumbnail() ){
											$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
											echo '<div class="featured-thumb">';
											echo '<img src="'. esc_url($image[0]) .'" alt="'. esc_attr( get_the_title() ) .'"/>';
											echo '</div>';
										}
										?>
										<h5><?php the_title(); ?></h5>
										<p><?php echo wp_trim_words( get_the_content(), 15, '...' ); ?></p>
										<a class="featured-button" href="<?php the_permalink() ?>"><?php esc_html_e('Read More','santamas') ?></a>
									</div>
									<?php
								}
								wp_reset_postdata();
							}
						}
					}
					?>
				</div>
			</div>
		</div>
		<?php
	}
}

function santamas_blog_section(){
	$santamas_disable_blog = get_theme_mod( 'santamas_disable_blog', 'off' );

	if( $santamas_disable_blog == 'off' ){
		?>
		<div class="santamas-blog-section"  id="santamas-blog-section">
			<div class="santamas-container">
				<?php
				$santamas_blog_title = get_theme_mod('santamas_blog_title');
				$santamas_blog_sub_title = get_theme_mod('santamas_blog_sub_title');
				
				if($santamas_blog_title){ ?>
					<h4 class="section-title"><?php echo esc_html($santamas_blog_title); ?></h4>
				<?php }
				
				if($santamas_blog_sub_title){ ?>
					<div class="section-subtitle"><?php echo esc_html($santamas_blog_sub_title); ?></div>
				<?php } ?>

				<div class="santamas-blog-wrap clearfix">
				<?php 
					$santamas_blog_post_count = get_theme_mod('santamas_blog_post_count', 3 );
					$santamas_blog_cat_exclude = get_theme_mod('santamas_blog_cat_exclude');
					$santamas_blog_cat_exclude = explode(',', $santamas_blog_cat_exclude);

					$args = array(
						'posts_per_page' => 2,
						'category__not_in' => $santamas_blog_cat_exclude
						);
					$query = new WP_Query($args);
					if($query -> have_posts()): ?>
						<div class="blog-left">
						<?php
						while($query -> have_posts()) : $query -> the_post();
						$santamas_image = wp_get_attachment_image_src(get_post_thumbnail_id() , 'santamas-blog-thumb');
						if(has_post_thumbnail()){
							$image_url = $santamas_image[0];
						}else{
							$image_url = get_template_directory_uri() . '/images/placeholder.png';
						}
						?>
						<div class="blog-post-box">
							<div class="blog-thumbnail">
								<a href="<?php the_permalink(); ?>"><img src="<?php echo esc_url($image_url) ?>" alt="<?php esc_attr( get_the_title()); ?>"></a>
								<div class="blog-date"><?php echo get_the_date(); ?></div>
							</div>
							<div class="blog-excerpt">
								<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
								<p>
								<?php 
								if(has_excerpt()){
									echo get_the_excerpt();
								}else{
									echo wp_trim_words( get_the_content() , 15, '...' );
								}
								?>
								</p>
								<div class="blog-read-more">
									<a href="<?php the_permalink(); ?>"><?php esc_html_e('Read More', 'santamas'); ?></a>
								</div>
							</div>
						</div>
						<?php
						endwhile; ?>
						</div>
					
						<div class="blog-right">
							<?php
							$args = array(
								'posts_per_page' => 4,
								'category__not_in' => $santamas_blog_cat_exclude,
								'offset' => 2
								);
							$query = new WP_Query($args);
							while($query -> have_posts()) : $query -> the_post();
							$santamas_image = wp_get_attachment_image_src(get_post_thumbnail_id() , 'thumbnail');
							if(has_post_thumbnail()){
								$image_url = $santamas_image[0];
							}else{
								$image_url = get_template_directory_uri() . '/images/placeholder-thumb.png';
							}
							?>
							<div class="blog-post-box clearfix">
								<div class="blog-thumbnail">
									<a href="<?php the_permalink(); ?>"><img src="<?php echo esc_url($image_url) ?>" alt="<?php esc_attr( get_the_title() ); ?>"></a>
								</div>

								<div class="blog-excerpt">
									<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
									<div class="blog-date"><?php echo get_the_date(); ?></div>
								</div>
							</div>
							<?php
							endwhile; ?>
						</div>
						<?php
					endif;
					wp_reset_postdata();
				?>
				</div>	
			</div>
		</div>
	<?php
	}
}

function santamas_gallery_section(){
	$santamas_disable_gallery = get_theme_mod( 'santamas_disable_gallery', 'off' );

	if( $santamas_disable_gallery == 'off' ){
		$gallery_images = get_theme_mod('santamas_gallery_image');
		
		if( $gallery_images ){ ?>
		<div class="santamas-gallery-section"  id="santamas-gallery-section">
			<div class="santamas-gallery-slider" id="santamas-gallery-slider">
			<?php
				$image_ids = explode( ',', $gallery_images );
				foreach ( $image_ids as $attachment_id ) {
					$big_img = wp_get_attachment_image_src( $attachment_id, 'full' );
					$small_img = wp_get_attachment_image_src( $attachment_id, 'santamas-gallery-thumb' );
					echo '<a href="' . esc_url($big_img[0]) . '" class="gallery-thumb"><img src="' . esc_url($small_img[0]) . '" /></a>';
				}
			?>
			</div>
		</div>
		<?php
		}
	}
}

function santamas_social_icons(){
	$facebook = get_theme_mod('santamas_social_facebook');
	$twitter = get_theme_mod('santamas_social_twitter');
	$google_plus = get_theme_mod('santamas_social_google_plus');
	$pinterest = get_theme_mod('santamas_social_pinterest');
	$youtube = get_theme_mod('santamas_social_youtube');
	$linkedin = get_theme_mod('santamas_social_linkedin');

	if($facebook){
		echo '<a href="'.esc_url( $facebook ).'" target="_blank"><i class="fa fa-facebook"></i></a>';
	}
	
	if($twitter){
		echo '<a href="'.esc_url( $twitter ).'" target="_blank"><i class="fa fa-twitter"></i></a>';
	}
	
	if($google_plus){
		echo '<a href="'.esc_url( $google_plus ).'" target="_blank"><i class="fa fa-google-plus"></i></a>';
	}
	
	if($pinterest){
		echo '<a href="'.esc_url( $pinterest ).'" target="_blank"><i class="fa fa-pinterest"></i></a>';
	}
	
	if($youtube){
		echo '<a href="'.esc_url( $youtube ).'" target="_blank"><i class="fa fa-youtube"></i></a>';
	}
	
	if($linkedin){
		echo '<a href="'.esc_url( $linkedin ).'" target="_blank"><i class="fa fa-linkedin"></i></a>';
	}
}