(function () {

    var extensionName = 'Mediasite.Player.SurveyExtension';

    function SurveyExtension(mediasiteAPI, dbExtension, ariaMessageFactory) {
        if (!dbExtension.Data) return;

        var _eventBundle = new Mediasite.Player.EventBundle();
        var surveyData = JSON.parse(dbExtension.Data);
        var surveyTime = 0;
        var surveyOpened = false;
        var surveyExtensionElement;
        var userClosed = false;

        this.addHandler = function (event, handler) {
            _eventBundle.addHandler(event, handler);
        };

        loadCSS();

        ariaMessageFactory.watchMediasitePlayerSurveyExtension(this);

        function addAPIEventHandlers() {

            var liveStatus = mediasiteAPI.getLiveStatus();
            var isLive = (liveStatus != Mediasite.Player.PresentationStatus.OnDemand && liveStatus != Mediasite.Player.PresentationStatus.NotAvailable);

            if (isLive) {
                mediasiteAPI.addHandler('playerstatechanged', function (eventData) {
                    if (eventData.state == "Ended" && !surveyOpened) {
                        showUI();
                    }
                });
            } else {
                mediasiteAPI.addHandler('currenttimechanged', function (eventData) {

                    if (surveyTime == 0 || isNaN(surveyTime)) {
                        surveyTime = mediasiteAPI.getDuration() - 15;
                    }

                    if (userClosed && eventData.currentTime < surveyTime) {
                        surveyOpened = false;
                        userClosed = false;
                        destroyUI();
                    };

                    if (eventData.currentTime >= surveyTime && !surveyOpened) {
                        showUI();
                    }
                });
            }

            function showUI() {
                surveyOpened = true;
                createUI();
                surveyExtensionElement.fadeIn();
            }
        }

        function createUI() {

            surveyExtensionElement = $('<div class="SurveyExtension"></div>');
            surveyExtensionElement.hide();
            surveyExtensionElement.appendTo('body');

            var surveyExtensionMessageElement = $('<div class="SurveyExtensionMessage" tabindex="0" role="link" aria-label="' + surveyData.Message + '"><div aria-hidden="true">' + surveyData.Message + '</div></div>');
            surveyExtensionMessageElement.appendTo(surveyExtensionElement);

            var closeButton = $('<div class="SurveyExtensionClose" tabindex="0" role="button" aria-label="Close post presentation link"></div>');
            closeButton.appendTo(surveyExtensionElement);

            closeButton.click(function (event) {
                event.stopPropagation();
                userClosed = true;
                destroyUI();
            });

            closeButton.keyup(function (e) {
                if (e.which == 13 || e.which == 32) {
                    e.stopPropagation();
                    userClosed = true;
                    destroyUI();
                }
            });

            surveyExtensionElement.click(function () {
                surveyExtensionElement.fadeOut(function () {
                    window.open(surveyData.URL, new Date(), 'height=768, width=1024, scrollbars=yes, resizable=yes');
                    destroyUI();
                });
            });

            surveyExtensionMessageElement.click(function () {
                surveyExtensionElement.fadeOut(function () {
                    window.open(surveyData.URL, new Date(), 'height=768, width=1024, scrollbars=yes, resizable=yes');
                    destroyUI();
                });
            });

            surveyExtensionMessageElement.keyup(function (e) {
                if (e.which == 13 || e.which == 32) {
                    e.stopPropagation();
                    surveyExtensionElement.fadeOut(function () {
                        window.open(surveyData.URL, new Date(), 'height=768, width=1024, scrollbars=yes, resizable=yes');
                        destroyUI();
                    });
                }
            });

            repositionMessage();

            $(window).resize(repositionMessage);

            _eventBundle.fire("SurveyExtensionMessage", surveyData.Message);
        }

        function destroyUI() {
            if (surveyExtensionElement && surveyExtensionElement.length > 0) {
                surveyExtensionElement.remove();
            }
        }

        function loadCSS() {
            var stylesheet = dbExtension.ExtensionBasePath + "Extension.css";
            Mediasite.Css.LoadStyleSheets([stylesheet], addAPIEventHandlers);
        }

        function repositionMessage() {
            var top = (($(window).height() - surveyExtensionElement.outerHeight(true)) / 2) + 'px';
            var left = (($(window).width() - surveyExtensionElement.outerWidth(true)) / 2) + 'px';
            surveyExtensionElement.css({ top: top, left: left });
        }
    }

    Mediasite.Player.RegisterExtension(extensionName,
    {
        Constructor: SurveyExtension
    });


})();