﻿var DocumentService = {};

DocumentService.projectAsUriComponent= function(project, existingUriComponents)
{
    if(typeof(existingUriComponents) != "object")
        existingUriComponents = {};
    
    if(project)
    {
        if(typeof(project) == "number")
            existingUriComponents["pid"] = project;
        else if(typeof(project) == "string")
            existingUriComponents["pname"] = project;
        else if(typeof(project) == "object")
        {
            if(project["id"])
                existingUriComponents["pid"] = project["id"];
            else if(project["name"])
                existingUriComponents["pname"] = project["name"];
            else if(project["uri"])
                existingUriComponents["puri"] = project["uri"];
        }
    }
    
    return existingUriComponents;
}

//Displays workitem editor window for a specific workitem.
//If that workitem is already displayed this will give focus to existing workitem editor window.
//workItemId: WorkItem id to edit.
DocumentService.showWorkItem= function(workItemId)
{
    if(!workItemId)
        throw "Unspecified WorkItem Id.";

    return WindowHelpers.openWorkItemEditorWithId(workItemId);
}

//Opens new workitem editor with specified workitem type.
//workItemType: WorkItem type name.
//project: Project reference. It might be either project id if it is a number value or project name if it is a string value. 
//example: DocumentService.newWorkItem("Bug", 2); //This will open new workitem editor for "Bug" workitem type for project whose id is 2.
//example: DocumentService.newWorkItem("Bug", {"id": 2}); //This will open new workitem editor for "Bug" workitem type for project whose id is 2.
//example: DocumentService.newWorkItem("Bug", "Northwind"); //This will open new workitem editor for "Bug" workitem type for project whose name is Northwind.
//example: DocumentService.newWorkItem("Bug", {"name": "Northwind"}); //This will open new workitem editor for "Bug" workitem type for project whose name is Northwind.
//example: DocumentService.newWorkItem("Bug", {"uri": "project-uri"}); //This will open new workitem editor for "Bug" workitem type for project whose uri is project-uri.
DocumentService.newWorkItem= function(workItemType, project, reserved)
{
    if(JsUtility.stringIsNullOrEmpty(workItemType))
        throw "Unspecified WorkItem Type Name.";
    
    var _url = CommonUrls.WorkItemEditor;
    
    var _uriComponents = {"wit": workItemType};
    
    DocumentService.projectAsUriComponent(project, _uriComponents);
            
    _url = JsUtility.joinUriComponents(_uriComponents, _url, "?");
    _url = JsUtility.joinUriComponents(reserved, _url, "&");
    
    return WindowHelpers.openWorkItemEditor(_url, "_blank");
}

//Opens new query editor.
//[scope]: Optional query scope it may be either "private" or "public". Default value is "private". 
//In either case, user can specify query scope while saving.
//[name]: Optional name for the query is being created.
//[project]: Optional project reference. It might be either project id if it is a number value or project name if it is a string value.
//[wiql]: Optional query text to initalize query editor.
DocumentService.newQuery= function(scope, name, project, wiql, reserved)
{
    var _url = CommonUrls.QueryEditor;
    
    var _uriComponents = {"scope": scope, "name": name, "wiql": wiql};
    
    DocumentService.projectAsUriComponent(project, _uriComponents);
    
    _url = JsUtility.joinUriComponents(_uriComponents, _url, "?");
    _url = JsUtility.joinUriComponents(reserved, _url, "&");
    
    return this.openNewWindow(_url, "_blank");
}

//Opens query editor for specific stored query.
//queryId: Stored query id to edit.
DocumentService.viewQuery= function(queryId, reserved)
{
    if(JsUtility.stringIsNullOrEmpty(queryId))
        throw "Unspecified Query Id.";
    
    var _url = CommonUrls.QueryEditor;
    var _uriComponents = {"id": queryId};
    
    _url = JsUtility.joinUriComponents(_uriComponents, _url, "?");
    _url = JsUtility.joinUriComponents(reserved, _url, "&");
    
    return WindowHelpers.openQueryEditor(_url);
}

//Runs specified stored query and displays results in a query results window.
//queryId: Stored query id.
DocumentService.viewQueryResult= function(queryId)
{
    if(JsUtility.stringIsNullOrEmpty(queryId))
        throw "Unspecified Query Id.";

    return WindowHelpers.openNewWindow(CommonUrls.QueryResultNW + "?qid=" + encodeURIComponent(queryId));
}

//Runs specified stored query text and displays results in a query results window.
//wiql: Query text to run.
//[name]: Optional name for the results.
DocumentService.viewQueryResultByWiql= function(wiql, name)
{
    if(JsUtility.stringIsNullOrEmpty(wiql))
        throw "Undefined, Null or Empty Query Text.";

    var _url = CommonUrls.QueryResultNW;
    
    var _uriComponents = {"wiql": wiql, "name":  name};
    
    _url = JsUtility.joinUriComponents(_uriComponents, _url, "?");
        
    return WindowHelpers.openNewWindow(_url);
}

//Displays Workitem Picker Dialog and returns selected workitem infos to resultHandler.
//[resultHandler]: Optional callback function to get results back. 
//example:DocumentService.showWorkItemPickerDialog(function(workItemInfoArray){ //Do your stuff here });
//workItemInfoArray: Array of workItemInfo objects. workItemInfo object is defined as {id: <workitem id>, title: <workitem title>, state: <workitem state> }.
DocumentService.showWorkItemPickerDialog= function(resultHandler)
{
    return WindowManager.openDialog(CommonUrls.PickWorkItems,
					    null, 
					    "dialogwidth:620px;dialogheight:500px;center:yes;resizable:no;status:no;scroll:no", 
					    resultHandler);  
}

//Displays report in a window.
//report: report id or report path.
//project: Project reference.
DocumentService.viewReport= function(report, project)
{
    if(JsUtility.stringIsNullOrEmpty(report))
        throw "Unspecified Report Specifier.";
 
    var _url = CommonUrls.ReportView;
    var _uriComponents = {"report": report};
    
    DocumentService.projectAsUriComponent(project, _uriComponents);
    
    _url = JsUtility.joinUriComponents(_uriComponents, _url, "?");
    
    return WindowHelpers.openNewWindow(_url);
}

//Displays build status of a specific build process in a window.
//buildUri: Build Id for a specific build process.
DocumentService.viewBuildReport= function(buildUri)
{
    if(JsUtility.stringIsNullOrEmpty(buildId))
        throw "Unspecified Build Id.";

    return WindowHelpers.openBuildResult(buildId);
}

//Displays changeset details for a specific changeset.
//changesetId: Changeset number.
DocumentService.viewChangeset= function(changesetId)
{
    return WindowHelpers.openNewWindow(CommonUrls.SccChangeset + "?changeset=" + changeset);
}

//Displays history of a source control item.
//itemId: Id of an artifact in source control database.
//changesetId: Changeset Id in order to find specific version of the source control item. 
DocumentService.viewHistory= function(itemId, changesetId)
{
    var _sccItem = JsUtility.joinUriComponents({"id": itemId, "cs" : changesetId});
    
    return WindowHelpers.openNewWindow(CommonUrls.SccHistory + "?scc-item=" + encodeURIComponent(_sccItem));
}

//Displays contents of a source control item.
//itemId: Id of an artifact in source control database.
//changesetId: Changeset Id in order to find specific version of the source control item. 
DocumentService.viewSccFile= function(itemId, changesetId)
{
    var _sccItem = JsUtility.joinUriComponents({"id": itemId, "cs" : changesetId});
    
    return WindowHelpers.openNewWindow(CommonUrls.SccView + "?scc-item=" + encodeURIComponent(_sccItem));
}

//Displays difference between contents of two source control items.
DocumentService.diffSccFiles= function(leftItemId, leftChangeset, rightItemId, rightChangeset)
{
    var _url = JsUtility.joinUriComponents({"oitem": leftItemId, "ocs" : leftChangeset, "mitem": rightItemId, "mcs" : rightChangeset}, CommonUrls.SccDiff, "?");

    return WindowHelpers.openNewWindow(_url);
}

//Displays line by line basis changes in contents of source control item.
DocumentService.annotateSccFile= function(itemId, changesetId)
{
    var _sccItem = JsUtility.joinUriComponents({"id": itemId, "cs" : changesetId});
    
    return WindowHelpers.openNewWindow(CommonUrls.SccAnnotate + "?scc-item=" + encodeURIComponent(_sccItem));
}
