$(document).ready(() => {
    startTranslation().then(() => {
        initMain().then(function () {
            initIndex()
            loadUserModal()
        });
    })    
})

function initIndex() {
    importCssFile('assets/css/index.css');
    initHeader();
    getLoginInfo()
        .then(
            function (_) {
                if (onlyVods) {
                    window.location.href = '/vod'
                    return
                }
                initIndexData();
                $("body").show();
            }

        )};

function loadUserModal() {
    $("#modalContainer").load("../../user-modal.html");
};

function initIndexData() {
    initRenewToken();
    initLeftMenu()
    initRightMenu();
    initStillWatchingModal();
    getChannels()
    .then(
        function (data) {
            initIndexPage(data.channels, data.featuredChannel);
            initFooter();
        }
    ).catch(
        function () {
            showErrorPage("index-main-elem");
        }
    );
}

function initIndexPage(channels, featuredChannel) {
    fillIndexPageTemplate(channels, featuredChannel);
    let options = {
        "fixed": false,
        "hideMediaControl": false,
        "mediaControlRemove": [
            "seekbar",
            "playpause",
            "position",
            "duration",
            "fullscreen",
            "playstop",
            "live",
            "volume",
            "hd-indicator"
        ],
        "hideErrors": true,
        "trailerMode": true,
        "poster": featuredChannel.poster,
        "aonly" : typeof featuredChannel.aonly == 'undefined' ? false : featuredChannel.aonly,
        "mute": false,
    };
    initPlayer(featuredChannel, "player", options);
}

function fillIndexPageTemplate(channels, featuredChannel) {
    let template;
    template =
    '<div class="index-player">' +
    '<div class="index-player-fill"></div>' +
    '<div class="index-player-container" id="player"></div>' +
    '</div>' +
    '<div class="index-content" id="index-content">' +
    '<div class="index-player-attachments">' +
    '<div class="index-player-info">' +
    '<div class="index-player-info-logo">' +
        '<img id="index-player-info-logo-img">' +
        '</div>' +

        '<div class="index-player-info-guide">' +
        '<p id="index-player-info-guide-now-label"></p>' +
        '<h1 id="index-player-info-guide-now-text"></h1>' +
        '</div>' +
        '</div>' +
        '<div class="index-player-actions">' +
        '<a href="theater?id=' + featuredChannel.id + '"><i class="fas fa-play"></i><span>'+ i18nextInstance.t('assistir') +'</span></a>' +
        '</div>' +
        '<div class="index-player-more">' +
        '<a href="#all-channels"><i class="fas fa-chevron-down"></i><span>'+ i18nextInstance.t('ver_mais') +'</span></a>' +
        '</div>' +
        '</div>' +
        '</div>';
        $('#index-main-elem').html(template);
        $('#index-player-info-logo-img').attr("src", featuredChannel.logo);

    getChannelGuide(featuredChannel)
    .then(
        function (guide) {
            $('#index-player-info-guide-now-label').text(i18nextInstance.t('agora'));
            $('#index-player-info-guide-now-text').text(guide[0].title);
            }
        ).catch(
        function () {
            $('#index-player-info-guide-now-label').text(i18nextInstance.t('agora'));
            $('#index-player-info-guide-now-text').text(i18nextInstance.t('programacao') + " " + featuredChannel.name);
        }
        );
        initChannelsList(channels);
}
function endTrailerMode(options) {
    getPlayer().configure({
        poster: "",
        chromeless: true,
        allowUserInteraction: false,
    });

    setTimeout(function () {
        getPlayer().configure({
            poster: options.poster,
            chromeless: true,
            allowUserInteraction: false,
        });

        addCssRule(".media-control[data-media-control].media-control-hide", "display: block !important;");
        addCssRule(".media-control[data-media-control].media-control-hide .media-control-background[data-background]", "opacity: 1 !important;");

        $(".index-player-actions a").css("font-size", "calc(10px + 1.5vw)");
        $(".index-player-actions a span").css("font-size", "calc(10px + 1.75vw)");
        $(".index-player-actions a span").css("margin-left", "calc(5px + .75vw)");
    }, 2000);
}

function initChannelsList(channels) {
    let options = {
        "title": i18nextInstance.t('canais_ao_vivo'),
        "parentId": "index-content",
        "elementId": "all-channels",
    };
    initChannelsCarousel(channels, options);
}