<?php
/* Adds Maskitto_Services widget. */
class Maskitto_Slider extends WP_Widget {

    /* Register widget with WordPress. */
    function __construct() {
        parent::__construct(
            'maskitto_slider',
            __('Maskitto: Slider', 'maskitto-light'),
            array(
                'description' => __( 'Only for page builder.', 'maskitto-light' ),
                'panels_icon' => 'dashicons dashicons-format-video',
                'panels_groups' => 'theme-widgets'
            )
        );
    }


    /* Front-end display of widget. */
    public function widget( $args, $instance ) {
        $sliders = wp_count_posts( 'slider' );
        if( isset( $sliders->publish ) && $sliders->publish > 0){
    ?>
        <div class="page-slideshow">
            <i class="fa fa-circle-o-notch fa-spin"></i>
            <div class="slideshow">
                <?php
                    $loop = new WP_Query( array('post_type' => 'slider', 'posts_per_page' => 10 ) );
                    while ( $loop->have_posts() ) : $loop->the_post();

                    $class1 = (string) NULL;
                    $class2 = (string) NULL;
                    $style1 = (string) NULL;

                    $image = esc_url( get_post_meta( get_the_ID(), 'wpcf-background-image', true ));
                    $caption = esc_attr( get_post_meta( get_the_ID(), 'wpcf-caption', true ));

                    $button_name = esc_attr( get_post_meta( get_the_ID(), 'wpcf-button-name', true ));
                    $button_url = esc_url( get_post_meta( get_the_ID(), 'wpcf-button-url', true ));
                    if( !$button_url ) $button_url = '#';
                    $button_icon = esc_attr( get_post_meta( get_the_ID(), 'wpcf-button-icon', true ));

                    $button_name2 = esc_attr( get_post_meta( get_the_ID(), 'wpcf-button-name2', true ));
                    $button_url2 = esc_url( get_post_meta( get_the_ID(), 'wpcf-button-url2', true ));
                    if( !$button_url2 ) $button_url2 = '#';
                    $button_icon2 = esc_attr( get_post_meta( get_the_ID(), 'wpcf-button-icon2', true ));

                    $background_color = esc_attr( get_post_meta( get_the_ID(), 'wpcf-background-color', true ));
                    $background_shadow = esc_attr( get_post_meta( get_the_ID(), 'wpcf-background-shadow', true ));
                    $background_pattern = esc_attr( get_post_meta( get_the_ID(), 'wpcf-background-texture', true ));
                    $caption_background = esc_attr( get_post_meta( get_the_ID(), 'wpcf-caption-background', true ));

                    $align_horizontal = esc_attr( get_post_meta( get_the_ID(), 'wpcf-text-horizontal', true ));
                    $align_vertical = esc_attr( get_post_meta( get_the_ID(), 'wpcf-text-vertical', true ));

                    if( !$background_shadow ) {
                        $class1.= ' remove-shadow';
                    }

                    if( $image ) {
                        $style1.= "background-image: url($image);";
                    }

                    if( $background_color ) {
                        $style1.= "background-color: $background_color;";
                    }

                    if( $background_pattern ) {
                        $class2.= ' slide-patern-show';
                    }

                    if( $align_vertical == 'top' ) {
                        $class2.= ' slide-align-top';
                    }

                    if( $align_horizontal == 'left' ) {
                        $class2.= ' slide-align-left';
                    } else if( $align_horizontal == 'right' ) {
                        $class2.= ' slide-align-right';
                    }

                    if( $caption_background ) {
                        $class2.= ' caption-background';
                    }

                ?>
                    <div class="slideshow-slide<?php echo $class1; ?>" style="<?php echo $style1; ?>">
                        <div class="slide-patern<?php echo $class2; ?>">
                            <div class="container">
                                <div class="slide-details">
                                    <div class="slide-title"><?php the_title(); ?><?php echo maskitto_light_admin_edit(get_the_ID()); ?></div>
                                    <div></div>
                                    <?php if( $caption ) { ?>
                                        <div class="slide-info"><?php echo $caption; ?></div>
                                    <?php } ?>
                                    <?php if( $button_name || $button_name2 ) { ?>
                                        <div class="slide-button">
                                            <?php if( $button_name ) { ?>
                                                <a href="<?php echo $button_url; ?>" class="btn btn-danger">
                                                    <?php if( $button_icon ) { ?>
                                                        <i class="fa <?php echo $button_icon; ?>"></i>
                                                    <?php } ?>
                                                    <?php echo $button_name; ?>
                                                </a>
                                            <?php } ?>

                                            <?php if( $button_name2 ) { ?>
                                                <a href="<?php echo $button_url2; ?>" class="btn btn-white">
                                                    <?php if( $button_icon2 ) { ?>
                                                        <i class="fa <?php echo $button_icon2; ?>"></i>
                                                    <?php } ?>
                                                    <?php echo $button_name2; ?>
                                                </a>
                                            <?php } ?>
                                        </div>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>


    <?php } }

    /* Sanitize widget form values as they are saved. */
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['subtitle'] = ( ! empty( $new_instance['subtitle'] ) ) ? strip_tags( $new_instance['subtitle'] ) : '';

        return $instance;
    }

}