<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE bindings SYSTEM "/vkontakte.dtd"><?xml-stylesheet type="text/css" href="/xb-skin/default/style.css"?>

<bindings xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <binding extends="_vk.button.counter.xbl#button" id="messages">
        <content>
            <xul:toolbarbutton action="open-page" anonid="button" class="chromeclass-toolbar-additional toolbarbutton-1 yb-vkontakte-button" id="yb-vkontakte-messages" label="" page="messages" type="menu-button" vk-type="messages">
                <xul:menupopup anonid="menu" class="yb-vkontakte-message-menu">
                    <xul:menuitem action="update" label="&vkontakte.messages.menu.update;" update="messages"/>
                    
                    <xul:menuseparator anonid="separator"/>
                    <xul:menuitem action="open-page" label="&vkontakte.messages.menu.messages;" page="messages"/>
                </xul:menupopup>
            </xul:toolbarbutton>
        </content>

		<implementation>
            <field name="BUTTON_NAME" readonly="true">'messages'</field>

            <field name="tooltips" readonly="true">
                ({
                    'need_auth': 'vkontakte.messages.tooltip.need_auth',
                    'normal': 'vkontakte.messages.tooltip.normal',
                    'counter': {
                        'form_1': 'vkontakte.messages.tooltip.new.form_1',
                        'form_2': 'vkontakte.messages.tooltip.new.form_2',
                        'form_5': 'vkontakte.messages.tooltip.new.form_5'
                    }
                })
            </field>

            <method name="_customConstructor">
                <body><![CDATA[var $ = this;
$._counterButtonConstructor();
var mailman = this.app.mailman;
mailman.addSubscriber({
subject: "new-messages",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: messages}) {
$.waiting = false;
if ($.updateMessages)
{
$.updateMessages(messages);
$.updateTooltip();
}

}
}});
mailman.send({
subject: "please-repeat",
body: "new-messages"});]]></body>
            </method>

            <method name="updateMessages">
                <parameter name="messages"/>
                <body><![CDATA[this.clearMessages();
this.counter = messages.total;
if (this.counter > 0)
{
this.showMessages(messages.data);
}
]]></body>
            </method>


            <method name="clearMessages">
                <body><![CDATA[var $ = this;
for (let i = $.menu.childNodes.length - 1;i > 0;--i) {
let item = $.menu.childNodes[i];
if (Node.ELEMENT_NODE == item.nodeType && item.hasAttribute("isMessage"))
{
$.menu.removeChild(item);
}

}
]]></body>
            </method>


            <method name="showMessages">
                <parameter name="messages"/>
                <body><![CDATA[var $ = this;
var separator = document.createElement("menuseparator");
separator.setAttribute("isMessage","true");
$.menu.insertBefore(separator,$.separator);
messages.forEach(function (message) {
var item = $.createMessageItem(message);
$.menu.insertBefore(item,$.separator);
}
);]]></body>
            </method>


            <method name="createMessageItem">
                <parameter name="messageData"/>
                <body><![CDATA[var {mid: mid, body: text, _profile: profile} = messageData;
text = text.replace("<br>"," ","g");
var item = document.createElement("menuitem");
var username = profile.first_name + " " + profile.last_name;
var label = this.cutText(username + ": " + text);
item.setAttribute("label",label);
item.setAttribute("action","open-url");
item.setAttribute("url",this.getMessageLinkById(mid));
item.setAttribute("isMessage","true");
return item;]]></body>
            </method>


            <method name="getMessageLinkById">
                <parameter name="id"/>
                <body><![CDATA[return "http://vk.com/mail?act=show&id=" + id;]]></body>
            </method>


            <method name="cutText">
                <parameter name="text"/>
                <body><![CDATA[const MAX_LENGTH = 80;
const CUT_AFTER = 77;
var length = text.length;
if (length <= MAX_LENGTH)
{
return text;
}
else
{
return text.slice(0,CUT_AFTER) + "...";
}
]]></body>
            </method>

		</implementation>
    </binding>

</bindings>
