var HelloIllustrator = function ()
{
    /**
     * Private debug flag for this object container.
     */
    var debugMode = false;

    /**
     * Content URL's for the point product.
     */
    this.url =
    {
        online  : "https://helpx.qa.adobe.com/illustrator/hello/dev/index.html",
        offline : "./offline/index.html"
    }

    /**
     * All the point products seem to have implemented a different event types, so we need this
     * definition here until we can standardize them.
     */
    this.events =   {
                        pipEventType                    : "com.adobe.illustrator.LOGPIP",
                        aboutToCloseEvent               : "com.adobe.illustrator.welcome.aboutToAutomaticallyCloseHello",
                        handleExecuteMenuCommand        : "com.adobe.illustrator.HandleExecuteMenuCommand",
                        handleRecentFileOpen            : "com.adobe.illustrator.welcome.HandleRecentFileOpen",
                        contentTabChangedNotification   : "com.adobe.illustrator.welcome.contentTabChangedNotification",
                        isInContentTutorialNotification : "com.adobe.illustrator.welcome.isInContentTutorialNotification"
                    }
    
    /**
     * UI element definitions.
     */
    this.ui     =   {
                        create  :   {
                                        initialImage    : "images/AiCreateInitialExp.jpg",
                                        backgroundColor : "#231f20"
                                    },
                        footer  :   {
                                        helpURL         : "https://helpx.adobe.com/illustrator.html",
                                        helpLabel       : "Illustrator Help",
                                        forumsURL       : "https://forums.adobe.com/community/illustrator",
                                        forumsLabel     : "Forums",
                                        inspireURL      : "https://www.behance.net/search?field=48&sort=featured_date&time=all",
                                        inspireLabel    : "Inspire",
                                        blogURL         : "http://blogs.adobe.com/adobeillustrator",
                                        blogLabel       : "Blog"
                                    }
                    }

    /**
     * Another workaround for merging the different PP extensions into one. Although we have
     * a different extension ID, the host app expects the following one in order for it's 
     * various pieces to work correctly.
     */
    this.overrideExtensionID = "com.illustrator.Welcome.extension1";

    /**
     * Get the Hello JSON data corresponding to the currently logged in user.
     *
     * @param callback      function object which will be called on data generation completion
     */
    HelloIllustrator.prototype.getUserJsonData = function( callback )
    {
        try
        {
            window.__adobe_hello.evalScript( "app.getHelloJSONData()", callback);
        }
        catch( e )
        {
            window.__adobe_hello.logMessage( "HelloIllustrator.getUserJsonData catch: " + e.message );
        }
    }
    
    /**
     * Opens an existing document.  If the specified path is the constant string
     * "open" then it will open the browse/open file UI in the host application.
     *
     * @param fullPath      string object containing the path or ID of the existing
     *                      document to open
     */
    HelloIllustrator.prototype.openExistingDocument = function( fullPath )
    {
        window.__adobe_hello.sendAboutToCloseEvents();
        
        if ( fullPath == "open" )
        {
            window.__adobe_hello.sendExecuteMenuCommandEvent( "open" );
        }
        else
        {
            window.__adobe_hello.sendRecentFileOpenEvent( fullPath );
        }
        
        window.__adobe_hello.logHelloInteractionPIPEvent( "OpenExistingDoc" );
        window.__adobe_hello.closeHello();
    }
    
    /**
     * Create a new document of the specified type by showing the appropriate new document
     * UI.  If the 'suppressNewDocUI' is true then the document created with default params
     * and the new document UI is not shown.
     *
     * @param documentType          string object containing the ID of the new doc type to
     *                              create
     * @param suppressNewDocUI      boolean value indicating true to not show the new doc UI,
     *                              false otherwise
     */
    HelloIllustrator.prototype.openDocumentOfType = function( documentType, suppressNewDocUI )
    {
    	window.__adobe_hello.sendAboutToCloseEvents();

        if ( documentType == "From Template" )
        {
            sendExecuteMenuCommandEvent( "newFromTemplate" );
        }
        else
        {
            var trueOrFalse = suppressNewDocUI ? "false" : "true";
            var script      = "app.documents.addDocumentWithDialogOption(\""+documentType+"\"," +  trueOrFalse + ");";
            
            function callback(result) { }
            
            window.__adobe_hello.evalScript( script, callback );
        }
        
        window.__adobe_hello.logHelloInteractionPIPEvent( "CreateNewDoc:"+documentType );
        window.__adobe_hello.closeHello();
    }
    
    /**
     * Sets the "Don't Show Again" preference in the host application.
     *
     * @param dontShowAgain     boolean value indicating true if "dont show again" was
     *                          requested, false otherwise
     */
    HelloIllustrator.prototype.setDontShowAgainPreference = function( dontShowAgain )
    {
        window.__adobe_hello.logHelloInteractionPIPEvent( ( dontShowAgain ) ? "SetDoNotShowAgain" : "ClearDoNotShowAgain" );

        var trueOrFalse = dontShowAgain ? "true" : "false";
        var script      = "app.preferences.setBooleanPreference(\"" + "Hello/DontShowAgainPrefKey_Ver18_1" +"\"," + trueOrFalse + ");";
        
        function callback(result) { }
        
        window.__adobe_hello.evalScript( script, callback );
    }
    
    /**
     * Attempt to reload the Hello URL from the server.
     */
    HelloIllustrator.prototype.retryLoadFromServer = function()
    {
    	window.__adobe_hello.logHelloInteractionPIPEvent( "RetryPageLoad" );
        
        if (window.navigator.onLine === true)
        {
            window.location = window.__adobe_hello.getHelloURL();
        }
    }
    
    /**
     * Auto-close the Hello UI interface.
     */
    HelloIllustrator.prototype.notifyDoneWithDialog = function()
    {
        if ( window.__adobe_cep__ )
        {
            window.__adobe_hello.logHelloInteractionPIPEvent( "DialogAutoDismiss" );
            window.__adobe_cep__.closeExtension();
        }
    }
    
    /**
     * Indicate that the requested URL failed to load.
     *
     * @param errorString       string object containing the error description
     */
    HelloIllustrator.prototype.notifyFailedToLoad = function( errorString )
    {
        if ( errorString )
            window.__adobe_hello.logPIPEvent( "ProjectHello", "Failure", errorString );
    }
    
    /**
     * Send a notification message to the host application indicating the content has entered
     * a tutorial.
     *
     * @param isInContentTutorial   boolean value indicating true if content is tutorial, false otherwise
     */
    HelloIllustrator.prototype.notifyInContentTutorial = function( isInContentTutorial )
    {
        window.__adobe_hello.sendEvent( this.events.isInContentTutorialNotification, (isInContentTutorial ? "Yes" : "No"));
    }
    
    /**
     * Send a notification message to the host application indicating the conent tab has changed.
     *
     * @param currentTab        string object containing the current tab's ID
     */
    HelloIllustrator.prototype.notifyContentTabChanged = function( currentTab )
    {
        window.__adobe_hello.sendEvent( this.events.contentTabChangedNotification, currentTab);
    }
};
