﻿$(document).ready(function () {

    var hotsite = CookieService.ReadCookie('Hotsite') ? JSON.parse(CookieService.ReadCookie('Hotsite')) : undefined;
    var eventId = undefined;

    verifyHotsite();
    loadHotsite();

    function loadHotsite() {

        $.ajax({
            type: 'GET',
            dataType: 'json',
            crossDomain: true,
            data: hotsite,
            url: App.baseServicesUrl + 'Hotsites/GetVideoSectionForViewCache',
            success: function (data) {

                setTimeout(function () {
                    $('.video-link').addClass('active');
                }, 500);

                var result = data.result;

                if (result) {

                    if (!result.isPublished)
                        goToNotPublishedPage();

                    if (result.target != undefined) {
                        block();
                        window.location.href = result.externalLink;
                    }

                    if (result.name) {
                        $('.name-page').html(result.name);
                    }
                                        
                    loadSponsors();

                    if (!result.videos || !result.videos.length) {
                        $('#container-videos').remove();
                    }
                    else {

                        var cardsHtml = '';
                        var externalCardsHtml = '';

                        eventId = result.eventId;

                        result.videos.forEach(x => {

                            if (x.externalLink) {
                                externalCardsHtml = externalCardsHtml.concat(
                                    '<div class= "col-lg-6 text-center wow move-up content-video" data-src="' + x.url + '" data-poster="data:image/jpeg; base64,' + x.thumbnail + '" data-sub-html="<h4>' + x.title + '</h4><p>' + x.subtitle + '</p>">',
                                    '<a href="' + x.url + '" class="video-link mt-30">',
                                    '<div class="single-popup-wrap">',
                                    '<img class="img-fluid" src="data:image/jpeg; base64,' + x.thumbnail + '">',
                                    '<div class="ht-popup-video video-button">',
                                    '<div class="video-mark">',
                                    '<div class="wave-pulse wave-pulse-1"></div>',
                                    '<div class="wave-pulse wave-pulse-2"></div>',
                                    '</div>',
                                    '<div class="video-button__two">',
                                    '<div class="video-play">',
                                    '<span class="video-play-icon"></span>',
                                    '</div>',
                                    '</div>',
                                    '</div>',
                                    '</div>',
                                    '</a>',
                                    '<div class="content"' + (x.title || x.subtitle ? ' style="min-height: 75px; margin-top: 10px;"' : '') + '>',
                                    '<h2 class="title">',
                                    '<strong>' + (x.title ? x.title : '') + '</strong>',
                                    '</h2>',
                                    '<p class="subtitle">' + (x.subtitle ? x.subtitle : '') + '</p>',
                                    '</div>',
                                    '</div>'
                                );
                            }
                            else {
                                cardsHtml = cardsHtml.concat(
                                    '<div class= "col-lg-6 text-center wow move-up content-video" data-src="' + x.url + '" data-poster="data:image/jpeg; base64,' + x.thumbnail + '" data-sub-html="<h4>' + x.title + '</h4><p>' + x.subtitle + '</p>">',
                                    '<a href="' + x.url + '" class="video-link mt-30">',
                                    '<div class="single-popup-wrap">',
                                    '<img class="img-fluid" src="data:image/jpeg; base64,' + x.thumbnail + '">',
                                    '<div class="ht-popup-video video-button">',
                                    '<div class="video-mark">',
                                    '<div class="wave-pulse wave-pulse-1"></div>',
                                    '<div class="wave-pulse wave-pulse-2"></div>',
                                    '</div>',
                                    '<div class="video-button__two">',
                                    '<div class="video-play">',
                                    '<span class="video-play-icon"></span>',
                                    '</div>',
                                    '</div>',
                                    '</div>',
                                    '</div>',
                                    '</a>',
                                    '<div class="content"' + (x.title || x.subtitle ? ' style="min-height: 75px; margin-top: 10px;"' : '') + '>',
                                    '<h2 class="title">',
                                    '<strong>' + (x.title ? x.title : '') + '</strong>',
                                    '</h2>',
                                    '<p class="subtitle">' + (x.subtitle ? x.subtitle : '') + '</p>',
                                    '</div>',
                                    '</div>'
                                );
                            }
                        });

                        $('#card-external-videos').html(externalCardsHtml);
                        $('#card-videos').html(cardsHtml);

                        if (result.text)
                            $('#container-text #text').html(result.text);
                        else
                            $('#container-text').remove();

                        setTimeout(function () {
                            loadUser();
                        }, 300);
                    }
                }
            }
        });

        return false;
    }

    function loadUser() {

        if (LoginServive.IsLogged()) {

            if (LoginServive.ProfileInformations.user.userName == 'admin') {
                activeVideos();
            }
            else {

                $.ajax({
                    type: 'GET',
                    dataType: 'json',
                    crossDomain: true,
                    data: {
                        CPF: LoginServive.ProfileInformations.user.userName,
                        TenantId: hotsite.tenantId,
                        EventId: eventId
                    },
                    url: App.baseServicesUrl + 'Registrations/GetUserForRegistrationByTenantAndLogin',
                    success: function (data) {

                        if (!!data) {

                            var result = data.result;

                            if (result.hasRegistration) {

                                if (result.registrationNotPayied) {
                                    $('#modalInfo .modal-body p').html('É necessário realizar o pagamento para visualizar o vídeo.<br>Caso tenha efetuado o pagamento é só aguardar a confirmação.')
                                    disableVideos();
                                }
                                else if (!result.approvedRegistration) {
                                    $('#modalInfo .modal-body p').html('É necessário aguardar a confirmação da inscrição para visualizar o vídeo.')
                                    disableVideos();
                                }
                                else {
                                    activeVideos();
                                }
                            }
                            else {
                                disableVideos();
                            }
                        }
                        else {
                            disableVideos();
                        }
                    },
                    error: function () {
                        disableVideos();
                    }
                });

                return false;
            }
        }
        else {
            disableVideos();
        }
    }

    function activeVideos() {

        $('#card-external-videos a').attr('target', '_blank');

        setTimeout(function () {
            $('#card-videos').lightGallery({
                share: false,
                zoom: false,
                actualSize: false,
                autoplayControls: false
            });
        }, 0);
    }

    function disableVideos() {

        $('#card-videos a, #card-external-videos a').attr("href", "javascript:void(0)");

        $('#card-videos .content-video, #card-external-videos .content-video').on('click', function () {

            if (LoginServive.IsLogged()) {
                $('#modalInfo').modal('show');
            }
            else {
                $('#modalInfoLogin').modal('show');
                setTimeout(function () {

                    var path = '/login';
                    var params = window.location.pathname.split('/');
                    var pages = ['login', 'alterar-senha', 'confirmar-email'];

                    if (!!params[2] && !pages.some(s => params[2] === s))
                        path += '?eventCode=' + params[1] + '&page=' + params[2];

                    window.location.href = window.location.origin + path;

                }, 3000);
            }
        });
    }
});