"use strict";
const EXPORTED_SYMBOLS = ["core", "resources"];
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
const resources = {
browser: {
styles: ["/native/fx/bindings.css"]}};
const WIDGET_NAME = "http://bar.yandex.ru/packages/yandexbar#lenta";
const core = {
_strSettingsXUL: "",
Settings: {
getMainTemplate: function LentaWidgetSettings_getMainTemplate(aWidgetUnitName, aWidgetInstanceId) {
if (! this._strSettingsXUL)
{
let prefsXULPath = "native/fx/lenta/preferences.xul";
let prefsScriptPath = "native/fx/preferences/preferences.js";
let xulStream = core._api.Package.getFileInputChannel(prefsXULPath).contentStream;
let strXUL = core._api.StrUtils.readStringFromStream(xulStream);
xulStream.close();
let scriptStream = core._api.Package.getFileInputChannel(prefsScriptPath).contentStream;
let strScript = core._api.StrUtils.readStringFromStream(scriptStream);
scriptStream.close();
this._strSettingsXUL = strXUL.replace("{{SCRIPT}}",strScript);
}

return this._strSettingsXUL;
}
},
init: function LentaWidget_init(api) {
this._api = api;
this._logger = api.logger;
this._loadModules();
if (this.yauth.authorized)
{
this.counters.requireService(this.counterServiceID,this);
}

this.yauth.addListener(this.yauth.EVENTS.AUTH_STATE_CHANGED,this);
}
,
finalize: function LentaWidget_finalize() {
this.yauth.removeListener(this.yauth.EVENTS.AUTH_STATE_CHANGED,this);
this.counters.ignoreService(this.counterServiceID,this);
delete this.utils;
delete this._api;
delete this._logger;
delete this.__stringBundle;
}
,
buildWidget: function LentaWidget_buildWidget(WIID, item) {
item.setAttribute("yb-native-widget-name",WIDGET_NAME);
item.setAttribute("yb-native-widget-wiid",WIID);
item.module = this;
}
,
destroyWidget: function LentaWidget_destroyWidget(WIID, item, context) {
item.removeAttribute("yb-native-widget-name");
item.removeAttribute("yb-native-widget-wiid");
this.dlgman.closeAlertDialog("lenta");
this.dlgman.closeFeedsDialog();
}
,
get API() {
return this._api;
}
,
get SERVICE_URL() {
var url = this._api.Localization.createStringBundle("/urls/lenta.properties").get("LentaURL");
delete this.SERVICE_URL;
return this.SERVICE_URL = url;
}
,
onButtonClick: function LentaWidget_onButtonClick(event, widget) {
if (this.yauth.authorized)
{
this.gotoMessages(event);
}
 else
{
let dialogParams = {
name: "lenta",
title: widget.title};
this.yauth.openAuthDialog(event,dialogParams,false,(function () {
this.gotoMessages(event);
}
).bind(this));
}

}
,
refreshData: function LentaWidget_refreshData(event, callback) {
this.counters.forceUpdate(this.counterServiceID,callback);
}
,
gotoMessages: function LentaWidget_gotoMessages(event) {
this._navToLentaPage("",this._STAT_CODES.GOTO_MESSAGES,event);
}
,
addFeeds: function LentaWidget_addFeeds() {
this.dlgman.openFeedsDialog();
}
,
observe: function LentaWidget_observe(subject, topic, data) {
if (topic == this.yauth.EVENTS.AUTH_STATE_CHANGED)
{
if (data.hasAuth)
{
this.counters.requireService(this.counterServiceID,this);
}
 else
{
this.counters.ignoreService(this.counterServiceID,this);
}

}

}
,
counterServiceID: 44,
counterXpathExpr: "number(/s/c/lenta/@v)",
onCountersChange: function LentaWidget_onCountersChange(flags, responseDoc) {
if (! flags || ! ("ok" in flags))
{
return;
}

var counterValue = this._api.XMLUtils.queryXMLDoc(this.counterXpathExpr,responseDoc);
var nDiff = counterValue - this.yauth.user.selectPrefInt("lenta.prev.counter");
if (nDiff && counterValue)
{
if (this.getPref("showTextAlert",true))
{
let topBrowserWindow = this.dlgman.getTopBrowserWindow();
let isFullScreen = topBrowserWindow && topBrowserWindow.fullScreen;
if (! isFullScreen)
{
let badgeCounter = nDiff > 0 ? nDiff : counterValue;
this.dlgman.openAlertDialog("lenta",this.API,{
url: this.SERVICE_URL,
urlAction: badgeCounter > 1 ? this._STAT_CODES.CLICK_ON_MULTI_ALERT : this._STAT_CODES.CLICK_ON_ALERT,
serviceTitle: this._stringBundle.get("feedsNotificationTitle"),
badgeCounter: badgeCounter,
badgeText: this._stringBundle.getPlural("feedsNotificationMsg",[badgeCounter])});
}

}

let isSoundAlertEnabled = this.API.Environment.barPlatform.soundsEnabled && this.getPref("playSoundAlert",false);
if (isSoundAlertEnabled)
{
let strURI = this.getPref("soundURI");
if (strURI)
{
this.utils.playSound(strURI);
}

}

}

this.yauth.user.savePref("lenta.prev.counter",counterValue || 0);
}
,
__stringBundle: null,
get _stringBundle() {
return this.__stringBundle || (this.__stringBundle = this._api.Localization.createStringBundle("/native/fx/lenta.properties"));
}
,
getPref: function LentaWidget_getPref(strPrefName, defaultValue) {
var prefFullName = this._api.Settings.getComponentBranchPath() + strPrefName;
var prefsModule = this._api.Settings.PrefsModule;
return prefsModule.get(prefFullName,defaultValue);
}
,
setPref: function LentaWidget_setPref(strPrefName, strPrefValue) {
var prefFullName = this._api.Settings.getComponentBranchPath() + strPrefName;
var prefsModule = this._api.Settings.PrefsModule;
return prefsModule.set(prefFullName,strPrefValue);
}
,
_STAT_CODES: {
GOTO_MESSAGES: 1100,
CLICK_ON_ALERT: 11,
CLICK_ON_MULTI_ALERT: 12},
_MODULES: {
utils: "utils.jsm",
counters: "counters.jsm",
yauth: "yauth.jsm",
dlgman: "dlgman.jsm"},
_loadModules: function LentaWidget__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);
}

}
,
_navToLentaPage: function LentaWidget__navToLentaPage(page, statAction, origEvent) {
var lentaURL = this.SERVICE_URL + page;
if (statAction)
this.API.Statistics.logShortAction(statAction);
this.API.Controls.navigateBrowser({
url: lentaURL,
eventInfo: origEvent});
}
};
