// File: $callWave/web/web site/htdocs/widgets/sms/resources/js/AutoLoginView.js
// Desc: View-controller class for auto login screen.
// $Revision: 5$
// $Date: 7/24/2007 11:18:37 PM$
// $Author: Donnie Tognazzini$

// Constructor ---------------------------------------------------------------------------------------------
function SMS_GadgetAutoLoginView(parent)
{
    // Attributes
    this.m_viewController = parent;
    this.m_timer = null;
    this.m_autoConnectCanceled = false;
    this.m_accountHandler = new ACC_AccountHandler(parent.m_source, parent.m_ver, parent.m_os);

    var self = this;

    // UI-Controller Callbacks
    this.m_onGetAccountInfoSuccess = null;

    // ACC_AccountHandler Callback Handlers
    this.m_accountHandler.m_onGetAccountInfoSucceeded = function(responseMessage) {self.onGetAccountInfoSucceeded(responseMessage);};
    this.m_accountHandler.m_onGetAccountInfoFailed = function(statusMessage) {self.onGetAccountInfoFailed(statusMessage);}
}

// Prototype -----------------------------------------------------------------------------------------------
SMS_GadgetAutoLoginView.prototype = new SMS_View;

SMS_GadgetAutoLoginView.prototype.ELEM_ID_VIEWFRAME = 'AUTOLOGIN_ViewFrame';

SMS_GadgetAutoLoginView.prototype.LOGIN_RETRY_INTERVAL_SECONDS = 15;

SMS_GadgetAutoLoginView.prototype.init = function()
{

}

SMS_GadgetAutoLoginView.prototype.display = function()
{
    this.m_timer = null;
    this.m_autoConnectCanceled = false;

    this.displayElement(this.ELEM_ID_VIEWFRAME);

    // Issue a GETA request to authenticate the stored credentials.
    this.m_accountHandler.getAccountInfo( VM_GetPreference( PREF_KEY_ACCOUNT ),
                                          VM_GetPreference( PREF_KEY_U_STRING ) );
}

SMS_GadgetAutoLoginView.prototype.hide = function()
{
    this.hideElement(this.ELEM_ID_VIEWFRAME);
}

SMS_GadgetAutoLoginView.prototype.lnkStopLogin_onClick = function()
{
    this.m_autoConnectCanceled = true;

    // Cancel the GETA request
    this.m_accountHandler.StopGetAccountInfoRequest();

    if (this.m_timer)
    {
        clearTimeout(this.m_timer);
        this.m_timer = null;
    }

    // Notify the application.
    this.m_viewController.cancelAutoConnect();
}

SMS_GadgetAutoLoginView.prototype.onGetAccountInfoSucceeded = function(responseMessage)
{
    try
    {
        // Bail if auto-connect has been canceled.
        if (this.m_autoConnectCanceled)
        {
            return;
        }

        // Success?  If not, then re-try.
        if (responseMessage.m_status == MSG_ResponseStatus.SUCCESS)
        {
            // Callback to view controller.
            this.m_onGetAccountInfoSuccess(responseMessage);
        }
        else
        {
            this.retry();
        }
    }
    catch (e)
    {
        // Notify the application.
        this.m_viewController.cancelAutoConnect(e);
    }
}

SMS_GadgetAutoLoginView.prototype.onGetAccountInfoFailed = function(statusMessage)
{
    try
    {
        // Keep re-trying on failure.  The user can stop this process.
        this.retry();
    }
    catch (e)
    {
         // Notify the application.
        this.m_viewController.cancelAutoConnect(e);
    }
}

SMS_GadgetAutoLoginView.prototype.retry = function()
{
     // Bail if auto-connect has been canceled.
    if (this.m_autoConnectCanceled)
    {
        clearTimeout(this.m_timer);
        this.m_timer = null;
        return;
    }

    // Keep executing until we connect successfully or until the user stops the auto-connect process.
    var self = this;

    // Create callback function.
    var timeoutCallback = function()
    {
        self.setStatusMsgInfo('Re-trying...');
        clearTimeout(self.m_timer);
        self.m_timer = null;
        self.m_accountHandler.getAccountInfo( VM_GetPreference( PREF_KEY_ACCOUNT ), VM_GetPreference( PREF_KEY_U_STRING ) );
    };

    // Re-start the timer.
    this.m_timer = setTimeout(timeoutCallback, this.LOGIN_RETRY_INTERVAL_SECONDS * 1000);
}