<?php
$galleryImages = $this->getGalleryImages();
$zoom_image = $this->getEnableZoom();
$lbox_image = $this->getLbox();
$rtl = $this->getRightToLeft();
$_product = $this->getProduct();
$arImage = $_product->getData('mgs_arimage');
$arTemplate = $this->getLayout()->createBlock("MGS\ClaueTheme\Block\AbstractProduct")->setProduct($_product)->setFullWidth(true)->setTemplate("MGS_ClaueTheme::arImage.phtml")->toHtml();

?>
<div class="vertical-gallery<?= count($galleryImages) <= 1 ? ' no-thumb' : '' ?>">
    <?php if (count($galleryImages) > 1): ?>
        <div id="vertical-thumbnail-wrapper">
            <div id="vertical-thumbnail" class="vertical-thumbnail">
                <?php $i = 0; foreach ($galleryImages as $image): ?>
                    <?php $lbImage = $image->getLabel() ? $image->getLabel() : $image->getLabelDefault(); ?>
                    <div class="item-thumb<?= $i == 0 ? ' active' : '' ?>" data-owl="<?= $i ?>">
                        <img class="img-fluid" src="<?= $block->escapeUrl($image->getSmallImageUrl()); ?>" alt="<?= $lbImage ?>"/>

                        <?php if ($image->getMediaType() == 'external-video'): ?>
                            <div class="popup-video-thumb"><span class="ti-video-camera"></span></div>
                        <?php elseif ($image->getArImage()): ?>
                            <a class="ar-thumbnail"><span><i class="pe-7s-box2"></i></span></a>
                        <?php endif ?>
                    </div>
                    <?php $i++; ?>
                <?php endforeach ?>
            </div>
        </div>
    <?php endif; ?>

    <div id="owl-carousel-gallery" class="owl-carousel gallery-vertical">
        <?php $i =0; foreach ($galleryImages as $image): $i++; ?>
            <?php $lbImage = $image->getLabel() ? $image->getLabel() : $image->getLabelDefault(); ?>
            <?php $isVideo = $image->getMediaType() == 'external-video' ? true : false; ?>
            <?php $href = $isVideo ? $image->getVideoUrl() : $image->getImageZoom(); ?>

            <div class="product item-image imgzoom<?= $isVideo ? ' item-image-video' : '' ?><?= ($image->getArImage() && $arImage) ? ' disable-owl-swipe' : '' ?>" data-zoom="<?= $block->escapeUrl($image->getImageZoom()); ?>">
                <?php if ($isVideo): ?>
                    <div class="label-video"><?= __('Video') ?></div>
                <?php endif; ?>

                <?php if ($image->getArImage()): ?>
                    <?php if ($arImage): ?>
                        <?= $arTemplate ?>
                    <?php else: ?>
                        <img class="img-responsive" src="<?= $block->escapeUrl($image->getLargeImageUrl()); ?>" alt="<?= $lbImage ?>"/>
                    <?php endif; ?>
                <?php else: ?>
                    <?php if ($lbox_image): ?>
                        <a class="lb<?= $isVideo ? ' video-link' : '' ?>" href="<?= $block->escapeUrl($href); ?>">
                            <img class="img-fluid" src="<?= $block->escapeUrl($image->getLargeImageUrl()); ?>" alt="<?= $lbImage ?>"/>
                        </a>
                    <?php else: ?>
                        <img class="img-fluid" src="<?= $block->escapeUrl($image->getLargeImageUrl()); ?>" alt="<?= $lbImage ?>"/>
                        <?php if ($isVideo): ?>
                            <a target="_blank" rel="noopener" class="popup-video" href="<?= $image->getVideoUrl(); ?>">
                                <span class="ti-video-camera"></span>
                            </a>
                        <?php endif; ?>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<script>
    require([ 'jquery', 'mgsslick', 'mgsowlcarousel' ], function($) {
        const owlGallery = $('#owl-carousel-gallery').owlCarousel({
            items: 1,
            autoplay: false,
            lazyLoad: false,
            nav: true,
            dots: false,
            navText: ["<i class='pe-7s-angle-left'></i>","<i class='pe-7s-angle-right'></i>"],
            rtl: false,
            onInitialized: function(){
                var carowselHeight = $('#owl-carousel-gallery .owl-stage-outer').height();
                $('#owl-carousel-gallery .owl-item model-viewer').height(carowselHeight);
            },
            <?php if ($arImage): ?>
                touchDrag: false,
                mouseDrag: false,
            <?php endif; ?>
            rtl: <?= $rtl ?>
        });

        const owlTimer = setInterval(function() {
            if(owlGallery.height() > 1) clearInterval(owlTimer);
            owlGallery.trigger('refresh.owl.carousel', [100]);
        }, 300);

        $('#vertical-thumbnail').slick({
            dots: false,
            arrows: true,
            vertical: true,
            slidesToShow: 5,
            slidesToScroll: 5,
            verticalSwiping: true,
            centerMode: true,
            prevArrow: '<span class="icon-angle-up"><i class="pe-7s-angle-up"></i></span>',
            nextArrow: '<span class="icon-angle-down"><i class="pe-7s-angle-down"></i></span>',
            responsive: [
                {
                    breakpoint: 1199,
                    settings: {
                        slidesToShow: 2,
                        slidesToScroll: 2
                    }
                },
                {
                    breakpoint: 768,
                    settings: {
                        slidesToShow: 3,
                        slidesToScroll: 3
                    }
                },
                {
                    breakpoint: 600,
                    settings: {
                        slidesToShow: 2,
                        slidesToScroll: 2
                    }
                },
                {
                    breakpoint: 360,
                    settings: {
                        slidesToShow: 1,
                        slidesToScroll: 1
                    }
                }
            ]
        });

        $('#owl-carousel-gallery').on('changed.owl.carousel', function(event) {
            var index = event.item.index;
            $('#vertical-thumbnail .item-thumb').removeClass('active');
            $('#vertical-thumbnail .item-thumb[data-owl='+index+']').addClass('active');
            var wdw = $(window).width();
            var ci = <?= count($galleryImages); ?>;
            if(wdw >= 1199 && ci > 3) {
                $('#vertical-thumbnail').slick('slickGoTo', index);
            }else if(wdw < 1199 && wdw >= 768 && ci > 2){
                $('#vertical-thumbnail').slick('slickGoTo', index);
            }else if(wdw < 768 && wdw >= 600 && ci > 3){
                $('#vertical-thumbnail').slick('slickGoTo', index);
            }else if(wdw < 768 && wdw >= 600 && ci > 2){
                $('#vertical-thumbnail').slick('slickGoTo', index);
            }else if(wdw < 360){
                $('#vertical-thumbnail').slick('slickGoTo', index);
            }
        });

        $('#vertical-thumbnail .item-thumb').on('click', function() {
            $('#vertical-thumbnail .item-thumb').removeClass('active');
            const position = $(this).attr('data-owl');
            $('#owl-carousel-gallery').trigger('to.owl.carousel', position);
            $(this).addClass('active');
        });
    });

    <?php if ($zoom_image): ?>
        require([ 'jquery', 'zoom-images' ], function($) {
            $(".imgzoom").each(function( index ) {
                zoomElement(this);
            });
        });
    <?php endif; ?>

    <?php if ($lbox_image): ?>
        require([ 'jquery', 'magnificPopup' ], function($) {
            $('.product.media').magnificPopup({
                delegate: '.imgzoom .lb',
                type: 'image',
                tLoading: 'Loading image #%curr%...',
                mainClass: 'mfp-img-gallery',
                fixedContentPos: true,
                gallery: {
                    enabled: true,
                    navigateByImgClick: true,
                    preload: [0,1]
                },
                iframe: {
                    markup: '<div class="mfp-iframe-scaler"><div class="mfp-close"></div><iframe class="mfp-iframe" frameborder="0" allowfullscreen></iframe><div class="mfp-bottom-bar"><div class="mfp-title"></div><div class="mfp-counter"></div></div></div>'
                },
                image: {
                    tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
                },
                callbacks: {
                    elementParse: function (item) {
                        if (item.el[0].className == 'lb video-link') {
                            item.type = 'iframe';
                        } else {
                            item.type = 'image';
                        }
                    }
                }
            });
        });
    <?php endif; ?>
</script>
