
                this.updateMessageText(this.resourceStrings.UpdateMessageRestartText);
                await bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "Autopilot update installed and needs reboot");

                await this.setDeviceUpForReboot();

                await WinJS.Promise.timeout(FinishedMessageDelay);
            } else if (result === EnterpriseDeviceManagement.Service.AutoPilot.AutopilotUpdateStatus.updateInstalled) {
                await bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "Autopilot update installed and does not need reboot. We are forcing a reboot to re-consume updated .js files");
                this.updateMessageText(this.resourceStrings.UpdateMessageRestartText);

                await this.setDeviceUpForReboot();

                await WinJS.Promise.timeout(FinishedMessageDelay);
            } else if (result === EnterpriseDeviceManagement.Service.AutoPilot.AutopilotUpdateStatus.unknownFailure) {
                await bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "Autopilot update unknownFailure");
                throw new Error("AutopilotUpdateStatus.unknownFailure");
            } else if (result === EnterpriseDeviceManagement.Service.AutoPilot.AutopilotUpdateStatus.updateFailed) {
                await bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "Autopilot updateFailed");
                throw new Error("AutopilotUpdateStatus.updateFailed");
            } else {
                throw new Error("AutopilotUpdateStatus.unknownState");
            }
        }

        async setDeviceUpForReboot()
        {
            const UpdateRebootCXIDKey = "UpdateRebootCXID";
            // Mark that this reboot is caused by autopilot update, and force the start selector to jump to the Autopilot prefetch page.
            await bridge.invoke("CloudExperienceHost.Storage.SharableData.addValue", "resetFromAutopilotUpdate", true);
            await bridge.invoke("CloudExperienceHost.setRebootForOOBE", "AutopilotPrefetch");

            try {
                var enterpriseManagementWorker = new EnterpriseDeviceManagement.Enrollment.ReflectedEnroller();
                enterpriseManagementWorker.clearAutoLoginData();
            } catch (e) {
                await bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "Failed to clear Autologin Credentials");
            }

            let autopilotServer = new EnterpriseDeviceManagement.Service.AutoPilot.AutoPilotServer();

            // Update UpdateRebootCXIDKey to jump back to this page if it currently isn't set.
            let existingUpdateRebootCXID = await autopilotServer.getSettingAsync(UpdateRebootCXIDKey);
            if (existingUpdateRebootCXID === "")
            {
                let currentNode = await bridge.invoke("CloudExperienceHost.AutoPilot.AutopilotWrapper.GetCurrentNode");
                await autopilotServer.storeSettingAsync(UpdateRebootCXIDKey, currentNode.cxid);
            }
        }

        async handleAutopilotUpdateProgressNotificationAsync(progress) {
            await bridge.invoke("CloudExperienceHost.Telemetry.logEvent", `Autopilot Update Progress: ${JSON.stringify(progress)}`);

            if (progress.autopilotUpdateState === EnterpriseDeviceManagement.Service.AutoPilot.AutopilotUpdateState.downloading) {
                this.updateMessageText(this.resourceStrings.UpdateMessageDownloadingText_param.replace("{0}", progress.currentStateProgress));
            } else if (progress.autopilotUpdateState === EnterpriseDeviceManagement.Service.AutoPilot.AutopilotUpdateState.installing) {
                this.updateMessageText(this.resourceStrings.UpdateMessageInstallingText_param.replace("{0}", progress.currentStateProgress));
            }
        }

        performAutopilotUpdateAndHookProgressToUIAsync() {
            return autopilotCore.performAutopilotUpdateWithProgressAsync()
                .then(
                    result => this.handleAutopilotUpdateResultAsync(result),
                    null, // don't handle errors, let them propagate
                    progress => this.handleAutopilotUpdateProgressNotificationAsync(progress)
                );
        }

        async startUpdateAsync() {
            try {
                await bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "oobeAutopilotUpdateStarted");

                const timeoutDuration = await autopilotCore.getAutopilotUpdateTimeoutAsync();

                const updatePromise = this.performAutopilotUpdateAndHookProgressToUIAsync();

                await WinJS.Promise.timeout(timeoutDuration, updatePromise);

                bridge.fireEvent(constants.Events.done, constants.AppResult.success);
            } catch (error) {
                await bridge.invoke("CloudExperienceHost.Telemetry.logEvent", `AutopilotUpdateFailed: ${error}`);
                bridge.fireEvent(constants.Events.done, constants.AppResult.error);
            }
        }
    }

    return AutopilotUpdateViewModel;
});
