define(["events","globalize","playbackManager","connectionManager","playMethodHelper","layoutManager","serverNotifications","paper-icon-button-light","css!./playerstats"],function(events,globalize,playbackManager,connectionManager,playMethodHelper,layoutManager,serverNotifications){"use strict";function init(instance){var parent=document.createElement("div");parent.classList.add("playerStats"),layoutManager.tv&&parent.classList.add("playerStats-tv"),parent.classList.add("hide");var button;button=layoutManager.tv?"":'';var contentClass=layoutManager.tv?"playerStats-content playerStats-content-tv":"playerStats-content";parent.innerHTML='
',button=parent.querySelector(".playerStats-closeButton"),button&&button.addEventListener("click",onCloseButtonClick.bind(instance)),document.body.appendChild(parent),instance.element=parent}function onCloseButtonClick(){this.enabled(!1)}function renderStats(elem,categories){elem.querySelector(".playerStats-stats").innerHTML=categories.map(function(category){var categoryHtml="",stats=category.stats;stats.length&&category.name&&(categoryHtml+='");for(var i=0,length=stats.length;i';var stat=stats[i];categoryHtml+='',categoryHtml+=stat.label,categoryHtml+="
",categoryHtml+='',categoryHtml+=stat.value,categoryHtml+="
",categoryHtml+=""}return categoryHtml}).join("")}function getSession(instance,player){var now=(new Date).getTime();if(now-(instance.lastSessionTime||0)<1e4)return Promise.resolve(instance.lastSession);var apiClient=connectionManager.getApiClient(playbackManager.currentItem(player).ServerId);return apiClient.getSessions({deviceId:apiClient.deviceId()}).then(function(sessions){return instance.lastSession=sessions[0]||{},instance.lastSessionTime=(new Date).getTime(),Promise.resolve(instance.lastSession)},function(){return Promise.resolve({})})}function translateReason(reason){return globalize.translate("sharedcomponents#"+reason)}function getTranscodingStats(session,player,displayPlayMethod){var videoCodec,audioCodec,totalBitrate,audioChannels,sessionStats=[];return session.TranscodingInfo&&(videoCodec=session.TranscodingInfo.VideoCodec,audioCodec=session.TranscodingInfo.AudioCodec,totalBitrate=session.TranscodingInfo.Bitrate,audioChannels=session.TranscodingInfo.AudioChannels),"Transcode"===displayPlayMethod&&(totalBitrate&&sessionStats.push({label:"Bitrate:",value:getDisplayBitrate(totalBitrate)}),session.TranscodingInfo.CompletionPercentage&&sessionStats.push({label:"Transcoding progress:",value:session.TranscodingInfo.CompletionPercentage.toFixed(1)+"%"}),session.TranscodingInfo.Framerate&&sessionStats.push({label:"Transcoding framerate:",value:session.TranscodingInfo.Framerate+" fps"}),session.TranscodingInfo.TranscodeReasons&&session.TranscodingInfo.TranscodeReasons.length&&sessionStats.push({label:"Reason for transcoding:",value:session.TranscodingInfo.TranscodeReasons.map(translateReason).join("
")})),sessionStats}function getDisplayBitrate(bitrate){return bitrate>1e6?(bitrate/1e6).toFixed(1)+" Mbps":Math.floor(bitrate/1e3)+" kbps"}function getStats(instance,player){var statsPromise=player.getStats?player.getStats():Promise.resolve([]),sessionPromise=getSession(instance,player);return Promise.all([statsPromise,sessionPromise]).then(function(responses){var playerStats=responses[0],session=responses[1],displayPlayMethod=playMethodHelper.getDisplayPlayMethod(session),baseCategory={stats:[],name:"Playback Info"};baseCategory.stats.unshift({label:"Play method:",value:displayPlayMethod}),baseCategory.stats.unshift({label:"Player:",value:player.name});var categories=[];categories.push(baseCategory);for(var i=0,length=playerStats.length;i