Mediasite.namespace("Player");
Mediasite.namespace("Player.Conviva");


Mediasite.Player.ConvivaConfiguration = (function () {
    function ConvivaConfigurationFactory() {
        var self = this;

        self.getPartialConvivaConfiguration = function (playerContext) {

            if (playerContext.GlobalOptions.EnableConviva) {
                return {
                    isEnabled: playerContext.GlobalOptions.EnableConviva,
                    serviceUrl: playerContext.GlobalOptions.ConvivaServiceUrl,
                    customerId: playerContext.GlobalOptions.ConvivaCustomerId,
                    title: playerContext.CurrentPresentation.Title,
                    isLive: playerContext.CurrentPresentation.IsLivePresentation(),
                    presentationId: playerContext.CurrentPresentation.PresentationId,
                    playerType: playerContext.PlayerLayoutOptions.PlayerType,
                    duration: playerContext.CurrentPresentation.Duration / 1000,
                    recordedDateString: playerContext.CurrentPresentation.Date,
                    numberOfSlides: (function () {
                        var slideStream = undefined;
                        slideStream = playerContext.CurrentPresentation.GetStream(Mediasite.ContentStreamType.Slide);
                        if (slideStream != undefined && slideStream.HasSlides() && !playerContext.CurrentPresentation.IsLivePresentation()) {
                            return slideStream.Slides.length;
                        } else {
                            return 0;
                        }
                    })(),
                    numberOfVideos: (function () {
                        var videoCount = 0;
                        for (var streamCount = 0; streamCount < playerContext.CurrentPresentation.Streams.length; streamCount++) {
                            if (playerContext.CurrentPresentation.Streams[streamCount].HasVideo()) {
                                videoCount++;
                            }
                        }
                        return videoCount;
                    })(),
                    playerId: playerContext.GlobalOptions.PlayerId,
                    layoutId: playerContext.GlobalOptions.LayoutId,
                    siteName: playerContext.GlobalOptions.SiteName,
                    siteVersion: playerContext.GlobalOptions.SiteVersion,
                    siteBuildNumber: playerContext.GlobalOptions.SiteBuildNumber,
                    pluginMimeType: playerContext.pluginMimeType,
                    playerName: playerContext.GlobalOptions.PlayerName,
                    userNameHash: playerContext.GlobalOptions.UserNameHash,
                    multisiteId: playerContext.GlobalOptions.MultisiteId,
                    streamType: "",
                    pluginMimeType: "",
                    streamUrl: ""
                };
            } else {
                return { isEnabled: false };
            }
        }

        self.makeGetConvivaConfiguration = function (partialConfiguration) {
            return function (lateBoundConfig) {
                var extendedConfig = $.extend({}, partialConfiguration);
                extendedConfig.streamType = lateBoundConfig.streamType;
                extendedConfig.streamUrl = lateBoundConfig.streamUrl;
                extendedConfig.pluginMimeType = lateBoundConfig.pluginMimeType;
                extendedConfig.mediaPlayerType = lateBoundConfig.mediaPlayerType;

                return extendedConfig;
            }
        }
    }

    function applyConfigToContentMetadataObject(config, metadataObj) {
    
        var m = metadataObj;

        m.assetName = formatAssetName(config.presentationId, config.title, config.streamType);
        m.streamType = config.isLive ? Conviva.ContentMetadata.StreamType.LIVE : Conviva.ContentMetadata.StreamType.VOD;
        m.applicationName = Mediasite.Player.MediaPlayerType.GetDisplayName(parseInt(config.mediaPlayerType));
        m.viewerId = config.userNameHash;
        m.duration = config.duration;
        m.streamUrl = config.streamUrl;
        m.custom = {
            presentationId: config.presentationId,
            playerType: getPlayerTypeName(config.playerType),
            duration: config.duration + '',
            recordedDate: formatDate(config.recordedDateString),
            site: window.location.host,
            referrer: document.referrer,
            streamProtocol: getStreamProtocol(config.pluginMimeType),
            numberOfSlides: config.numberOfSlides + '',
            numberOfVideos: config.numberOfVideos + '',
            catalogId: getCatalogId(),
            playerVersion: formatVersion(config.siteVersion, config.siteBuildNumber),
            playerId: formatPlayerId(config.playerId, config.playerName),
            siteName: config.siteName,
            streamUrl: config.streamUrl,
            multisiteId: config.multisiteId
        };

        return m;
    };

    function getCatalogId() {
        // TODO: Don't access querystring here.
        return Mediasite.GetQueryParam("collection") || Mediasite.GetQueryParam("catalog");
    }

    function formatAssetName(id, title, streamType) {
        return "[" + id.substring(0, 6) + "...] " + title + " [" + getStreamTypeName(streamType) + "]";
    }

    function formatVersion(version, build) {
        return version + "." + build;
    }

    function getStreamTypeName(contentStreamType) {
        if (contentStreamType == 0) {
            return 'Video 1';
        } else if (contentStreamType == 2) {
            return 'Slide';
        } else if (contentStreamType == 3) {
            return 'Presentation';
        } else if (contentStreamType == 4) {
            return 'Video 2';
        } else if (contentStreamType == 5) {
            return 'Video 3';
        }
    }

    function formatDate(dateString) {
        var date = new Date(dateString);

        return getMonthName(date.getMonth()) + " " + date.getDate() + ", " + date.getFullYear();
    }

    function getMonthName(month) {
        var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

        return monthNames[month];
    }

    function getPlayerTypeName(playerType) {

        if (playerType == Mediasite.Player.PlayerType.None) {
            return 'None';
        }
        if (playerType == Mediasite.Player.PlayerType.NewStandardPlayer) {
            return 'Full Experience';
        }
        if (playerType == Mediasite.Player.PlayerType.PipPlayer) {
            return 'Pip';
        }
        if (playerType == Mediasite.Player.PlayerType.MiniPlayer) {
            return 'Mini';
        }
        if (playerType == Mediasite.Player.PlayerType.StandardPlayer) {
            return 'Standard';
        }
        if (playerType == Mediasite.Player.PlayerType.ScreenReader) {
            return 'Screen Reader';
        }
        if (playerType == Mediasite.Player.PlayerType.ContentOnlyPlayer) {
            return 'Content Only';
        }
        if (playerType == Mediasite.Player.PlayerType.SmartPlayer) {
            return 'Multiview';
        }
    }

    function getStreamProtocol(mimeType) {
        switch (mimeType) {
            case Mediasite.Player.MimeTypes.MP4:
                return "MP4";
            case Mediasite.Player.MimeTypes.SS:
                return "Smooth Streaming";
            case Mediasite.Player.MimeTypes.M3U:
                return "HLS";
            case Mediasite.Player.MimeTypes.Dash:
                return "DASH";
            default:
                return "";
        }
    }

    function formatPlayerId(playerId, playerName) {
        return "[" + playerId.substring(0, 6) + "...] " + playerName;
    }

    return {
        ConvivaConfigurationFactory: ConvivaConfigurationFactory,
        applyConfigToContentMetadataObject: applyConfigToContentMetadataObject
    };
})();



Mediasite.Player.Conviva.NullConvivaTracker = function () {
    this.configure = function (convivaConfig) { };

    this.handleMediaSourceLoading = function (sourceUrl, mediaElement) { };

    this.handleMediaError = function (message, isFatal) { };

    this.handleMediaEnded = function (isLive) { };

    this.handleBitrateChange = function (bitrateBps) { };
};


Mediasite.Player.Conviva.createConvivaTracker = function () {
    var constructor = Mediasite.Player.Conviva.ConvivaTracker || Mediasite.Player.Conviva.NullConvivaTracker;
    return new constructor();
};