<?php
if ( ! function_exists( 'moment_featured_slider_array' ) ) :
    /**
     * Featured Slider array creation
     *
     * @since Moment Pro 1.0.0
     *
     * @param string $from_slider
     * @return array
     */
    function moment_featured_slider_array( $from_slider ){
        global $moment_customizer_all_values;
        $moment_feature_slider_number = absint( $moment_customizer_all_values['moment-featured-slider-number'] );
        $moment_feature_slider_single_words = absint( $moment_customizer_all_values['moment-fs-single-words'] );
        $moment_feature_slider_contents_array[0]['moment-feature-slider-title'] = __('High Style WordPress Photography Theme','moment');
        $moment_feature_slider_contents_array[0]['moment-feature-slider-content'] = __("A beauttiful way to show your captures",'moment');
        $moment_feature_slider_contents_array[0]['moment-feature-slider-link'] = '#';
        $moment_feature_slider_contents_array[0]['moment-feature-slider-image'] = get_template_directory_uri()."/assets/img/slider.jpg";
        $repeated_page = array('moment-fs-pages-ids');
        $moment_feature_slider_args = array();

        if ( 'from-category' ==  $from_slider ){
            $moment_feature_slider_category = $moment_customizer_all_values['moment-featured-slider-category'];
            if( 0 != $moment_feature_slider_category ){
                $moment_feature_slider_args =    array(
                    'post_type' => 'post',
                    'cat' => $moment_feature_slider_category,
                    'ignore_sticky_posts' => true
                );
            }
        }
        else{
            $moment_feature_slider_posts = salientthemes_customizer_get_repeated_all_value(3 , $repeated_page);
            $moment_feature_slider_posts_ids = array();
            if( null != $moment_feature_slider_posts ) {
                foreach( $moment_feature_slider_posts as $moment_feature_slider_post ) {
                    if( 0 != $moment_feature_slider_post['moment-fs-pages-ids'] ){
                        $moment_feature_section_posts_ids[] = $moment_feature_slider_post['moment-fs-pages-ids'];
                    }
                }

                if( !empty( $moment_feature_section_posts_ids )){
                    $moment_feature_slider_args =    array(
                        'post_type' => 'page',
                        'post__in' => $moment_feature_section_posts_ids,
                        'posts_per_page' => $moment_feature_slider_number,
                        'orderby' => 'post__in'
                    );
                }

            }
        }
        if( !empty( $moment_feature_slider_args )){
            // the query
            $moment_fature_section_post_query = new WP_Query( $moment_feature_slider_args );

            if ( $moment_fature_section_post_query->have_posts() ) :
                $i = 0;
                while ( $moment_fature_section_post_query->have_posts() ) : $moment_fature_section_post_query->the_post();
                    $url ='';
                    if(has_post_thumbnail()){
                        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
                        $url = $thumb['0'];
                    }
                    $moment_feature_slider_contents_array[$i]['moment-feature-slider-title'] = get_the_title();
                    $moment_feature_slider_contents_array[$i]['moment-feature-slider-content'] = moment_words_count( $moment_feature_slider_single_words ,get_the_content());;
                    $moment_feature_slider_contents_array[$i]['moment-feature-slider-link'] = get_permalink();
                    $moment_feature_slider_contents_array[$i]['moment-feature-slider-image'] = $url;
                    $i++;
                endwhile;
                wp_reset_postdata();
            endif;
        }
        return $moment_feature_slider_contents_array;
    }
endif;

if ( ! function_exists( 'moment_featured_home_slider' ) ) :
    /**
     * Featured Slider
     *
     * @since Moment 1.0.0
     *
     * @param null
     * @return null
     *
     */
    function moment_featured_home_slider() {
        global $moment_customizer_all_values;

        if( 1 != $moment_customizer_all_values['moment-feature-slider-enable'] ){
            return null;
        }
        $moment_feature_slider_selection_options = $moment_customizer_all_values['moment-featured-slider-selection'];
        $moment_slider_arrays = moment_featured_slider_array( $moment_feature_slider_selection_options );


        if( is_array( $moment_slider_arrays )){
        $moment_feature_slider_number = absint( $moment_customizer_all_values['moment-featured-slider-number'] );
        $moment_feature_slider_mode = $moment_customizer_all_values['moment-fs-slider-mode'];
        $moment_feature_slider_time = $moment_customizer_all_values['moment-fs-slider-time'];
        $moment_feature_slider_pause_time = $moment_customizer_all_values['moment-fs-slider-pause-time'];
        $moment_feature_enable_arrow = $moment_customizer_all_values['moment-fs-enable-arrow'];
        $moment_feature_enable_pager = $moment_customizer_all_values['moment-fs-enable-pager'];
        $moment_feature_enable_autoplay = $moment_customizer_all_values['moment-fs-enable-autoplay'];
        $moment_feature_enable_title = $moment_customizer_all_values['moment-fs-enable-title'];
        $moment_feature_enable_caption = $moment_customizer_all_values['moment-fs-enable-caption'];
        $moment_feature_enable_button = $moment_customizer_all_values['moment-fs-enable-button'];
        $moment_home_slider_button = $moment_customizer_all_values['moment-fs-button-text'];
     

    ?>

    <?php if ($moment_feature_slider_number == 1) { ?>
        <section class="wrapper main-slider wrapper-slider">
            <div class="container-full">
                    <?php
                    $i = 1;
                    foreach( $moment_slider_arrays as $moment_slider_array ){
                        if( $moment_feature_slider_number < $i){
                            break;
                        }
                        if(empty($moment_slider_array['moment-feature-slider-image'])){
                            $moment_feature_slider_image = get_template_directory_uri().'/assets/img/no-image-1260_530.png';
                        }
                        else{
                            $moment_feature_slider_image =$moment_slider_array['moment-feature-slider-image'];
                        }
                        ?>
                        <div class="slide-item">
                            <div class="container-full" style="background-image: url('<?php echo esc_url( $moment_feature_slider_image )?>');">
                                <div class="thumb-overlay"></div>
                                <div class="row">
                                    <div class="column-xsd-10 column-sd-title-divider column-xsd-offset-1 column-sd-offset-1 banner-content">
                                        <?php if ((1 == $moment_feature_enable_title) || (1 == $moment_feature_enable_caption)){?>
                                            <div class="banner-content-holder">
                                                <div class="banner-content-inner">
                                                    <?php if( 1 == $moment_feature_enable_title) {
                                                        ?>
                                                            <h2 class="slider-title"><a href="<?php echo esc_url( $moment_slider_array['moment-feature-slider-link'] );?>"><?php echo esc_html( $moment_slider_array['moment-feature-slider-title'] );?></a></h2>
                                                        <?php
                                                    }
                                                    if( 1 == $moment_feature_enable_caption){
                                                        ?>
                                                        <div class="text-content">
                                                            <?php echo wp_kses_post( $moment_slider_array['moment-feature-slider-content'] );?>
                                                        </div>
                                                        <?php
                                                    }?>
                                                    <?php if ( 1 == $moment_feature_enable_button) { ?>
                                                        <div class="btn-holder">
                                                            <a href="<?php echo esc_url( $moment_slider_array['moment-feature-slider-link'] );?>" class="slider-button">
                                                                <?php echo esc_html($moment_home_slider_button);?>
                                                            </a>
                                                        </div> 
                                                    <?php }
                                                    ?>    
                                                </div>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <?php
                    $i++;
                    }
                    ?>
            </div>
        </section>
    <?php }
    else { ?>
        <section class="wrapper main-slider wrapper-slider">
            <?php if( 1 == $moment_feature_enable_arrow){ ?>
                <div class="controls">
                    <a href="#" class="slide-prev" id="slide-prev"><i class="fa fa-angle-left"></i></a> 
                    <a href="#" class="slide-next" id="slide-next"><i class="fa fa-angle-right"></i></a>
                </div>
            <?php }  ?>

            <div class="container-full">
                <div class="cycle-slideshow"
                data-cycle-fx=<?php echo esc_attr( $moment_feature_slider_mode);?>
                data-cycle-speed="<?php echo (esc_attr( $moment_feature_slider_time) * 1000)?>"
                data-cycle-carousel-fluid=true
                data-cycle-carousel-visible=1
                data-cycle-pause-on-hover="true"
                data-cycle-slides="> div"
                data-cycle-prev="#slide-prev"
                data-cycle-next="#slide-next"
                data-cycle-auto-height=container
                data-cycle-swipe=true
                data-cycle-swipe-fx=scrollHorz
                <?php if( 1 == $moment_feature_enable_pager){ ?>
                    data-cycle-pager="#slide-pager"
                <?php }  ?>
                <?php if( 1 != $moment_feature_enable_autoplay){ ?>
                    data-cycle-timeout=0
                <?php }  ?>
                <?php if(1 == $moment_feature_enable_autoplay){ ?>
                    data-cycle-timeout=<?php echo (esc_attr( $moment_feature_slider_pause_time) * 1000)?>
                <?php }  ?>
                >
                    <?php
                    $i = 1;
                    foreach( $moment_slider_arrays as $moment_slider_array ){
                        if( $moment_feature_slider_number < $i){
                            break;
                        }
                        if(empty($moment_slider_array['moment-feature-slider-image'])){
                            $moment_feature_slider_image = get_template_directory_uri().'/assets/img/no-image-1260_530.png';
                        }
                        else{
                            $moment_feature_slider_image =$moment_slider_array['moment-feature-slider-image'];
                        }
                        ?>
                        <div class="slide-item">
                            <div class="container-full" style="background-image: url('<?php echo esc_url( $moment_feature_slider_image )?>');">
                                <div class="thumb-overlay"></div>
                                <div class="row">
                                    <div class="column-xsd-10 column-sd-10 column-xsd-offset-1 column-sd-offset-1 banner-content">
                                        <?php if ((1 == $moment_feature_enable_title) || (1 == $moment_feature_enable_caption)){?>
                                            <div class="banner-content-holder">
                                                <div class="banner-content-inner">
                                                    <?php if( 1 == $moment_feature_enable_title) {
                                                        ?>
                                                            <h2 class="slider-title"><a href="<?php echo esc_url( $moment_slider_array['moment-feature-slider-link'] );?>"><?php echo esc_html( $moment_slider_array['moment-feature-slider-title'] );?></a></h2>
                                                        <?php
                                                    }
                                                    if( 1 == $moment_feature_enable_caption){
                                                        ?>
                                                        <div class="text-content">
                                                            <?php echo wp_kses_post( $moment_slider_array['moment-feature-slider-content'] );?>
                                                        </div>
                                                        <?php
                                                    }?>
                                                    <?php if ( 1 == $moment_feature_enable_button) { ?>
                                                        <div class="btn-holder">
                                                            <a href="<?php echo esc_url( $moment_slider_array['moment-feature-slider-link'] );?>" class="slider-button">
                                                                <?php echo esc_html($moment_home_slider_button);?>
                                                            </a>
                                                        </div> 
                                                    <?php }
                                                    ?>      
                                                </div>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <?php
                    $i++;
                    }
                    ?>

                </div>
            </div>
            <div class="cycle-pager slide-pager" id="slide-pager"></div>
        </section>
    <?php }
    }
}
endif;
add_action( 'moment_featured_slider', 'moment_featured_home_slider', 10 );