var playerEmbed = {
    prontus_id: '',
    hostUrl: '',
    src : '',
    config : '',
    // Config predeterminada

    init: function () {
        var url = window.location.href;
        var splitted = url.split('?');
        if (typeof splitted[1] === 'undefined') {
            return;
        }
        var args = splitted[1].split(/&/);
        var temp;
        for (var i = 0; i < args.length; i++) {
            temp = args[i].split(/=/);
            if (temp[0] == "src") {
                playerEmbed.src = temp[1];
            }
        }
        if (playerEmbed.src == "") {
            console.error('No se puede determinar src');
            return;
        }
        temp = playerEmbed.src.split('/');
        if (temp.length > 1) {
            playerEmbed.prontus_id = temp[1];
        }
        temp = window.location.href.split('/');
        if (temp.length > 2) {
            playerEmbed.hostUrl = temp[0] + '//' + temp[2];
        }

        Utils.ajaxRequest({
            url: playerEmbed.hostUrl + '/' + playerEmbed.prontus_id + '/site/js/playerConf.json',
            success: function (response) {
                playerEmbed.config = JSON.parse(response);
                playerEmbed.startPlayer();
            },
            error: function () {
                console.error('Ocurrió un error al cargar la configuracion adicional.');
                playerEmbed.startPlayer();
            }
        });
    },

    startPlayer: function () {
        var json_data = playerEmbed.src.replace('.mp4', '.json');
        Utils.ajaxRequest({
            url: playerEmbed.hostUrl + json_data,
            success: function (response) {
                var data = JSON.parse(response);
                if (typeof data != 'undefined' && typeof data['qualities'] != 'undefined' && typeof data['qualities']['1']['file'] !== 'undefined') {
                    playerEmbed.install(data, playerEmbed.src);
                } else {
                    playerEmbed.install({}, playerEmbed.src);
                }
            },
            error: function () {
                console.error('Ocurrió un error al cargar.');
            }
        });
    },

    install: function (metadata, src) {
        var config = new Object();
        config.player = new Object();
        config.player.autoPlay = false;
        if (typeof metadata.poster !== 'undefined' && metadata.poster !== '' ) {
            config.player.image = playerEmbed.hostUrl + metadata.poster;
        }

        if (typeof metadata.thumbnails != 'undefined') {
            config.player.thumbnails = playerEmbed.hostUrl + metadata.thumbnails;
        }

        var quality_counter = 0;
        if (typeof metadata.qualities != 'undefined') {
            quality_counter = Object.keys(metadata.qualities).length;
        }

        config.mediaSrc = new Object();
        if (typeof metadata.hls !== 'undefined' && metadata.hls !== '' ) {
            config.mediaSrc.defaultSrc = playerEmbed.hostUrl + metadata.hls;
        } else if (quality_counter > 1) {
            config.qualities = new Object();
            var labels = ["HD", "SD", "LQ"];
            var list = new Array();

            for (var i = 1; i <= quality_counter; i++) {
                list.push({
                    "label": labels[i-1],
                    "src": playerEmbed.hostUrl + metadata['qualities'][i]['file']
                });
            }

            config.qualities.list = list;
            config.qualities.defaultQ = labels[0];
        } else if (typeof metadata['qualities'] != 'undefined') {
            config.mediaSrc.defaultSrc = playerEmbed.hostUrl + metadata['qualities']['1']['file'];
        } else {
            config.mediaSrc.defaultSrc = playerEmbed.hostUrl + src;
        }
        if (typeof playerEmbed.config.adEnable !== 'undefined' && playerEmbed.config.adEnable == 1 &&
           (typeof metadata.chk_ad_disable === 'undefined' || (typeof metadata.chk_ad_disable !== 'undefined' && metadata.chk_ad_disable != 1))) {
            config.player.adEnable = true;
            config.ads = new Object();
            if (typeof playerEmbed.config.adTag !== 'undefined' && playerEmbed.config.adTag !== '' ) {
                config.ads.adVideoTag = playerEmbed.config.adTag;
            }

            if (typeof metadata.ad_tag !== 'undefined' && metadata.ad_tag != null && metadata.ad_tag !== '' ) {
                config.ads.adVideoTag = metadata.ad_tag;
            }

            if (typeof playerEmbed.config.adInfoMsg !== 'undefined' && playerEmbed.config.adInfoMsg !== '' ) {
                config.ads.adInfoMsg = playerEmbed.config.adInfoMsg;
            }
            if (typeof playerEmbed.config.adBannerInterval !== 'undefined' && playerEmbed.config.adBannerInterval !== '' ) {
                config.ads.adBannerInterval = playerEmbed.config.adBannerInterval;
            }
            if (typeof playerEmbed.config.overlayDelay !== 'undefined' && playerEmbed.config.overlayDelay !== '' ) {
                config.ads.overlayDelay = playerEmbed.config.overlayDelay;
            }
            if (typeof playerEmbed.config.overlayAutoHideDelay !== 'undefined' && playerEmbed.config.overlayAutoHideDelay !== '' ) {
                config.ads.overlayAutoHideDelay = playerEmbed.config.overlayAutoHideDelay;
            }
        }

        if (typeof metadata.subtitle != 'undefined') {
            config.subtitle = new Object();
            config.subtitle = {
                caption: 'subtitles',
                lang: 'es',
                label: 'Subtítulo',
                src: metadata.subtitle
            };
        }

        prontusPlayer.installNow(config);
    }
};
