"use-strict";

var ua = require("resip_ua");
var logger = require("logger");
var async = require("async");
var system = require("system");
var assert = require("assert");
var accounts = require("pbx_accounts");

var sipStack = new ua.SipStack();
var dum = new ua.DialogUsageManager(sipStack);
var mwi = new ua.ClientSubscriptionHandler();
var dialog = new ua.ClientSubscriptionHandler();
var profile = new ua.MasterProfile();

const SUBSCRIBE_DURATION = 3600;

profile.setUserAgent("ms_teams event monitor");
dum.setMasterProfile(profile);
dum.addClientSubscriptionHandler("message-summary", mwi);
dum.addClientSubscriptionHandler("dialog", dialog);

var event_publish = function(type, event)
{
    var profile = accounts.get_profile_by_aor(event.key);
    if (profile && ("event_handler" in profile) && (type in profile.event_handler)) {
        var publisher = profile.event_handler[type];
        if (publisher) {
            publisher(JSON.stringify(event));
        }
    }
}
var mwi_publish = function(event)
{
    event_publish("message-summary", event);
}

var dialog_publish = function(event)
{
    event_publish("message-summary", event);
}

var monitor = function (aor, event, event_handler)
{
    var uri = "sip:" + aor;
    var profile = accounts.get_profile_by_aor(aor);
    if (!profile) {
        return false;
    }
    if (event_handler) {
        profile.event_handler[event] = event_handler;
    }
    dum.sendClientSubscription(profile, uri, event, SUBSCRIBE_DURATION, SUBSCRIBE_DURATION - 10);
    return true;
}


mwi.onUpdatePending(function(event) {
    event.trigger = "onUpdatePending";
    mwi_publish(event);
    logger.log_info("mwi::onUpdatePending-->" + JSON.stringify(event));
});

mwi.onUpdateActive(function(event){
    event.trigger = "onUpdateActive";
    mwi_publish(event);
    logger.log_info("mwi::onUpdateActive-->" + JSON.stringify(event));
});

mwi.onNewSubscription(function(event){
    event.trigger = "onNewSubscription";
    logger.log_info("mwi::onNewSubscription-->" + JSON.stringify(event));
});

mwi.onNotifyNotReceived(function(event){
    event.trigger = "onNotifyNotReceived";
    logger.log_info("mwi::onNotifyNotReceived-->" + JSON.stringify(event));
});

mwi.onTerminated(function(event){
    event.trigger = "onTerminated";
    logger.log_info("mwi::onTerminated-->" + JSON.stringify(event));
    async.setTimeout(monitor, 5000, [event.key, "message-summary"]);
});

dialog.onUpdatePending(function(event){
    event.trigger = "onUpdatePending";
    dialog_publish(JSON.stringify(event));
    logger.log_info("dialog::onUpdatePending-->" + JSON.stringify(event));
});

dialog.onUpdateActive(function(event){
    event.trigger = "onUpdateActive";
    dialog_publish(JSON.stringify(event));
    logger.log_info("dialog::onUpdateActive-->" + JSON.stringify(event));
});

dialog.onNewSubscription(function(event){
    event.trigger = "onNewSubscription";
    logger.log_info("dialog::onNewSubscription-->" + JSON.stringify(event));
});

dialog.onNotifyNotReceived(function(event){
    event.trigger = "onNotifyNotReceived";
    logger.log_info("dialog::onNotifyNotReceived-->" + JSON.stringify(event));
});

dialog.onTerminated(function(event){
    event.trigger = "onTerminated";
    logger.log_info("dialog::onTerminated-->" + JSON.stringify(event));
    async.setTimeout(monitor, 5000, [event.key, "dialog"]);
});


exports.stack = sipStack;
exports.dum = dum;
exports.mwi = mwi;
exports.dialog = dialog;
exports.profile = profile;

exports.addTransport = function(port)
{
    sipStack.addTransport(ua.UDP, port);
    sipStack.addTransport(ua.TCP, port);
}

exports.overrideContact = function(ip, port)
{
    dum.overrideContact(ip, port);
}

exports.run = function()
{
    sipStack.run();
    dum.run();
}

exports.monitor = monitor;



