//
// Copyright (C) Microsoft. All rights reserved.
//
(() => {
    WinJS.UI.Pages.define("/webapps/inclusiveOobe/view/oobeprivacysettings-aadc-main.html", {
        init: (element, options) => {
            require.config(new RequirePathConfig('/webapps/inclusiveOobe'));

            // Load css per scenario
            let loadCssPromise = requireAsync(['legacy/uiHelpers', 'legacy/bridge']).then((result) => {
                return result.legacy_uiHelpers.LoadCssPromise(document.head, "", result.legacy_bridge);
            });

            let langAndDirPromise = requireAsync(['legacy/uiHelpers', 'legacy/bridge']).then((result) => {
                return result.legacy_uiHelpers.LangAndDirPromise(document.documentElement, result.legacy_bridge);
            });

            // Load resource strings
            let getLocalizedStringsPromise = requireAsync(['legacy/bridge']).then((result) => {
                return result.legacy_bridge.invoke("CloudExperienceHost.StringResources.makeResourceObject", "oobePrivacySettingsAadc");
            }).then((result) => {
                this.resourceStrings = JSON.parse(result);
            });

            let getSettingsLocalizedStringsPromise = requireAsync(['legacy/bridge']).then((result) => {
                return result.legacy_bridge.invoke("CloudExperienceHost.StringResources.makeResourceObject", "oobePrivacySettingsEntry");
            }).then((result) => {
                this.settingsEntryResourceStrings = JSON.parse(result);
            });

            let initializePrivacySettingsPromise = requireAsync(['oobeprivacysettings-data']).then((result) => {
                return result.oobeprivacysettings_data.initializePrivacySettingsAsync();
            });

            let isOutOfBoxExperienceIncompletePromise = requireAsync(['legacy/bridge']).then((result) => {
                const bridge = result.legacy_bridge;
                return bridge.invoke("CloudExperienceHost.Bridge.isFunctionAvailable", "Environment.isOutOfBoxExperienceIncomplete")
                .then((isFunctionAvailable) => {
                    if (isFunctionAvailable) {
                        bridge.invoke("Environment.isOutOfBoxExperienceIncomplete").then((isOOBEIncomplete) => {
                            this.isOutOfBoxExperienceIncomplete = isOOBEIncomplete;
                        });
                    }
                    else {
                        this.isOutOfBoxExperienceIncomplete = (Windows.System.Profile.SystemSetupInfo.outOfBoxExperienceState != Windows.System.Profile.SystemOutOfBoxExperienceState.completed);
                    }
                });
            });

            let isConnectedToNetworkPromise = requireAsync(['legacy/bridge']).then((result) => {
                return result.legacy_bridge.invoke("CloudExperienceHost.Environment.hasInternetAccess");
            }).then((isConnectedToNetwork) => {
                this.isInternetAvailable = isConnectedToNetwork;
            });

            return WinJS.Promise.join({ loadCssPromise: loadCssPromise, langAndDirPromise: langAndDirPromise, getLocalizedStringsPromise: getLocalizedStringsPromise, getSettingsLocalizedStringsPromise: getSettingsLocalizedStringsPromise, initializePrivacySettingsPromise: initializePrivacySettingsPromise, isOutOfBoxExperienceIncompletePromise: isOutOfBoxExperienceIncompletePromise, isConnectedToNetworkPromise: isConnectedToNetworkPromise });
        },
        error: (e) => {
            require(['legacy/bridge', 'legacy/events'], (bridge, constants) => {
                bridge.fireEvent(constants.Events.done, constants.AppResult.error);
            });
        },
        ready: (element, options) => {
            require(['lib/knockout', 'corejs/knockouthelpers', 'legacy/bridge', 'legacy/events', 'oobeprivacysettings-aadc-vm', 'lib/knockout-winjs'], (ko, KoHelpers, bridge, constants, OobePrivacySettingsAadcViewModel) => {
                // Setup knockout customizations
                koHelpers = new KoHelpers();
                koHelpers.registerComponents(CloudExperienceHost.RegisterComponentsScenarioMode.InclusiveOobe);
                window.KoHelpers = KoHelpers;

                // Apply bindings and show the page
                let vm = new OobePrivacySettingsAadcViewModel.OobePrivacySettingsAadcViewModel(this.resourceStrings, this.settingsEntryResourceStrings, this.isOutOfBoxExperienceIncomplete, this.isInternetAvailable);
                ko.applyBindings(vm, document.documentElement);
                KoHelpers.waitForInitialComponentLoadAsync().then(() => {
                    WinJS.Utilities.addClass(document.body, "pageLoaded");
                    bridge.fireEvent(constants.Events.visible, true);
                    KoHelpers.setFocusOnAutofocusElement();
                });
            });
        }
    });
})();
