<?php
/**
 * Implementation of clients feature.
 *
 * @package Trade_Line
 */

// Check clients status.
add_filter( 'trade_line_filter_clients_status', 'trade_line_check_clients_status' );

// Add clients to the theme.
add_action( 'trade_line_action_before_footer', 'trade_line_add_featured_clients', 3 );

// Clients details.
add_filter( 'trade_line_filter_clients_details', 'trade_line_get_clients_details' );


if ( ! function_exists( 'trade_line_get_clients_details' ) ) :
	/**
	 * Clients details.
	 *
	 * @since 1.0.0
	 *
	 * @param array $input Clients details.
	 */
	function trade_line_get_clients_details( $input ) {

		$featured_clients_number = trade_line_get_option( 'featured_clients_number' );
		$clients_logos = array();
		for( $i=1 ; $i <= $featured_clients_number ; $i ++ ){
			$clients_logo    =   trade_line_get_option( 'featured_clients_logo_' .$i);
			$clients_log_url =   trade_line_get_option( 'featured_clients_url_' .$i);
				if ( ! empty( $clients_logo ) ) {
					$clients_logos[$i]['images'] = $clients_logo;
					$clients_logos[$i]['url']    = $clients_log_url;
				}
		}
		if ( ! empty( $clients_logos ) ) {
			$input = $clients_logos;
		}

		return $input;

	}
endif;

if ( ! function_exists( 'trade_line_add_featured_clients' ) ) :
	/**
	 * Add featured clients.
	 *
	 * @since 1.0.0
	 */
	function trade_line_add_featured_clients() {

		$flag_apply_clients = apply_filters( 'trade_line_filter_clients_status', true );
		if ( true !== $flag_apply_clients ) {
			return false;
		}

		$clients_details = array();
		$clients_details = apply_filters( 'trade_line_filter_clients_details', $clients_details );

		if ( empty( $clients_details ) ) {
			return;
		}

		// Render clients now.
		trade_line_render_featured_clients( $clients_details );

	}
endif;


if ( ! function_exists( 'trade_line_render_featured_clients' ) ) :
	/**
	 * Render featured clients.
	 *
	 * @since 1.0.0
	 *
	 * @param array $clients_details Details of clients content.
	 */
	function trade_line_render_featured_clients( $clients_details = array() ) {

		if ( empty( $clients_details ) ) {
			return;
		}
		$featured_clients_heading             = trade_line_get_option( 'featured_clients_heading' );
		$featured_clients_enable_arrow        = trade_line_get_option( 'featured_clients_enable_arrow' );
		$featured_clients_enable_autoplay     = trade_line_get_option( 'featured_clients_enable_autoplay' );
		$featured_clients_visible_active      = trade_line_get_option( 'featured_clients_visible_active' );
		$featured_clients_transition_delay    = trade_line_get_option( 'featured_clients_transition_delay' );
		$featured_clients_enable_non_circular = trade_line_get_option( 'featured_clients_enable_non_circular' );

	?>
	<?php
	$carousel_args = array(
		'slidesToShow' => absint( $featured_clients_visible_active ),
		'dots'         => false,
		'prevArrow'    => '<span data-role="none" class="slick-prev" tabindex="0"><i class="fa fa-angle-left" aria-hidden="true"></i></span>',
		'nextArrow'    => '<span data-role="none" class="slick-next" tabindex="0"><i class="fa fa-angle-right" aria-hidden="true"></i></span>',
		'responsive'   => array(
			array(
				'breakpoint' => 1024,
				'settings'   => array(
					'slidesToShow' => 5,
					),
				),
			array(
				'breakpoint' => 768,
				'settings'   => array(
					'slidesToShow' => 4,
					),
				),
			array(
				'breakpoint' => 660,
				'settings'   => array(
					'slidesToShow' => 3,
					),
				),
			array(
				'breakpoint' => 480,
				'settings'   => array(
					'slidesToShow' => 2,
					),
				),
			array(
				'breakpoint' => 360,
				'settings'   => array(
					'slidesToShow' => 1,
					),
				),
			),
		);
	if ( true !== $featured_clients_enable_arrow ) {
		$carousel_args['arrows'] = false;
	}
	if ( true === $featured_clients_enable_non_circular ) {
		$carousel_args['infinite'] = false;
	}
	if ( true === $featured_clients_enable_autoplay ) {
		$carousel_args['autoplay']      = true;
		$carousel_args['autoplaySpeed'] = 1000 * absint( $featured_clients_transition_delay );
	}
	$carousel_args_encoded = wp_json_encode( $carousel_args );
	?>

	<div id="featured-clients">
		<div class="container">
			<?php if ( ! empty( $featured_clients_heading ) ) : ?>
				<h3 class="clients-heading"><?php echo esc_html( $featured_clients_heading ); ?></h3>
			<?php endif ?>

			<div class="trade-line-featured-clients-carousel" data-slick='<?php echo $carousel_args_encoded; ?>'>
			    <?php foreach ( $clients_details as $key => $client ) : ?>
			    	<?php
			    	$target = '_self';
			    	$new_window = trade_line_get_option( 'featured_clients_enable_new_window');
			    	if ( true === $new_window ) {
			    		$target = '_blank';
			    	}
			    	$url = 'javascript:void(0);';
			    	if ( ! empty( $client['url'] ) ) {
			    		$url = esc_url( $client['url'] );
			    	}
			    	$link_open  = '';
			    	$link_close = '';
			    	if ( ! empty( $client['url'] ) ) {
			    		$link_open  = '<a href="' . esc_url( $client['url'] ) . '" target="' . esc_attr( $target ) . '">';
			    		$link_close = '</a>';
			    	}
			    	?>
			    	<article class="client-item">
			    		<?php echo $link_open; ?>
			    		<img src="<?php echo esc_url( $client['images'] ); ?>" alt="" />
			    		<?php echo $link_close; ?>
			    	</article>
			    <?php endforeach ?>

			</div><!-- .trade-line-featured-clients-carousel -->
		</div><!-- .container -->
	</div><!-- #featured-clients -->
    <?php
	}

endif;

if( ! function_exists( 'trade_line_check_clients_status' ) ):

	/**
	 * Check status of clients.
	 *
	 * @since 1.0.0
	 */
	function trade_line_check_clients_status( $input ){

		global $post, $wp_query;

		// Clients status.
		$featured_clients_status = trade_line_get_option( 'featured_clients_status' );

		// Get Page ID outside Loop.
		$page_id = $wp_query->get_queried_object_id();

		// Front page displays in Reading Settings.
		$page_on_front  = get_option( 'page_on_front' ) ;
		$page_for_posts = get_option( 'page_for_posts' );

		if ( 'entire-site' == $featured_clients_status || ( ( is_front_page() || ( is_home() && $page_for_posts !== $page_id ) ) && 'home-page' === $featured_clients_status ) ){
			$input = true;
		}
		else{
			$input = false;
		}
		return $input;

	}

endif;
