VIDER_INSTALL_RESULT_SUCCESS = 1;
            this.ESP_POLICY_PROVIDER_INSTALL_RESULT_TIMEOUT = 2;
            this.ESP_POLICY_PROVIDER_INSTALL_RESULT_FAILURE = 3;

            // Private member variables
            this.resourceStrings = resourceStrings;
            this.tpmIsAttested = false;
            this.whiteGloveMode = 0; // Initialize to unknown mode
            this.isUsingDeviceTicket = false;

            this.sessionUtilities = sessionUtilities;
            this.mdmBootstrapSessionUtilities = new mdmBootstrapSessionUtilities(
                resourceStrings,
                this.sessionUtilities.runningInOobe(),
                sessionUtilities);

            this.initializationPromise = this.sessionUtilities.autopilotApis.getDeviceAutopilotModeAsync().then((mode) => {
                this.whiteGloveMode = mode;
            }).then(() => {
                return this.sessionUtilities.autopilotApis.getOobeSettingsOverrideAsync(EnterpriseDeviceManagement.Service.AutoPilot.AutoPilotOobeSetting.aadAuthUsingDeviceTicket);
            }).then((isUsingDeviceTicket) => {
                this.isUsingDeviceTicket = isUsingDeviceTicket;
                return bridge.invoke("CloudExperienceHost.Storage.SharableData.getValue", "AutopilotWhiteGloveStartTime");
            }).then((startTime) => {
                this.whiteGloveStartTime = startTime;
            });
        }

        // Private methods
        runTpmAttestationAsync() {
            return new WinJS.Promise(
                // Promise initialization handler
                (completeDispatch, errorDispatch, progressDispatch) => {
                    let tpmAttestationWaitPromise = new WinJS.Promise(
                        // Promise initialization handler
                        (completeDispatch, errorDispatch, progressDispatch) => {
                            // Create event handler.
                            this.tpmAttestationListener = (hresult) => {
                                if (0 === hresult.target) {
                                    this.sessionUtilities.logInfoEvent(`BootstrapStatus: TPM attestation succeeded.`);
                                    this.tpmIsAttested = true;
                                } else {
                                    this.tpmAttestationErrorString = this.sessionUtilities.formatNumberAsHexString(hresult.target, 8);
                                    this.sessionUtilities.logInfoEvent(`BootstrapStatus: TPM attestation failed (hr = ${this.tpmAttestationErrorString}).`);
                                }

                                completeDispatch(true);
                            };

                            // Register event handler.
                            this.sessionUtilities.autopilotSubscriptionManager.addEventListener("attestationcompleted", this.tpmAttestationListener.bind(this));
                        },

                        // Promise cancellation event handler
                        () => {
                        });

                    // Set a max timeout for TPM attestation
                    let tpmAttestationTimeoutPromise = WinJS.Promise.timeout(this.MAX_TPM_ATTESTATION_WAIT_TIME_IN_MILLISECONDS).then(() => {
                        this.sessionUtilities.logInfoEvent("BootstrapStatus: TPM attestation timed out.");
                    });

                    let tpmAttestationPromises = [
                        tpmAttestationTimeoutPromise,
                        tpmAttestationWaitPromise
                    ];

                    // Wait for either the TPM attested state or the timeout.
                    return WinJS.Promise.any(tpmAttestationPromises).then(() => {
                        if (this.tpmAttestationListener !== null) {
                            this.sessionUtilities.autopilotSubscriptionManager.removeEventListener("attestationcompleted", this.tpmAttestationListener);
                        }

                        tpmAttestationTimeoutPromise.cancel();
                        tpmAttestationTimeoutPromise = null;

                        // ERROR_TIMEOUT
                        if (!this.tpmIsAttested && (undefined === this.tpmAttestationErrorString)) {
                            this.tpmAttestationErrorString = this.sessionUtilities.formatNumberAsHexString(this.sessionUtilities.HRESULT_TIMEOUT, 8);
                            this.sessionUtilities.storeTransientState(this.sessionUtilities.WHITE_GLOVE_ERROR_USER_MESSAGE, this.resourceStrings.WhiteGloveTpmTimeoutError);
                        }

                        completeDispatch({
                            actionSucceeded: this.tpmIsAttested,
                            statusMessage: this.tpmAttestationErrorString
                        });
                    });
                },

                // Promise cancellation event handler
                () => {
                });
        }

        startWaitForTpmAttestationAsync() {
            if (!this.isUsingDeviceTicket) {
                // Check if this is the White Glove flow, which requires TPM attestation.
                return bridge.invoke("CloudExperienceHost.Storage.SharableData.getValue", "AutopilotWhiteGloveStartTime").then(
                    // Continuation handler
                    (result) => {
                        if (undefined === result) {
                            // White Glove is NOT in progress.
                            this.sessionUtilities.logInfoEvent("BootstrapStatus: Unable to find AutopilotWhiteGloveStartTime value.");

                            // Since the device won't be using a device AAD ticket and White Glove is not in progress, then there's no need to initiate TPM attestation.
                            return WinJS.Promise.as({
                                actionSucceeded: true,
                                statusMessage: null
                            });
                        }

                        // White Glove is in progress.
                        this.sessionUtilities.logInfoEvent("OOBEProvisioningProgress AutopilotWhiteGloveFlow");
                        return this.runTpmAttestationAsync();
                    },

                    // Error handler
                    (e) => {
                        // Return an error.
                        this.sessionUtilities.logInfoEvent("BootstrapStatus: Unable to find AutopilotWhiteGloveStartTime value.");
                        this.sessionUtilities.logErrorEvent("BootstrapStatus: TPM attestation failed.", e);

                        return WinJS.Promise.as({
                            actionSucceeded: false,
                            statusMessage: null
                        });
                    });
            }

            return this.runTpmAttestationAsync();
        }

        pollForPpkgProcessingResultsAsync() {
            return new WinJS.Promise(
                // Promise initialization handler
                (completeDispatch, errorDispatch, progressDispatch) => {
                    return this.sessionUtilities.provisioningPluginManager.getProvisioningSucceededAsync().then((processingResult) => {
                        if (this.sessionUtilities.provisioningPluginManager.isRebootRequired()) {
                            // Provisioning processing requires a reboot. Initiate it now.
                            this.sessionUtilities.logInfoEvent("BootstrapStatus: Provisioning processing requires a reboot.");
                            try {
                                bridge.fireEvent(constants.Events.done, constants.AppResult.action1);
                            } catch (e) {
                                this.sessionUtilities.logErrorEvent("BootstrapStatus: Attempted reboot following provisioning failed.", e);
                            }

                            // Stop polling with success (although shouldn't matter, since the device will reboot anyway).
                            completeDispatch({
                                actionSucceeded: true,
                                statusMessage: null
                            });

                        } else if (this.PROVISIONING_STATUS_SUCCEEDED === processingResult) {
                            // Stop polling with success.
                            completeDispatch({
                                actionSucceeded: true,
                                statusMessage: null
                            });

                        } else if (this.PROVISIONING_STATUS_FAILED === processingResult) {
                            this.sessionUtilities.logInfoEvent("BootstrapStatus: PPKG package provisioning failed.");

                            // Stop polling with error.
                            completeDispatch({
                                actionSucceeded: false,
                                statusMessage: this.resourceStrings["BootstrapPageDevicePreparationReapplyingPpkgsErrorMessage"]
                            });
                        }

                        // Default: Continue waiting.

                    }).then(() => {
                        // Continue waiting.
                        return WinJS.Promise.timeout(this.POLLING_INTERVAL_IN_MILLISECONDS);

                    }).then(() => {
                        // Poll once again.
                        return pollForPpkgProcessingResultsAsync();
                    });

                }, () => {
                    // Promise cancellation event handler
                });
        }

        startPpkgProcessingAsync() {
            return this.sessionUtilities.getSettingAsync(this.sessionUtilities.STATE_NAME_GLOBAL_RESTORE_MDM_TASKS).then((restoreMdmTasks) => {
                if (restoreMdmTasks === "false") {
                    return WinJS.Promise.as();
                }

                try {
                    return this.sessionUtilities.enrollmentApis.rebuildSchedulesAndSyncWithServerAsync();
                } catch (e) {
                    this.sessionUtilities.logErrorEvent("BootstrapStatus: rebuildSchedulesAndSyncWithServerAsync failed.", e);
                }

            }).then(() => {
                return this.sessionUtilities.getSettingAsync(this.sessionUtilities.STATE_NAME_GLOBAL_RUN_PROVISIONING);

            }).then((runProvisioning) => {
                if (runProvisioning === "false") {
                    // No provisioning should be done.
                    return WinJS.Promise.as({
                        actionSucceeded: true,
                        statusMessage: null
                    });
                }

                let provisioningPromises = [
                    this.sessionUtilities.provisioningPluginManager.applyAfterConnectivityPackagesAsync(),
                    WinJS.Promise.timeout(this.MIN_PPKG_PROCESSING_TIME_IN_MILLISECONDS)
                ];

                return WinJS.Promise.join(provisioningPromises)
                    .then(() => {
                        return this.pollForPpkgProcessingResultsAsync();
                    });

            });
        }

        startDeviceAadjAndEnrollmentHelperAsync() {
            this.sessionUtilities.logInfoEvent("BootstrapStatus: Starting Autopilot device enrollment.");

            return this.sessionUtilities.autopilotApis.performDeviceEnrollmentAsync().then((result) => {
                // Continuation handler
                this.sessionUtilities.logInfoEvent("BootstrapStatus: Device enrollment call completed. Processing results...");
                let enrollmentState = result.enrollmentDisposition;

                this.sessionUtilities.logInfoEvent("BootstrapStatus: Result data extracted.");

                if (enrollmentState === EnterpriseDeviceManagement.Service.AutoPilot.EnrollmentDisposition.completed) {
                    this.sessionUtilities.logInfoEvent("BootstrapStatus: Enrollment disposition marked as completed.");

                    this.sessionUtilities.logInfoEvent(`BootstrapStatus: Device enrollment results: ${enrollmentState}, ${this.sessionUtilities.formatNumberAsHexString(result.dispositionResult, 8)}`);

                    return WinJS.Promise.as({
                        actionSucceeded: true,
                        statusMessage: null
                    });

                } else {
                    this.sessionUtilities.logInfoEvent("BootstrapStatus: Enrollment disposition marked as not completed.");

                    let errorCode = this.sessionUtilities.formatNumberAsHexString(result.dispositionResult, 8);
                    let resultMessage = this.sessionUtilities.formatMessage(`${enrollmentState}, ${errorCode}`);

                    this.sessionUtilities.logInfoEvent(`BootstrapStatus: Device enrollment results: ${resultMessage}`);

                    switch (enrollmentState) {
                        case EnterpriseDeviceManagement.Service.AutoPilot.EnrollmentDisposition.aadConfigure:
                            this.sessionUtilities.storeTransientState(this.sessionUtilities.WHITE_GLOVE_ERROR_USER_MESSAGE, this.sessionUtilities.formatMessage(this.resourceStrings.WhiteGloveAadConfigureError, errorCode));
                            break;

                        case EnterpriseDeviceManagement.Service.AutoPilot.EnrollmentDisposition.aadJoin:
                            this.sessionUtilities.storeTransientState(this.sessionUtilities.WHITE_GLOVE_ERROR_USER_MESSAGE, this.sessionUtilities.formatMessage(this.resourceStrings.WhiteGloveAadJoinError, errorCode));
                            break;

                        case EnterpriseDeviceManagement.Service.AutoPilot.EnrollmentDisposition.aadDeviceDiscovery:
                            this.sessionUtilities.storeTransientState(this.sessionUtilities.WHITE_GLOVE_ERROR_USER_MESSAGE, this.sessionUtilities.formatMessage(this.resourceStrings.WhiteGloveAadDeviceDiscoveryError, errorCode));
                            break;

                        case EnterpriseDeviceManagement.Service.AutoPilot.EnrollmentDisposition.aadTicket:
                            this.sessionUtilities.storeTransientState(this.sessionUtilities.WHITE_GLOVE_ERROR_USER_MESSAGE, this.sessionUtilities.formatMessage(this.resourceStrings.WhiteGloveAadTicketError, errorCode));
                            break;

                        case EnterpriseDeviceManagement.Service.AutoPilot.EnrollmentDisposition.mdmEnrolling:
                            this.sessionUtilities.storeTransientState(this.sessionUtilities.WHITE_GLOVE_ERROR_USER_MESSAGE, this.sessionUtilities.formatMessage(this.resourceStrings.WhiteGloveMdmEnrollmentError, errorCode));
                            break;

                        default:
                            this.sessionUtilities.storeTransientState(this.sessionUtilities.WHITE_GLOVE_ERROR_USER_MESSAGE, this.sessionUtilities.formatMessage(this.resourceStrings.WhiteGloveGenericEnrollmentError, errorCode));
                    }

                    return WinJS.Promise.as({
                        actionSucceeded: false,
                        statusMessage: resultMessage
                    });
                }
            },
            (e) => {
                // Error handler
                this.sessionUtilities.logErrorEvent("BootstrapStatus: performDeviceEnrollmentAsync failed.", e);

                return WinJS.Promise.as({
                    actionSucceeded: false,
                    statusMessage: null
                });
            });
        }

        startDeviceAadjAndEnrollmentAsync() {
            if ((!this.isUsingDeviceTicket || (this.whiteGloveMode === EnterpriseDeviceManagement.Service.AutoPilot.AutopilotMode.whiteGloveDJPP))
                && (this.whiteGloveMode !== EnterpriseDeviceManagement.Service.AutoPilot.AutopilotMode.whiteGloveCanonical)) {
                // If the device won't be using a device AAD ticket, then there's no need to perform
                // device AADJ and MDM enrollment.
                return WinJS.Promise.as({
                    actionSucceeded: true,
                    statusMessage: null
                });
            } 

            if (undefined !== this.whiteGloveStartTime) {
                // Device enrollment for White Glove.
                this.sessionUtilities.logInfoEvent("BootstrapStatus: White glove device enrollment.");

                return this.sessionUtilities.autopilotApis.setDeviceAutopilotModeAsync(EnterpriseDeviceManagement.Service.AutoPilot.AutopilotMode.whiteGloveCanonical).then(() => {
                    return this.startDeviceAadjAndEnrollmentHelperAsync();
                },
                (e) => {
                    this.sessionUtilities.logErrorEvent("BootstrapStatus: White glove device enrollment error.", e);
                });
            }

            this.sessionUtilities.logInfoEvent("BootstrapStatus: Normal (i.e., non-white-glove) device enrollment.");
            return this.startDeviceAadjAndEnrollmentHelperAsync();
        }

        waitForEspPolicyProviders() {
            this.sessionUtilities.logInfoEvent("BootstrapStatus: Start wait for ESP policy providers");

            return this.sessionUtilities.espTrackingUtility.waitForPolicyProviderInstallationToCompleteAsync().then((result) => {
                this.sessionUtilities.logInfoEvent("BootstrapStatus: waitForPolicyProviderInstallationToComplete returned, processing results...");

                if (result.installationResult === this.ESP_POLICY_PROVIDER_INSTALL_RESULT_SUCCESS) {
                    // Completed successfully
                    this.sessionUtilities.logInfoEvent("BootstrapStatus: waitForPolicyProviderInstallationToComplete returned success result.");

                    return WinJS.Promise.as({
                        actionSucceeded: true,
                        statusMessage: null
                    });
                }

                // Return failure.
                let errorCode = 0;

                switch (result.installationResult) {
                    case this.ESP_POLICY_PROVIDER_INSTALL_RESULT_TIMEOUT:
                        // Provider timeout
                        errorCode = result.errorCode;
                        this.sessionUtilities.logInfoEvent("BootstrapStatus: Policy provider failure: installation timed out.");
                        break;

                    case this.ESP_POLICY_PROVIDER_INSTALL_RESULT_FAILURE:
                        // Provider reported error
                        errorCode = result.errorCode;
                        this.sessionUtilities.logInfoEvent(`BootstrapStatus: Policy provider failure: installation failed with error ${this.sessionUtilities.formatNumberAsHexString(errorCode, 8)}.`);
                        break;

                    default:
                        errorCode = 0x8000FFFF; // E_UNEXPECTED
                        this.sessionUtilities.logInfoEvent(`BootstrapStatus: Policy provider failure: unexpected installationResult encountered (${result.installationResult}).`);
                }

                this.sessionUtilities.storeTransientState(this.sessionUtilities.WHITE_GLOVE_ERROR_USER_MESSAGE, this.sessionUtilities.formatMessage(this.resourceStrings.WhiteGloveEspProviderError, this.sessionUtilities.formatNumberAsHexString(errorCode, 8)));

                return WinJS.Promise.as({
                    actionSucceeded: false,
                    statusMessage: this.sessionUtilities.formatNumberAsHexString(errorCode, 8)
                });
            },
            (e) => {
                this.sessionUtilities.logErrorEvent("BootstrapStatus: waitForPolicyProviderInstallationToComplete_Failed", e);
            });
        }

        setContinueAnywayButtonVisibility() {
            // If this subcategory is being run, it means Device Preparation has succeeded and the Continue Anyway button can be enabled
            this.sessionUtilities.logInfoEvent("BootstrapStatus: setContinueAnywayButtonVisibility triggered.");

            return this.sessionUtilities.storeSettingAsync(this.sessionUtilities.STATE_NAME_GLOBAL_SHOW_CONTINUE_ANYWAY_BUTTON, "true").then(() => {
                return WinJS.Promise.as({
                    actionSucceeded: true,
                    statusMessage: null
                });
            });
        }

        // Category interface methods

        getId() {
            return "DevicePreparationCategory";
        }

        getTitle() {
            return this.resourceStrings["BootstrapPageDevicePreparationCategoryTitle"];
        }

        getIconClass() {
            return "icon-gears";
        }

        getDisposition() {
            return (this.sessionUtilities.runningInOobe() ? this.sessionUtilities.CATEGORY_DISPOSITION_VISIBLE : this.sessionUtilities.CATEGORY_DISPOSITION_IGNORED);
        }

        runsInOobe() {
            return true;
        }

        getInitializationPromise() {
            return this.initializationPromise;
        }

        getSubcategories() {
            return [
                new bootstrapStatusSubcategoryViewModel(
                    this.resourceStrings,
                    this.sessionUtilities,
                    "TpmAttestationSubcategory",
                    this.resourceStrings["BootstrapPageDevicePreparationTpmAttestationSubcategoryTitle"],
                    false,
                    () => {
                        // Skip TPM attestation if post-reset because device is already enrolled
                        if (this.sessionUtilities.provisioningPluginManager.isPostPowerwash()) {
                            this.sessionUtilities.logInfoEvent("BootstrapStatus: Skipping TPM attestation because in post-reset flow.");
                            return this.sessionUtilities.SUBCATEGORY_DISPOSITION_IGNORED;
                        }

                        return this.sessionUtilities.SUBCATEGORY_DISPOSITION_VISIBLE;
                    },
                    (progressCallback) => {
                        return this.startWaitForTpmAttestationAsync();
                    }),

                new bootstrapStatusSubcategoryViewModel(
                    this.resourceStrings,
                    this.sessionUtilities,
                    "AadjSubcategory",
                    this.resourceStrings["BootstrapPageDevicePreparationAadjSubcategoryTitle"],
                    false,
                    () => {
                        return this.sessionUtilities.SUBCATEGORY_DISPOSITION_VISIBLE;
                    },
                    (progressCallback) => {
                        return this.startPpkgProcessingAsync();
                    }),

                new bootstrapStatusSubcategoryViewModel(
                    this.resourceStrings,
                    this.sessionUtilities,
                    "MdmEnrollmentSubcategory",
                    this.resourceStrings["BootstrapPageDevicePreparationMdmEnrollmentSubcategoryTitle"],
                    false,
                    () => {
                        return this.sessionUtilities.SUBCATEGORY_DISPOSITION_VISIBLE;
                    },
                    (progressCallback) => {
                        return this.startDeviceAadjAndEnrollmentAsync();
                    }),

                new bootstrapStatusSubcategoryViewModel(
                    this.resourceStrings,
                    this.sessionUtilities,
                    "EspProviderInstallationSubcategory",
                    this.resourceStrings["BootstrapPageDevicePreparationEspProviderInstallationSubcategoryTitle"],
                    true,
                    () => {
                        return this.sessionUtilities.SUBCATEGORY_DISPOSITION_VISIBLE;
                    },
                    (progressCallback) => {
                        return this.waitForEspPolicyProviders();
                    }),

                // This subcategory needs to be parallelized with EspProviderInstallationSubcategory because it's waiting on Intune to install
                // policy providers during sync sessions.
                new bootstrapStatusSubcategoryViewModel(
                    this.resourceStrings,
                    this.sessionUtilities,
                    "DevicePreparation.InitiateSyncSessions",
                    "DevicePreparation.InitiateSyncSessions",
                    true,
                    () => {
                        return this.sessionUtilities.SUBCATEGORY_DISPOSITION_SILENT;
                    },
                    (progressCallback) => {
                        // This is a fire and forget best effort operation, always return succes
                        this.mdmBootstrapSessionUtilities.initiateSyncSessionsAsync(ModernDeployment.Autopilot.Core.SyncSessionExitCondition.policyProvidersComplete);

                        return WinJS.Promise.as({
                            actionSucceeded: true,
                            statusMessage: null
                        });
                    }),

                // The following subcategory has to run last in Device Preparation as
                // the continue anyway button should only be enabled to show if all previous
                // subcategories have succeeded
                new bootstrapStatusSubcategoryViewModel(
                    this.resourceStrings,
                    this.sessionUtilities,
                    "SetContinueAnywayButtonVisibility",
                    "SetContinueAnywayButtonVisibility",
                    false,
                    () => {
                        return this.sessionUtilities.SUBCATEGORY_DISPOSITION_SILENT;
                    },
                    (progressCallback) => {
                        return this.setContinueAnywayButtonVisibility();
                    })
            ];
        }

        getClickHandler() {
            return (handlerParameters) => {
                // True means that this handler succeeded.
                return WinJS.Promise.as(true);
            };
        }

    }

    return devicePreparationCategoryViewModel;
});
