﻿

var anzery = {};

anzery.matchServiceProxy = function(matchServiceBaseUri)
{
    this.matchServiceBaseUri = matchServiceBaseUri;
}

anzery.matchServiceProxy.prototype =
{
    getMostPopularArticles: function(category, topCount, daysUntilNow, callback)
    {
        if (typeof category === 'undefined' || category == null)
        {
            category = "";
        }

        this.callService("MostPopularArticles", { category : category, topCount : topCount, daysUntilNow : daysUntilNow }, callback);
    },
    findMatches: function(question, callback, topCount)
    {
        this.assertIsNonEmptyString(question, "question");

        if (typeof topCount === 'number' || (typeof topCount === 'string' && !isNaN(parseInt(topCount))))
        {
            this.callService("FindTopMatches", { question : question, topCount : topCount }, callback);
        }
        else
        {
            this.callService("FindMatches", { question : question }, callback);
        }
    },
    notifyGoodMatch : function(question, knowledgeArticleId, callback)
    {
        this.assertIsNonEmptyString(question, "question");
        this.assertIsIdNumber(knowledgeArticleId, "knowledgeArticleId");
        this.callService("NotifyGoodMatch", { question : question, knowledgeArticleId : knowledgeArticleId }, callback);
    },
    notifyBadMatch : function(question, knowledgeArticleId, callback)
    {
        this.assertIsNonEmptyString(question, "question");
        this.assertIsIdNumber(knowledgeArticleId, "knowledgeArticleId");
        this.callService("NotifyBadMatch", { question : question, knowledgeArticleId : knowledgeArticleId }, callback);
    },
    notifyView : function(question, knowledgeArticleId, callback)
    {      
        this.assertIsNonEmptyString(question, "question");
        this.assertIsIdNumber(knowledgeArticleId, "knowledgeArticleId");
        this.callService("NotifyView", { question : question, knowledgeArticleId : knowledgeArticleId }, callback);
    },
    notifyResolve : function(question, callback)
    {
        this.assertIsNonEmptyString(question, "question");
        this.callService("NotifyResolve", { question : question }, callback);
    },
    notifyResolveFailed : function(question, callback)
    {
        this.assertIsNonEmptyString(question, "question");
        this.callService("NotifyResolveFailed", { question : question }, callback);
    },
    assertIsNonEmptyString : function(paramValue, paramName)
    {
        if (typeof paramValue !== 'string' || paramValue == "")
        {
            var err = new Error();
            err.name = "ArgumentNullException";
            err.description = err.message = "Parameter '" + paramName + "' must be a non-empty string.";
            
            throw err;
        }
    },
    assertIsIdNumber : function(number, paramName)
    {
        if (typeof number !== 'number' || number <= 0)
        {
            var err = new Error();
            err.name = "ArgumentOutOfRangeException";
            err.description = err.message = "Parameter '" + paramName + "' must be a positive integer.";
            
            throw err;
        }        
    },
    callService : function(methodName, data, callback)
    {
        anzery.initJQuery(anzery.createFunctionDelegate(
            this,
            function() 
            {
                jQuery.ajax
                (
                    {
                        type : "GET", 
                        success : typeof callback === "function" ? callback : null,
                        scriptCharset : "utf-8",
                        dataType : "jsonp",
                        url : this.matchServiceBaseUri + "/" + methodName,
                        data : data
                    }
                );
            }
        ));        
    }
}

anzery.createFunctionDelegate = function(instance, method) 
{
    return function() 
    {
        return method.apply(instance, arguments);
    }
}

anzery.initJQuery = function(callback)
{
    if (typeof jQuery === 'undefined')
    {
        anzery.initJQuery.ensureScriptTagInserted();

        if (callback)
        {
            anzery.initJQuery.subscribers.push(callback);
        }

        if (anzery.initJQuery.timeoutId == -1)
        {
            anzery.initJQuery.timeoutId = setInterval("anzery.initJQuery()", 50);
        }
    } 
    else if (callback)
    {
        callback.call();
    }
    else if (anzery.initJQuery.subscribers.length > 0)
    {
        anzery.initJQuery.subscribers.pop().call();
    }           
    else
    {
        clearInterval(anzery.initJQuery.timeoutId);
    }
}
anzery.initJQuery.ensureScriptTagInserted = function()
{
    if (!anzery.initJQuery.scriptTagInserted) 
    {
        anzery.initJQuery.scriptTagInserted = true;

        var 
            script = document.createElement("script"),
            headElement = document.getElementsByTagName("head")[0];

        script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js");
        script.setAttribute("type", "text/javascript");

        headElement.appendChild(script);
    }
}
anzery.initJQuery.scriptTagInserted = false;
anzery.initJQuery.timeoutId = -1;
anzery.initJQuery.subscribers = new Array();

