"use strict";
const EXPORTED_SYMBOLS = ["core", "resources"];
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
const resources = {
browser: {
styles: ["/native/fx/bindings.css"]}};
const WIDGET_NAME = "http://bar.yandex.ru/packages/yandexbar#music";
const core = {
init: function MusicWidget_init(api) {
this._api = api;
this._loadModules();
this.yauth.addListener(this.yauth.EVENTS.AUTH_STATE_CHANGED,this);
}
,
finalize: function MusicWidget_finalize() {
this._api = null;
this.yauth.removeListener(this.yauth.EVENTS.AUTH_STATE_CHANGED,this);
}
,
buildWidget: function MusicWidget_buildWidget(WIID, item) {
item.setAttribute("yb-native-widget-name",WIDGET_NAME);
item.setAttribute("yb-native-widget-wiid",WIID);
item.module = this;
}
,
destroyWidget: function MusicWidget_destroyWidget(WIID, item, context) {

}
,
onNoMoreInstProjections: function MusicWidget_onNoMoreInstProjections(WIID) {
this._destroySlice();
}
,
observe: function MusicWidget_observe(aSubject, aType, aData) {
if (! this._slice)
return;
switch (aType) {
case this.yauth.EVENTS.AUTH_STATE_CHANGED:
this._api.logger.debug("[Music.Widget] " + this.yauth.EVENTS.AUTH_STATE_CHANGED + " isAuth " + this.yauth.authorized);
let isAuth = this.yauth.authorized;
let message = isAuth ? "user.login" : "user.logout";
let data = {
message: message,
data: null};
if (isAuth)
{
this._api.logger.debug("[Music.Widget] isAuth " + this.yauth.user.displayName + " sessionId " + this.yauth.getYandexCookie("Session_id",false));
data.data = this._getCurrentUserData();
}

this._notifySlice(data);
break;
default:
break;
}

}
,
get API() this._api,
get yauth() this.dauth.yauth,
get slice() this._slice || this._createSlice(),
get isPlaying() this._isPlaying,
play: function MusicWidget_play() {
this._notifySlice({
message: "play"});
}
,
pause: function MusicWidget_pause() {
this._notifySlice({
message: "stop"});
}
,
showSlice: function MusicWidget_showSlice(buttonElement) {
if (! this.isFlashEnabled)
{
this._api.Controls.createSlice({
url: this._api.Package.resolvePath("/native/fx/music/slice/noflash/music-noflash.html"),
disposable: true}).show(buttonElement);
return;
}

buttonElement.checked = true;
this.slice.show(buttonElement,(function onHide() {
buttonElement.checked = false;
this.API.Controls.getAllWidgetItems().forEach(function (item) {
item.refreshPlayingState(true);
}
);
}
).bind(this));
}
,
_createSlice: function MusicWidget__createSlice() {
if (! this._slice)
{
this._slice = this._api.Controls.createSlice({
url: "http://yandex.st/elements/slice/music.html",
messageHandler: this._messageHandler});
}

return this._slice;
}
,
_getCurrentUserData: function MusicWidget__getCurrentUserData() {
return {
userName: this.yauth.user.displayName,
sessionId: this.yauth.getYandexCookie("Session_id",false),
__exposedProps__: {
userName: "r",
sessionId: "r"}};
}
,
_destroySlice: function MusicWidget__destroySlice() {
if (! this._slice)
return;
this._slice.destroy();
this._slice = null;
}
,
_notifySlice: function MusicWidget__notifySlice(data) {
if (! this._slice)
return;
if (! ("__exposedProps__" in data))
{
data.__exposedProps__ = {
};
Object.keys(data).forEach(function (key) data.__exposedProps__[key] = "r");
}

this._slice.notify(data);
}
,
_messageHandler: function MusicWidget__messageHandler(messageData) {
switch (messageData.message) {
case "ready":
{
let data = this.yauth.authorized ? this._getCurrentUserData() : {
};
this._notifySlice({
message: "init",
data: data});
break;
}

case "set.play.state":
{
this._isPlaying = messageData.data.isPlaying === true;
this.API.Controls.getAllWidgetItems().forEach(function (item) {
item.refreshPlayingState(false);
}
);
break;
}

case "stop":
{
this._isPlaying = false;
this.API.Controls.getAllWidgetItems().forEach(function (item) {
item.stop();
}
);
break;
}

case "auth.dialog":
{
let topBrowser = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser");
if (! topBrowser)
return;
let widget = topBrowser.document.querySelector("[yb-native-widget-name='" + WIDGET_NAME + "']") || this.API.Controls.getAllWidgetItems()[0];
if (! widget)
return;
let dialogParams = {
name: "music",
title: widget.getAttribute("title")};
let onCloseCallback = (function onCloseCallback() {
this.slice.show(widget,function onHide() widget.checked = false);
}
).bind(this);
this.dauth.openAuthDialog({
target: widget},dialogParams,false,onCloseCallback);
break;
}

case "auth.logout":
{
let user = this.yauth.user;
if (user)
this.yauth.initLogoutProcess(user.name,false);
break;
}

}

}
,
get isFlashEnabled() {
const versionComparator = Cc["@mozilla.org/xpcom/version-comparator;1"].getService(Ci.nsIVersionComparator);
const ph = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
var enabled = ph.getPluginTags({
}).some(function (p) {
if (p.name !== "Shockwave Flash" || p.disabled)
return false;
var version = (p.version || p.description.replace(p.name,"")).replace(/,/g,".");
return versionComparator.compare(version,"11") >= 0;
}
);
if (enabled)
{
delete this.isFlashEnabled;
this.isFlashEnabled = true;
}

return enabled;
}
,
_MODULES: {
dauth: "yauth.jsm",
dlgman: "dlgman.jsm"},
_loadModules: function MusicWidget__loadModules() {
var shAPI = this._api.shareableAPI;
for each(let [moduleName, moduleFileName] in Iterator(this._MODULES)) {
Cu.import(this._api.Package.resolvePath("/native/fx/modules/" + moduleFileName),this);
let module = this[moduleName];
if (typeof module.init == "function")
module.init(shAPI);
}

}
,
_api: null,
_slice: null,
_noFlashSlice: null,
_isPlaying: false};
