<?php

/**
 * Class to generate sample data
 */
class MetaSliderPro_Quickstart {

    /**
     * Insert sample data for multiple slides
     * 
     * @since 2.35
     * 
     * @param string $slug      Basically the slide type or a custom slug e.g. 'external_video', 'youtube' or 'youtube-basic'
     * @param int $slider_id    Slideshow ID
     * 
     * @return array
     */
    public function import_slides( $slug, $slider_id )
    {
        // Use this to check later if everything was processed correctly
        $all_slides = array();

        switch ( $slug ) {

            case 'youtube':
                $youtubeSlide = new MetaYouTubeSlide();

                $videos = array(
                    '5XEtBHYUPg4',
                    'O1dgtYkfQZU',
                    'hVvEISFw9w0'
                );

                $fields = array(
                    'menu_order' => 9999,
                    'video_type' => 'video',
                    'settings' => array(
                        'lazyLoad' => 'on',
                        'mute' => 'off',
                        'showControls' => 'off',
                        'autoPlay' => 'on'
                    )
                );

                foreach ( $videos as $video_id ) {
                    $fields['video_id']     = $video_id;
                    $fields['video_url']    = 'https://www.youtube.com/watch?v=' . $video_id;

                    $new_slide_id = $youtubeSlide->import_slide( $slider_id, $fields );

                    if ( $new_slide_id ) {
                        $all_slides[] = $new_slide_id;
                    }
                }
                break;

            case 'youtube-shorts':
                $youtubeSlide = new MetaYouTubeSlide();

                $videos = array(
                    'VrO6BvrZB-E',
                    '-fSwO37nsHI',
                    '29ESP82CCQ8',
                    'wmJuqv96su8'
                );

                $fields = array(
                    'menu_order' => 9999,
                    'video_type' => 'video',
                    'settings' => array(
                        'lazyLoad' => 'on',
                        'mute' => 'off',
                        'showControls' => 'on',
                        'autoPlay' => 'off'
                    )
                );

                foreach ( $videos as $video_id ) {
                    $fields['video_id']     = $video_id;
                    $fields['video_url']    = 'https://www.youtube.com/shorts/' . $video_id;

                    $new_slide_id = $youtubeSlide->import_slide( $slider_id, $fields );

                    if ( $new_slide_id ) {
                        $all_slides[] = $new_slide_id;
                    }
                }
                break;
    
            case 'vimeo':
                $vimeoSlide = new MetaVimeoSlide();

                $videos = array(
                    '236156153',
                    '140088872',
                    '157588560'
                );

                $fields = array(
                    'menu_order' => 9999,
                    'video_type' => 'video',
                    'settings' => array(
                        'controls' => 'off',
                        'mute' => 'on',
                        'autoPlay' => 'on',
                        'title' => 'on'
                    )
                );

                foreach ( $videos as $video_id ) {
                    $fields['video_id']     = $video_id;

                    $new_slide_id = $vimeoSlide->create_slide( $slider_id, $fields );

                    if ( $new_slide_id ) {
                        $all_slides[] = $new_slide_id;
                    }
                }
                break;
            
            case 'html_overlay':
                //$images = MetaSlider_Image::instance()->get_theme_images('radix', 3);
                
                /* @since 2.37
                 * We use this method to declare images instead of get_theme_images() 
                 * due that retrieve more images than neened */
                $images = array(
                    'margo-brodowicz-183156-unsplash.jpg' => array(
                            'source' => METASLIDER_PATH . 'themes/images/margo-brodowicz-183156-unsplash.jpg'
                        ),
                    'manki-kim-269196-unsplash.jpg' => array(
                            'source' => METASLIDER_PATH . 'themes/images/manki-kim-269196-unsplash.jpg'
                        ),
                    'artem-bali-680991-unsplash.jpg' => array(
                            'source' => METASLIDER_PATH . 'themes/images/artem-bali-680991-unsplash.jpg'
                        )
                );

                $all_slides = $this->create_slides_through_images( $slug, $slider_id, $images );
                break;

            case 'post_feed':

                $postFeedSlide = new MetaPostFeedSlide();
                $new_slide_id = $postFeedSlide->create_slide( $slider_id );
                
                if ( $new_slide_id ) {
                    $all_slides[] = $new_slide_id;
                }

                break;

            case 'external':
                $externalSlide = new MetaExternalSlide();

                $fields = array(
                    'menu_order' => 9999,
                );

                $images = array(
                    METASLIDER_THEMES_URL . '/images/artem-bali-680991-unsplash.jpg',
                    METASLIDER_THEMES_URL . '/images/etienne-beauregard-riverin-48305-unsplash.jpg',
                    METASLIDER_THEMES_URL . '/images/margo-brodowicz-183156-unsplash.jpg'
                );

                foreach ( $images as $image ) {
                    $fields['extimgurl'] = $image;
                    $new_slide_id = $externalSlide->create_slide( $slider_id, $fields );

                    if ( $new_slide_id ) {
                        $all_slides[] = $new_slide_id;
                    }
                }
                break;

            case 'external_video':
                $images = array(
                    'big-buck-bunny.jpg' => array(
                            'source' => METASLIDERPRO_PATH . 'modules/quickstart/images/external-video/big-buck-bunny.jpg'
                        ),
                    'elephants-dream.jpg' => array(
                            'source' => METASLIDERPRO_PATH . 'modules/quickstart/images/external-video/elephants-dream.jpg'
                        ),
                    'sintel.jpg' => array(
                            'source' => METASLIDERPRO_PATH . 'modules/quickstart/images/external-video/sintel.jpg'
                        )
                );

                $all_slides = $this->create_slides_through_images( $slug, $slider_id, $images );
                break;

        }

        return $all_slides;
    }

    /**
     * Import theme images
     *
     * @since 2.35
     * @since 2.37 - $theme_id deprecated - No longer needed due we can't limit the number of images
     * 
     * @param string $type      Basically the slide type
     * @param int $slider_id    Slideshow ID
     * @param string $images    Array of images to upload
     * @param string $theme_id  Theme id that have demo images in it's manifest.php declaration. e.g. 'bubble'
     * 
     * @return array New slide IDs
     */
    public function create_slides_through_images( $type, $slider_id, $images = array(), $theme_id = 'radix' )
    {
        $all_slides = array();

        // Capture the slide markup that is typically echoed from legacy code
        ob_start();

        // @since 2.37 - Removed $theme_id as second param
        $image_ids = MetaSlider_Image::instance()->import( $images );

        if ( is_wp_error( $image_ids ) ) {
            wp_send_json_error( array(
                'message' => $image_ids->get_error_message()
            ), 400 );
        }

        $errors = array();

        switch ( $type ) {
                
            case 'html_overlay':

                $html = array(
                    '<div class="layer" style="width: 1199.67px; height: 98px; top: 215px; left: 0px; position: absolute;" data-width="1199.67" data-height="98" data-top="215" data-left="0"><div class="animation_in animated fadeIn" data-animation="fadeIn" data-animation-delay="0" style="width: 100%; height: 100%;"><div class="animation_out animated disabled" data-animation="disabled" data-animation-delay="0" style="height: 100%; width: 100%;"><div class="ms_content_wrap" style="height: 100%; background: rgba(22, 123, 200, 0.45);"><div class="content" id="layer_content_411135207" style="color: white; padding: 10px; position: relative;" data-padding="10"><p style="text-align: center;"><span style="font-size:3em;"><span style="font-family:verdana,geneva,sans-serif;">Flying Around The World</span></span></p></div></div></div></div></div><div class="layer" style="width: 1200px; height: 64px; top: 315px; left: 0px; position: absolute;" data-width="1200" data-height="64" data-top="315" data-left="0"><div class="animation_in animated rotateIn" data-animation="rotateIn" data-animation-delay="0.5" style="animation-delay: 500ms; -moz-animation-delay: 500ms; -webkit-animation-delay: 500ms;"><div class="animation_out animated disabled" data-animation="disabled" data-animation-delay="0" style="animation-delay: 500ms; -moz-animation-delay: 500ms; -webkit-animation-delay: 500ms;"><div class="ms_content_wrap" style="height: 100%; background: rgba(83, 163, 204, 0.56);"><div class="content" id="layer_content_842868579" style="color: white; padding: 10px; position: relative;" data-padding="10"><p style="text-align: center;"><span style="font-size:1.5em;">We reveal the 10 best destinations</span></p></div></div></div></div></div>',
                    '<div class="layer" style="width: 473px; height: 97px; top: 245px; left: 725px; position: absolute;" data-width="473" data-height="95" data-top="245" data-left="725"><div class="animation_in animated fadeInUp fadeInDown" data-animation="fadeInDown" data-animation-delay="1.5" style="animation-delay: 1500ms; -moz-animation-delay: 1500ms; -webkit-animation-delay: 1500ms;"><div class="animation_out animated disabled" data-animation="disabled" data-animation-delay="0" style="animation-delay: 1500ms; -moz-animation-delay: 1500ms; -webkit-animation-delay: 1500ms;"><div class="ms_content_wrap" style="height: 100%; background: rgba(142, 142, 142, 0);"><div class="content" id="layer_content_473846889" style="color: white; padding: 5px; position: relative;" data-padding="5"><p style="text-align: center;"><span style="font-size:1.5em;"><span style="font-family:courier new,courier,monospace;"><strong>Imported from Colombia</strong></span></span></p></div></div></div></div></div><div class="layer" style="width: 477px; height: 137px; top: 245px; left: 0px; position: absolute;" data-width="477" data-height="137" data-top="245" data-left="0"><div class="animation_in animated flipInX fadeInUp" data-animation="fadeInUp" data-animation-delay="0.5" style="animation-delay: 500ms; -moz-animation-delay: 500ms; -webkit-animation-delay: 500ms;"><div class="animation_out animated swing" data-animation="swing" data-animation-delay="0.2" style="animation-delay: 700ms; -moz-animation-delay: 700ms; -webkit-animation-delay: 700ms;"><div class="ms_content_wrap" style="height: 100%; background: rgba(142, 142, 142, 0);"><div class="content" id="layer_content_717645243" style="color: white; padding: 5px; position: relative;" data-padding="5"><p style="text-align: center;"><span style="font-size:1.875em;"><strong><span style="font-family:courier new,courier,monospace;"><span style="color:#FFFFFF;">Best Coffee in NYC!</span></span></strong></span></p></div></div></div></div></div>',
                    '<div class="layer" style="width: 858px; height: 101.667px; top: 230px; left: 0px; position: absolute;" data-width="858" data-height="101.667" data-top="230" data-left="0"><div class="animation_in animated fadeInLeft" data-animation="fadeInLeft" data-animation-delay="0.7" style="animation-delay: 700ms; -moz-animation-delay: 700ms; -webkit-animation-delay: 700ms;"><div class="animation_out animated disabled" data-animation="disabled" data-animation-delay="0" style="animation-delay: 700ms; -moz-animation-delay: 700ms; -webkit-animation-delay: 700ms;"><div class="ms_content_wrap" style="height: 100%; background: rgba(35, 34, 34, 0.5);"><div class="content" id="layer_content_463106150" style="color: white; padding: 20px; position: relative;" data-padding="20"><p style="text-align: right;"><span style="font-size:2.625em;">Where to find the best crafts?</span></p></div></div></div></div></div>'
                );
        
                /* Add 3 slides to make sure the $html array length is enough
                 * ...or we can also use count( $image_ids ) insead of number 2 
                 * and adjust $html length */
                for ( $i = 0; $i < 2 ; $i++ ) { 
        
                    $new_slide_id = $this->create_layer_slide( $slider_id, $image_ids[$i], $html[$i] );
        
                    if ( $new_slide_id ) {
                        $all_slides[] = $new_slide_id;
                    }
                }

                $new_slide_id = $this->create_layer_slide( $slider_id, $image_ids[$i], $html[$i] );
                break;
            
            case 'external_video':
                $externalVideoSlide = new MetaExternalVideoSlide();

                $fields = array(
                    'menu_order' => 9999,
                );

                $videos = array(
                    'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
                    'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4',
                    'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4'
                );

                foreach ( $videos as $i => $video ) {
                    // Upload image
                    $slide = new MetaSlider_Slide( $slider_id );
                    $slide->add_image( $image_ids[$i] );

                    $fields = array(
                        'menu_order' => 9999,
                        'video_url' => $video
                    );

                    $new_slide_id = $externalVideoSlide->create_slide( $slider_id, $fields );

                    if ( $new_slide_id ) {
                        $all_slides[] = $new_slide_id;
                        
                        // Assign the image to the slide
                        update_post_meta( $new_slide_id, '_thumbnail_id', $image_ids[$i] );
                    }
                }
                break;
            // Add more $type cases...

        }

        // Disregard the output. It's not needed for imports
        ob_end_clean();

        // Send back the first error, if any
        if ( isset( $errors[0] ) ) {
            wp_send_json_error( array(
                'message' => $errors[0]->get_error_message()
            ), 400 );
        }

        return $all_slides;
    }

    /**
     * Create a new Layer slide
     * 
     * @since 2.35
     * 
     * @param int $slider_id    Slideshow ID
     * @param int $image_id     Image ID
     * 
     * @return int The slide ID
     */
    public function create_layer_slide( $slider_id, $image_id, $html )
    {
        // Layer slide
        $slide      = new MetaSlider_Slide( $slider_id );
        $layerSlide = new MetaLayerSlide( $slider_id );

        // Upload image
        $slide->add_image( $image_id );

        // Create a new slide
        $slide_id = $layerSlide->create_slide( $slider_id );

        // Add HTML
        $layerSlide->add_or_update_or_delete_meta( $slide_id, 'html', $html );

        if ( ! $slide_id ) {
            wp_send_json_error( array(
                'message' => __( 'Slide ID not found', 'ml-slider-pro' )
            ), 400 );
        }

        // Assign the image to the slide
        update_post_meta( $slide_id, '_thumbnail_id', $image_id );

        return $slide_id;
    }

    /**
     * Setup slideshow settings
     * 
     * @since 2.35
     * 
     * @param string $slug Basically the slideshow type or a custom slug
     * 
     * @return array
     */
    public function set_slideshow_settings( $slug )
    {
        $settings = array();

        switch ( $slug ) {

            case 'youtube':
                $settings = array(
                    'type' => 'flex',
                    'width' => 1200,
                    'height' => 675,
                    'links' => 'on',
                    'effect' => 'slide',
                    'printCss' => 'on',
                    'printJs' => 'on',
                    'center' => 'on',
                    'autoPlay' => 'on',
                    'fullWidth' => 'on',
                    'noConflict' => 'on'
                );
                break;

            
            case 'youtube-shorts':
                $settings = array(
                    'type' => 'flex',
                    'width' => 400,
                    'height' => 600,
                    'links' => 'off',
                    'navigation' => 'off',
                    'effect' => 'slide',
                    'printCss' => 'on',
                    'printJs' => 'on',
                    'center' => 'on',
                    'autoPlay' => 'on',
                    'fullWidth' => 'on',
                    'noConflict' => 'on',
                    'loop' => 'continuously',
                    'carouselMode' => 'on',
                    'carouselMargin' => 5,
                    'smartCrop' => 'on',
                    'infiniteLoop' => 'on',
                    'touch' => 'on'
                );
                break;
    
            
            case 'vimeo':
                $settings = array(
                    'type' => 'flex',
                    'width' => 1200,
                    'height' => 600,
                    'links' => 'on',
                    'effect' => 'slide',
                    'printCss' => 'on',
                    'printJs' => 'on',
                    'center' => 'on',
                    'autoPlay' => 'on',
                    'fullWidth' => 'on',
                    'noConflict' => 'on'
                );
                break;

            case 'html_overlay':
                $settings = array(
                    'type' => 'flex',
                    'width' => 1200,
                    'height' => 600,
                    'links' => 'on',
                    'effect' => 'slide',
                    'printCss' => 'on',
                    'printJs' => 'on',
                    'center' => 'on',
                    'autoPlay' => 'on',
                    'fullWidth' => 'on',
                    'noConflict' => 'on'
                );
                break;

            case 'post_feed':
                $settings = array(
                    'type' => 'flex',
                    'width' => 1200,
                    'height' => 600,
                    'links' => 'on',
                    'effect' => 'slide',
                    'printCss' => 'on',
                    'printJs' => 'on',
                    'center' => 'on',
                    'autoPlay' => 'on',
                    'fullWidth' => 'on',
                    'noConflict' => 'on'
                );
                break;

            case 'external':
                $settings = array(
                    'type' => 'flex',
                    'width' => 1200,
                    'height' => 600,
                    'links' => 'on',
                    'effect' => 'slide',
                    'printCss' => 'on',
                    'printJs' => 'on',
                    'center' => 'on',
                    'autoPlay' => 'on',
                    'fullWidth' => 'on',
                    'noConflict' => 'on'
                );
                break;

            case 'external_video':
                $settings = array(
                    'type' => 'flex',
                    'width' => 900,
                    'height' => 450,
                    'links' => 'on',
                    'effect' => 'slide',
                    'printCss' => 'on',
                    'printJs' => 'on',
                    'center' => 'on',
                    'autoPlay' => 'on',
                    'fullWidth' => 'on',
                    'noConflict' => 'on'
                );
                break;

        }

        return $settings;
    }

    /**
     * Set slideshow theme configuration
     * 
     * @since 2.35
     * 
     * @param int $slideshow_id Slideshow ID
     * @param string $theme_id  Theme ID. e.g. 'bubble', 'radix', 'simply-dark'
     * 
     * @return bool
     */
    public function set_slideshow_theme( $slideshow_id, $theme_id )
    {
        $themeInstance  = new MetaSlider_Themes();

        $theme          = $themeInstance->get_theme_object( $slideshow_id, $theme_id );
        $setTheme       = $themeInstance->set( $slideshow_id, $theme );

        return $setTheme;
    }
}