<?php
/**
 * Created by PhpStorm.
 * User: marco
 * Date: 16/07/18
 * Time: 19.27
 */
/**
 * @param $model_array
 *
 * @return string
 */
function simple_content($model_array)
{
    $title = html5_h1($model_array[o66_model_fields_keys::title]);
    $acf_post_id = $model_array[o66_model_fields_keys::wp_id];
    $image_size = o66_image_sizes::nocrop_container;
    $main_image = '';

    if (class_exists('acf')) {
        $acf_image = get_field('main_image', $acf_post_id);
        if (!empty($acf_image)) {

//			$desktopImageSize = $image_size;
//			$mobileImageSizes = 'quad';

            $image = $acf_image['sizes'][$image_size];

            if (!empty($image)) : $main_image .= '
                <div class="main-image">
                    <div class="main-image__image-container img-responsive-container">
                        <img src="' . $image . '" alt="' . $model_array[o66_model_fields_keys::title] . '">
                    </div>
                </div>';
            endif;

        }
        if (empty($acf_image)) : $main_image .= '
                <div class="image-placeholder" style="height:25px; float: left; width: 100%">
                    
                </div>';
        endif;
    }

    $content =
        html5_div($title . $main_image . $model_array[o66_model_fields_keys::content], '', 'content');
    $acf_string = '';
    $field_name = 'content_block';

    while (have_posts()) : the_post();
        while (have_rows($field_name, $acf_post_id)) :
            the_row(); ?>
            <?php if (get_row_layout() == 'text_editor_block') : ?>
            <?php $acf_string .= '<div class="content">' . get_sub_field('text_editor') . '</div>'; ?>
            <?php // endif; ?>
        <?php elseif (get_row_layout() == 'gallery_block') : ?>
            <?php
            $gallery_images = get_sub_field('gallery');
            $column_size_field = get_sub_field('column_size');
            //col-lg-4 col-md-4 col-sm-4 col-xs-6 : 1/3
            //col-lg-3 col-md-3 col-sm-4 col-xs-6 : 1/4
            //col-lg-2 col-md-2 col-sm-4 col-xs-6 : 1/6

           // var_dump($column_size_field);
            ?>
            <?php if ($gallery_images) :
                $acf_string .= '<div class="entry-content__gallery gal_js"><div class="row">';
                $size_string = 'col-lg-3 col-md-3 col-sm-4 col-xs-6';
                if (!empty($column_size_field)){
                   if ($column_size_field == 'Un Terzo'){ $size_string = 'col-lg-4 col-md-4 col-sm-4 col-xs-6';}
                   if ($column_size_field == 'Un Quarto'){ $size_string = 'col-lg-3 col-md-3 col-sm-4 col-xs-6';}
                   if ($column_size_field == 'Un Sesto'){ $size_string = 'col-lg-2 col-md-2 col-sm-4 col-xs-6';}
                }

                foreach ($gallery_images as $gallery_image):
                    //  var_dump($gallery_image);
                    $acf_string .= "<div class='".$size_string."'>
                                <div class='entry-content__gallery__image'><a href='{$gallery_image["url"]}'><img src='{$gallery_image["sizes"]["quad"]}'
                                             alt='{$gallery_image["alt"]}' class='img-responsive'/>
                                    </a>
                                </div>
                            </div>"; ?>
                <?php endforeach;
                $acf_string .= '
                    </div>
                </div>';

            endif; ?>
        <?php elseif (get_row_layout() == 'video_gallery_block') : ?>
            <?php $gallery_images = get_sub_field('videogallery');
            $column_size_field = get_sub_field('column_size');
            $size_string = 'col-lg-4 col-md-4 col-sm-4 col-xs-6';
            if (!empty($column_size_field)){
                if ($column_size_field == 'Un Terzo'){ $size_string = 'col-lg-4 col-md-4 col-sm-6 col-xs-12';}
                if ($column_size_field == 'Un Quarto'){ $size_string = 'col-lg-3 col-md-3 col-sm-6 col-xs-12';}
                if ($column_size_field == 'Un Sesto'){ $size_string = 'col-lg-2 col-md-2 col-sm-6 col-xs-12';}
            }

            ?>
            <?php if ($gallery_images) :
                $acf_string .= '<div class="entry-content__gallery __is-video"><div class="row">';
                foreach ($gallery_images as $video_gallery_item):
                   // var_dump($video_gallery_item);
                    $video_url = $video_gallery_item["video_url"];
                    $gallery_image = '';

                    if ( strpos($video_url, 'youtube')){
                        parse_str( parse_url($video_url, PHP_URL_QUERY), $url_array);
                        $pre_video_id = $url_array['v'];

                        $gallery_image = 'url("'.'https://img.youtube.com/vi/'.$pre_video_id.'/mqdefault.jpg'.'")';
                    }

                    if ( strpos($video_url, 'vimeo')){
                       // parse_str( parse_url($video_url, PHP_URL_QUERY), $url_array);
                        //var_dump($url_array);
                        preg_match("/[^\/]+$/", $video_url, $matches);
                        $imgid = $matches[0];

                        $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$imgid.php"));

                        $gallery_image = 'url("'.$hash[0]['thumbnail_medium'].'")';
                        //<img src='${gallery_image}'
                        //                                             alt='{$video_gallery_item["video_title"]}' class='img-responsive'/>
                    }

                    $thumbnail = $video_gallery_item['thumbnail']['sizes']['rect'];
                  //  var_dump($thumbnail);
                    if (!empty($thumbnail)){
                        $gallery_image  = 'url("'.$thumbnail.'");';
                    }







                    $acf_string .= "<div class='.$size_string.'>
                                <div class='entry_video js-match-height'><a href='{$video_gallery_item["video_url"]}'>
                                <div class='ratio-video' style='background-image: ${gallery_image};'></div>                                
                                             <h3>{$video_gallery_item["video_title"]}</h3>
                                    </a>
                                    <div class='video_desc'>{$video_gallery_item["video_description"]}</div>
                                </div>
                            </div>"; ?>
                <?php endforeach;
                $acf_string .= '
                    </div>
                </div>';

            endif; ?>
        <?php endif; ?>


        <?php endwhile;
    endwhile;

    return html5_div(
        $content . $acf_string, null, 'blok simple_content_component'
    );
}
