/*!
**********************************************************************
@file Finish.js

Copyright 2003-2006 Adobe Systems Incorporated.                     
All Rights Reserved.                                                
                                                                    
NOTICE: All information contained herein is the property of Adobe   
Systems Incorporated.                                                                                                                    

***********************************************************************
*/

function Finish_wp(inController)
{
	this.SetController(inController);
	this.restartNeeded = false;
}

Finish_wp.prototype = new WizardPage("Finish");

Finish_wp.prototype.GetPageTitleDisplayName = function()
{
	return this.localization.GetString("locTitle", "Uninstall Complete");
}

Finish_wp.prototype.GetNavDisplayName = function()
{
	return this.localization.GetString("locNavTitle", "Finish");
}

Finish_wp.prototype.onResourcesLoaded = function()
{
	this.RenderInitialWizardImagesOfPage();//from WizardPage
	this.contentBox = this.getElementById("installFinishScrollPort");
}

Finish_wp.prototype.FillButtonArea = function()
{
	var buttonarea = document.getElementById("navButtons");
	var thisPage = this;

	this.quitWB = new WizardButton1(8, "c", this.wizardControl.session.localization.GetString("locexitbtn", "Exit"), 
	    "128px", "32px", "331px", "16px", "bigbutton_normalsize", buttonarea, function(){thisPage.wizardControl.NavQuit();});
}

Finish_wp.prototype.CreateWAMPage = function()
{
	if (this.wizardControl.WAM)
	{
		var defElement = this.quitWB.GetDisplayElement();
		this.wizardControl.WAM.AddElementSet	([
							//{element:_,hotkey:_,focal:_,nofocal:_,type:_},
							{element:this.getElementById("uninstallSSCol1"),virtualtype:'ReadableText',jawslabel:'Press Alt Plus E For Exit Button.'},
							{element:this.getElementById("installFinishSuccess"),virtualtype:'ReadableText'},
							{element:this.getElementById("uninstallCompleteSuccess_PP"),virtualtype:'ReadableText',jawslabel:'Press Alt Plus E For Exit Button.'},
							{element:this.getElementById("uninstallFailure"),virtualtype:'ReadableText',jawslabel:'Press Alt Plus E For Exit Button.'},
							{element:defElement,hotkey:'E',virtualtype:'WizardButton1'}
						]);
		this.wizardControl.WAM.SetPageDefaultEnterElement(defElement);
		this.wizardControl.WAM.SetPageDefaultEscapeElement(defElement);
		this.wizardControl.WAM.SetPageDefaultFocusElement(defElement);
		this.wizardControl.WAM.SetFocusOnDefaultElement();
		if (this.moreinformationWB)
			thiswizardControl.WAM.AddElement({element:this.moreinformationWB.GetDisplayElement(),hotkey:'M',virtualtype:'WizardButton1'})
	}
}

Finish_wp.prototype.onShow = function()
{
	this.ClearButtonArea();
	this.FillButtonArea();

	this.setWindowTitle();
	this.wizardControl.session.UISetCloseBoxEnabled(1);	
	
	var operation = "";

	// Get the scoop on the payloads.
	var allPayloads = GetPayloadListByOperationResult(this.wizardControl.session);

	// Do we need a restart to keep anyone happy?
	if (true == this.restartNeeded || this.wizardControl.session.IsRestartNeeded())
	{
		this.restartNeeded = true; // Needed in onNext() pass the right exit code

		var buttonTextMap = {
			Restart: this.localization.GetString("locFinishAndRestartButton", "Finish & Restart"),
			Finish: this.localization.GetString("locNextButton", "Finish")
		}
		this.errorbox.AddError("Finish.restart", "warning", 
			this.localization.GetString("locFinishRestartWarningMessage", "Restarting your computer is recommended.  Click '[Restart]' to exit the install and restart or click '[Finish]' to exit immediately.", buttonTextMap));

		// Log the restart info for secondary use
		gSession.LogInfo(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");		
		gSession.LogInfo("Restarting your computer is recommended:");
		gSession.LogInfo("In order to Finish the installation, please restart the computer");
		gSession.LogInfo(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	}

	var kPPCompleteSuccessDivID = "uninstallCompleteSuccess_PP";
	var kSuiteCompleteSuccessDivID = "uninstallCompleteSuccess_Suite";
	var kFailureDivID = "uninstallFailure";

	var allCompleteTypes = new Array(kPPCompleteSuccessDivID, kSuiteCompleteSuccessDivID, kFailureDivID);

	// Only if we are installing OR we have payload errors do we show the full report.
	if (allPayloads["fail"].visibleList.length == 0
		&& allPayloads["fail"].hiddenList.length == 0
		&& allPayloads["partialPass"].visibleList.length == 0
		&& allPayloads["partialPass"].hiddenList.length == 0)
	{
		// Suite success case
        if(gSession.IsSuiteMode())
        {
    	    this.makeElementVisibleInList(kSuiteCompleteSuccessDivID, allCompleteTypes);
		    
		    // Show the per-payload results.
		    for (var payloadSet in allPayloads)
		    {
				var set = allPayloads[payloadSet];
		        if ((set.visibleList && set.visibleList.length > 0) || (set.hiddenList && set.hiddenList.length > 0))
			    {
				    // Setup the section header and list
				    var componentCount = set.visibleList.length;
				    if (set.hiddenList.length > 0)
					    componentCount++;

				    var TOPcomponentList = this.getElementById("uninstalledtopLevelPayloadsList");
    				
				    var TOPcomponentTable = document.createElement("table");
				    TOPcomponentList.appendChild(TOPcomponentTable);
    				
				    var TOPcomponentTableBody = document.createElement("tbody");
				    TOPcomponentTable.appendChild(TOPcomponentTableBody);
    				
				    var BRcomponentList = this.getElementById("uninstalledbrandedSharedCompList");
    				
				    var BRcomponentTable = document.createElement("table");
				    BRcomponentList.appendChild(BRcomponentTable);
    				
				    var BRcomponentTableBody = document.createElement("tbody");
				    BRcomponentTable.appendChild(BRcomponentTableBody);
    				
				    // List out the visible payloads
				    for (var i = 0; i < set.visibleList.length; i++)
				    {
					    var name = set.visibleList[i].payloadName;
					    var icon = set.visibleList[i].payloadIcon;
    					
			            var tr = document.createElement("tr");
	                    tr.className = "tr_finish_product_success";
    	                
				        if(set.visibleList[i].payloadType == kTopLevelPayload)
						    TOPcomponentTableBody.appendChild(tr);
					    else
						    BRcomponentTableBody.appendChild(tr);
    	                
						    var td = document.createElement("td");
						    td.className = "td_finish_product_success";
						    tr.appendChild(td);
    	                
							    var finishProductTable = document.createElement("table");
							    td.appendChild(finishProductTable);
        				
								    var finishProductTableBody = document.createElement("tbody");
								    finishProductTable.appendChild(finishProductTableBody);
        				
									    var finishProductTr = document.createElement("tr");
									    finishProductTableBody.appendChild(finishProductTr);
        				
										    var finishProductIconTd = document.createElement("td");
										    finishProductIconTd.className = "td_finish_product_icon";
										    finishProductTr.appendChild(finishProductIconTd);
        				
										    var finishProductNameTd = document.createElement("td");
										    finishProductNameTd.className = "td_finish_name_icon";
										    finishProductTr.appendChild(finishProductNameTd);
        				
											    var productIconImg = new WizardImage1(icon, "", "productimageicon", finishProductIconTd);
    					
											    var nameSpanElement = document.createElement("span");
											    nameSpanElement.className = "productnametext";
                							    nameSpanElement.innerHTML = name;
											    finishProductNameTd.appendChild(nameSpanElement);
    					
                        /*
					    var li = document.createElement("li");

					    var opresult = this._textForOperationResult(set.visibleList[i].payloadOperation, set.visibleList[i].payloadResult);
					    if (set.visibleList[i].payloadNote)
					    {
						    var span = this.AppendTextElement("span", "(" + set.visibleList[i].payloadNote.Translate(this.wizardControl.session.localization) + ")", lip);
						    span.className = "payloadNote";
					    }

					    if (set.visibleList[i].payloadDetail || (opresult && opresult.detail))
					    {
						    if (opresult&& opresult.detail)
						    {
							    li.appendChild(this._createErrorListElement(null, opresult.detail))
						    }
						    if (set.visibleList[i].payloadDetail)
						    {
							    li.appendChild(this._createErrorListElement(null, set.visibleList[i].payloadDetail));
						    }
					    }
					    */
				    }
			    }
		    }
		}
		// PP success case
		else
		{
    	    this.makeElementVisibleInList(kPPCompleteSuccessDivID, allCompleteTypes);
		}
    }
	// Something failed
	else
	{
    	this.makeElementVisibleInList(kFailureDivID, allCompleteTypes);
		var uninstallPageInstructionElement = this.getElementById("uninstallPageInstructionElement");
		var errorText = this.localization.GetString("locDoneWithErrorsRemove");
		uninstallPageInstructionElement.innerHTML = errorText;
	}
	
	this.AutoReverseRTLOnce();
	this.ConfigureAutoReverseRTL();
	this.AutoReverseRTL();
	this.CreateWAMPage();
}


/**
Fabricate the DOM for a system check error/warning
*/
Finish_wp.prototype._createErrorListElement = function(inClass, inText, inOptContainerElement)
{
	var session = this.wizardControl.session;

	var isArray = function(inObj)
	{
		return typeof inObj == "object" && inObj.length && inObj.concat && inObj.join;
	};

	var isLocalizedString = function(inObj)
	{
		return typeof inObj == "object" && inObj.Translate && typeof inObj.Translate == "function";
	};

	var resolveString = function(inObj, forLog)
	{
		if (isLocalizedString(inObj))
		{
			if (forLog)
				return inObj.Translate(session.localization, 'en_US');
			else
				return inObj.Translate(session.localization);
		}
		return inObj;
	};

	var contentList = inText;
	if (!isArray(inText))
		contentList = new Array(inText);
		
	var errorDiv = document.createElement(typeof inOptContainerElement == "string" ? inOptContainerElement : "div");
	if (inClass)
		errorDiv.className = inClass;

	for (var i in contentList)
	{
		var item = contentList[i];
		if (isArray(item))
		{
			if (item.length > 0)
			{
				var listElement = document.createElement("ul");
				for (var si in item)
				{
					var li = document.createElement("li");
					li.appendChild(document.createTextNode(resolveString(item[si])));
					listElement.appendChild(li);
				}
				errorDiv.appendChild(listElement);
			}
		}
		else
		{
			var p = document.createElement("p");
			p.appendChild(document.createTextNode(resolveString(item)));
			errorDiv.appendChild(p);
		}
	}

	return errorDiv;
}


/**
For an payload operation and the companion result, come up with the UI text.
*/
Finish_wp.prototype._textForOperationResult = function(inOperation, inOperationResult)
{
	var result = {
		text: null,
		detail: null,
		alert: true
	};

	if (inOperationResult && inOperationResult.message)
	{
		if (inOperationResult.message.code == gConstants.kORSuccess
			|| inOperationResult.message.code == gConstants.kORSuccessWithReboot 
			|| inOperationResult.message.code == gConstants.kORSuccessWithMessage)
		{
			switch (inOperation)
			{
				case kInstallerActionInstall:
					result.text = this.localization.GetString("locSuccessInstalled", "Installed");
					break;
				case kInstallerActionRepair:
					result.text = this.localization.GetString("locSuccessRepaired", "Reinstalled");
					break;
				case kInstallerActionRemove:
					result.text = this.localization.GetString("locSuccessRemoved", "Removed");
					break;
			}
			
			if (inOperationResult.message.code == gConstants.kORSuccessWithMessage)
			{		
				if (inOperationResult.message.args)
				{
					result.detail = inOperationResult.message.args[getUserInterfaceLanguage(this.wizardControl.session)];
					if (null == result.detail)
					{
						result.detail = inOperationResult.message.args["en_US"];
					}					
				}
				result.alert = false;
			}
		}
		else
		{			
			switch (inOperation)
			{
				case kInstallerActionInstall:
					result.text = this.localization.GetString("locFailInstalled", "Not installed");
					break;
				case kInstallerActionRepair:
					result.text = this.localization.GetString("locFailRepaired", "Not reinstalled");
					break;
				case kInstallerActionRemove:
					result.text = this.localization.GetString("locFailRemoved", "Not removed");
					break;
			}
			switch (Number(inOperationResult.message.code))
			{
				case gConstants.kORUserCancel:
					result.detail = (kInstallerActionRemove != inOperation) ? 
					 				this.localization.GetString("locErrorUserCancel", "User canceled installation") :
									this.localization.GetString("locErrorUninstallUserCancel", "User canceled uninstallation");
					break;
				case gConstants.kORConflictsExist:
					result.detail = this.localization.GetString("locErrorConflicts", "Conflicts with a component already installed");
					break;
				case gConstants.kORUpgradeFailure:
					result.detail = this.localization.GetString("locErrorUpgradeFailure", "Upgrade failed");
					break;
				case gConstants.kORMissingMedia:
					result.detail = this.localization.GetString("locErrorFailedMissingMedia", "Install failed - missing media", inOperationResult.message.args);
					break;			
				default:
					result.detail = (kInstallerActionRemove != inOperation) ? 
				 					this.localization.GetString("locErrorFailed", "Install failed") :
									this.localization.GetString("locErrorUninstallFailed", "Uninstall failed");
					break;
			}
		}
	}
	return result;
}


Finish_wp.prototype.onNext = function()
{
	if (true == this.restartNeeded)
		this.wizardControl.session.UIExitDialog('8');
	else
		this.wizardControl.session.UIExitDialog();
}

Finish_wp.prototype.onCancel = function()
{
	this.wizardControl.session.UIExitDialog();
}


// The reworked Back is Finish in cases where a restart is optional
Finish_wp.prototype.onBack = function()
{
	this.wizardControl.session.UIExitDialog();
}


Finish_wp.prototype.AddTitle = function(inTitle)
{
	return this.AppendTextElement("h4", inTitle + ":");
}


Finish_wp.prototype.AppendTextElement = function(inElementName, inText, inContainer)
{
	var container = this.contentBox;
	if (arguments.length == 3)
		container = inContainer

	if (container)
	{
		var e = document.createElement(inElementName);
		e.appendChild(document.createTextNode(inText));
		container.appendChild(e);
		return e;
	}
	return null;
}

// Return the name of the class.
"Finish_wp";
