"use-strict";

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

var profiles = new Object();
var accounts = new Object();

exports.add = function(user, did, domain, auth_user, auth_realm, auth_pass)
{
    var aor = user + "@" + domain;
    var uri = "sip:" + aor;
    var key = "did_" + did;
    accounts[key] = { aor : aor, did : did, auth_user : auth_user, auth_pass : auth_pass, auth_realm : auth_realm };

    var profile = new ua.UserProfile();
    profile.account = accounts[key];
    profile.setDefaultFrom(uri);
    profile.setDigestCredential(aor, auth_user, auth_pass);
    profiles[user] = profile;
    accounts[key].profile = profile;
}

exports.get_account_by_aor = function (aor)
{
  for (var item in accounts) {
    if (typeof accounts[item] === "object" && ("aor" in accounts[item]) && accounts[item].aor == aor) {
      return accounts[item];
    }
  }
  return undefined;
}

exports.get_profile_by_aor = function (aor)
{
  for (var item in profiles) {
    if (typeof profiles[item] === "object" && ("account" in profiles[item]) && profiles[item].account.aor == aor) {
      return profiles[item];
    }
  }
  return undefined;
}