/**
 * @fileoverview
 * This file contains the Pluck comment manager class.
 */

/**
 * Pluck manager for comments.
 * @constructor
 * @author Matt York
 * @version 1.0
 */
com.rogers.socialmedia.core.pluck.manager.PluckCommentManager = function(config){
    this.inheritsFrom = com.rogers.socialmedia.core.pluck.manager.PluckBaseManager;
    this.inheritsFrom(config);
    
    /**
     * Displays the most commented articles (accross the entire site) widget.
     * @param {Integer} numItems  <b>Optional</b> number of comments to return.
     */
    this.getMostCommented = function(maxItems){
        if(!maxItems) maxItems = this.DEF_MAX_ITEMS;
		gSiteLife.SummaryArticlesMostCommented(maxItems);
	}
    
    /**
     * Displays the default comments input and existing comments (output) widgets
     * @param {String} contentId  A unique key used for referncing the page (Article, story etc.).
     * @param {Integer} numItems  <b>Optional</b> number of comments to return.
     * If not provided the default max items is used @see com.rogers.socialmedia.core.pluck.manager.PluckBaseManager.DEF_MAX_ITEMS
     */
    this.getComments = function(contentId, numItems){
        if(numItems == null) numItems = this.DEF_MAX_ITEMS;
        gSiteLife.Comments("ExternalResource", contentId, numItems, this.DEF_SORT);
    }
    
    /**
     * Displays the default comments output widgets
     * @param {String} contentId  A unique key used for referncing the page (Article, story etc.).
     * @param {Integer} numItems  <b>Optional</b> number of comments to return.
     * If not provided the default max items is used @see com.rogers.socialmedia.core.pluck.manager.PluckBaseManager.DEF_MAX_ITEMS
     */
    this.getCommentsOutput = function(contentId, numItems){
        if(numItems == null) numItems = this.DEF_MAX_ITEMS;
        gSiteLife.CommentsOutput("ExternalResource", contentId, false, numItems, this.DEF_SORT);
    }
    
    /**
     * Redirects to current URL
     */
    this.getCommentsInput = function(contentId){
        gSiteLife.CommentsInput("ExternalResource", contentId, location.href);
    }
    
    /**
     * Returns the total number of comments for a particular piece of content to the given function.
     * The responseFunction parameter is required because all DAAPI calls are asynchronous.
     * @param {String} contentId  A unique key used for referncing the page (Article, story etc.).
     * @param {Function} responseFunction  A function with one argument that is the number of comments. 
     */
    this.getTotalNumComments = function(contentId, responseFunction){
        var requestBatch = new RequestBatch();    
        var commentPage = new CommentPage(new ArticleKey(contentId), 10, 1);          
        requestBatch.AddToRequest(commentPage);   
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
                responseFunction(responseBatch.Responses[0].CommentPage.NumberOfComments);
        });
    }
    
    /**
     * Submits a comment on the given contentId
     */
	 /*
    this.submitComment = function(contentId, commentBody, pageTitle, responseFunction){
        var requestBatch = new RequestBatch();
        var commentAction = new CommentAction(new ArticleKey(contentId), this.getPageURL(), pageTitle, commentBody);  
        requestBatch.AddToRequest(commentAction);
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
                //alert("status: " + responseBatch.Messages[0].Message);
                if(typeof(responseFunction) == "function"){
                    responseFunction(responseBatch);
                }
                else if(typeof(responseFunction) == "string"){	
                    eval(responseFunction + "(" + JSON.stringify(responseBatch) + ");");
                }
        });
    }
	*/
	
	this.submitComment = function(contentId, commentBody, pageURL, pageTitle, responseFunction){
        var requestBatch = new RequestBatch();
        var commentAction = new CommentAction(new ArticleKey(contentId), pageURL, pageTitle, commentBody);  
        requestBatch.AddToRequest(commentAction);
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
                //alert("status: " + responseBatch.Messages[0].Message);
                if(typeof(responseFunction) == "function"){
                    responseFunction(responseBatch);
                }
                else if(typeof(responseFunction) == "string"){	
                    eval(responseFunction + "(" + JSON.stringify(responseBatch) + ");");
                }
        });
    }
	
	this.getCustomCommentsOutput = function(contentId, template, elementId, numPerPage, pageNum){
		if(numPerPage == null) numPerPage = this.DEF_MAX_ITEMS;
        if(pageNum == null) pageNum = 1;
        var requestBatch = new RequestBatch();    
        var commentPage = new CommentPage(new ArticleKey(contentId), numPerPage, pageNum, this.DEF_SORT); 
        requestBatch.AddToRequest(commentPage);     
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
                //alert("status: " + responseBatch.Messages[0].Message);
				var result = null;
				responseBatch = JSON.parse("{\"ResponseBatch\":" + JSON.stringify(responseBatch) + "}");
				var compiledTemplate = TrimPath.parseTemplate(template);
				var result = compiledTemplate.process(responseBatch);
				//alert("result: " + result);
				document.getElementById(elementId).innerHTML = result;
        });
	}
};