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#login";
const core = {
init: function LoginWidget_init(api) {
this._api = api;
this._logger = api.logger;
this._branding = this._api.Services.obtainService("ru.yandex.custombar.branding","package",this);
this._loadModules();
}
,
finalize: function LoginWidget_finalize() {
this._api.Services.releaseService("ru.yandex.custombar.branding","package",this);
this._branding = null;
delete this.utils;
delete this._api;
delete this._logger;
}
,
buildWidget: function LoginWidget_buildWidget(WIID, item) {
item.setAttribute("yb-native-widget-name",WIDGET_NAME);
item.setAttribute("yb-native-widget-wiid",WIID);
item.module = this;
}
,
destroyWidget: function LoginWidget_destroyWidget(WIID, item, context) {
try {
if (typeof item.destroy == "function")
item.destroy();
}
 finally {
item.removeAttribute("yb-native-widget-name");
item.removeAttribute("yb-native-widget-wiid");
}

}
,
get API() {
return this._api;
}
,
get strBundle() {
var bundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
var bundle = bundleService.createBundle(this.API.Package.resolvePath("/native/fx/login.properties"));
delete this.strBundle;
this.__defineGetter__("strBundle",function strBundle() bundle);
return this.strBundle;
}
,
_branding: null,
get branding() {
return this._branding;
}
,
observeServiceEvent: function LoginWidget_observeServiceEvent() {

}
,
getPref: function LoginWidget_getPref(strPrefName, defaultValue) {
var prefFullName = this._api.Settings.getPackageBranchPath() + strPrefName;
var prefsModule = this._api.Settings.PrefsModule;
return prefsModule.get(prefFullName,defaultValue);
}
,
setPref: function LoginWidget_setPref(strPrefName, strPrefValue) {
var prefFullName = this._api.Settings.getPackageBranchPath() + strPrefName;
var prefsModule = this._api.Settings.PrefsModule;
return prefsModule.set(prefFullName,strPrefValue);
}
,
onButtonClick: function LoginWidget_onButtonClick(event, widget) {
var dialogParams = {
name: "bar",
title: widget.title};
this.dauth.openAuthDialog(event,dialogParams,false,null,(function (data) {
if (data.type == "logout")
{
this.logoutAll();
}

}
).bind(this));
}
,
switchUsers: function LoginWidget_switchUsers(event, widget) {
var dialogParams = {
name: "bar",
title: widget.title};
this.dauth.openAuthDialog(event,dialogParams,true,null,(function (data) {
if (data.type == "logout")
{
this.logoutAll();
}

}
).bind(this));
}
,
login: function LoginWidget_login(strUsername, strPwd) {
this.dauth.initLoginProcess(strUsername,strPwd,true);
}
,
logoutAll: function LoginWidget_logoutAll(startCallback) {
const NOPROMPT_PREF_NAME = "logoutWithoutConfirm";
var noPrompt = this.getPref(NOPROMPT_PREF_NAME);
var checker = {
value: false};
if (! noPrompt)
{
let answerBtnIndex = this.utils.showPromptEx(null,this.branding.expandBrandTemplates(this.strBundle.GetStringFromName("logOutTitle")),this.branding.expandBrandTemplates(this.strBundle.GetStringFromName("logOutConfirm")),this.strBundle.GetStringFromName("logOutYesPrompt"),this.strBundle.GetStringFromName("logOutNoPrompt"),this.strBundle.GetStringFromName("logOutNoAlwaysPrompt"),this.strBundle.GetStringFromName("logOutForgetPass"),checker);
switch (answerBtnIndex) {
case 0:
break;
case 1:
return false;
case 2:
this.setPref(NOPROMPT_PREF_NAME,true);
break;
}

}

if (startCallback)
startCallback();
this.dauth.initLogoutAll();
if (checker.value)
this.dauth.pwdmng.forgetAllUsers();
}
,
_MODULES: {
utils: "utils.jsm",
dauth: "yauth.jsm",
dlgman: "dlgman.jsm"},
_buildMenu: function LoginWidget__buildMenu() {
var menuItems = [];
try {
let users = this.dauth.yauth.liveUsers;
let document = this.utils.getCurrentWindow().document;
menuItems = menuItems.concat(this._buildMenuItems(users));
if (users.length)
{
let menuseparator = document.createElement("menuseparator");
menuItems.push(menuseparator);
}

users = this.dauth.mfdauth.liveUsers;
menuItems = menuItems.concat(this._buildMenuItems(users));
if (users.length)
{
let menuseparator = document.createElement("menuseparator");
menuItems.push(menuseparator);
}

}
catch (e) {
this._logger.debug("User canceled master-password request.");
}

return menuItems;
}
,
_buildMenuItems: function LoginWidget__buildMenuItems(users) {
var menuItems = [];
var document = this.utils.getCurrentWindow().document;
for(let [, user] in Iterator(users)) {
let menuitem = document.createElement("menuitem");
menuitem.setAttribute("label",user.displayName);
menuitem.setAttribute("crop","center");
menuItems.push(menuitem);
if (this.dauth.isLoginHasAuth(user.name))
{
menuitem.setAttribute("disabled","true");
}
 else
{
menuitem.setAttribute("oncommand","getBindingParent(this).login('" + user.name + "', '" + user.password + "')");
}

}

return menuItems;
}
,
_loadModules: function LoginWidget__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);
}

}
,
get _xslUsersListDoc() {
try {
let filePath = "/native/fx/login/xsl-login-items.xsl";
let channel = this._api.Package.getFileInputChannel(filePath);
let xslDoc = this._api.XMLUtils.xmlDocFromStream(channel.contentStream,this.utils.newURI(this._api.Package.resolvePath(filePath),null,null),null,true);
delete this._xslUsersListDoc;
this.__defineGetter__("_xslUsersListDoc",function _xslUsersListDoc() xslDoc);
return xslDoc;
}
catch (ex) {
this._api.logger.error(ex);
return null;
}

}
};
