﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="BadBug.debug.js" />
/// <reference path="BadBug.Security.Cryptography.debug.js" />
/// <reference path="User.IdentityAuthentication.js" />

if( !window.__CurrentUser ) window.__CurrentUser = { username: "Guest", name: "Guest", isAuthenticated: false };

window._User = function(){
    window._User.initializeBase( this );
    this._username = null;
    this._passwordKey = null;
    this._sessionKey = null;
    this._callBack = null;
    this._localVersion = 0;
    this._keepAliveTimer = 0;
};

window._User.prototype = {
    initialize: function(){
        window._User.callBaseMethod( this, "initialize" );    
    },
    dispose: function(){
        window._User.callBaseMethod( this, "dispose" );
    }    ,
    get_username: function(){
        return this._username;
    },
    get_callBack: function(){
        return this._callBack;
    },
    get_isAuthenticated: function(){
        return window.__CurrentUser.isAuthenticated;
    },
    signIn: function( username, password, callBack ){
        this._callBack = callBack;
        this._username = username;
        var utf8Bytes = password.toUTF8Bytes();
        var md5Bytes = BadBug.Security.Cryptography.MD5.compute( utf8Bytes, 0, utf8Bytes.length );
        this._passwordKey = BadBug.Security.Cryptography.MD5.compute( md5Bytes, 0, md5Bytes.length );
        this._getLoginToken();               
    },
    signOut: function(){
        EGovernment.Web.User.IdentityAuthentication.SignOut();
    },
    keepAlive: function(){
        EGovernment.Web.User.IdentityAuthentication.KeepAlive( this._localVersion, function( result, context ){
            context._keepAliveTimer = setTimeout( function(){ context.keepAlive(); }, 10000 );     
        },
        function( error, context ){
        
        },
        this );        
    },
    _getLoginToken: function(){
        var randBytes = [];
        var randBytesCount = ( Math.round( Math.random() * 1000000 ) % 5 ) + 5;
        for( var i = 0; i < randBytesCount; i ++ ) randBytes.push( Math.round( Math.random() * 1000000 ) % 256 );
        var passwordData = BadBug.Security.Cryptography.QQCrypt.encode( this._passwordKey, randBytes, 0, randBytes.length );
        Sys.Net.WebServiceProxy.invoke( "../User/IdentityAuthentication.asmx",
        "GetLoginToken",
        false, {
            username: this._username,
            passwordData: passwordData
        },
        function( result, context ){
            var _isOK = false;
            var msg = "";
            if( result != null )
            {
                var _body = BadBug.Security.Cryptography.QQCrypt.decode( context._passwordKey, result );
                if( _body == null )
                {
                    var emptyKey = new Array( 16 );
                    for( var i = 0; i < 16; i ++ ) emptyKey[ i ] = 0;
                    _body = BadBug.Security.Cryptography.QQCrypt.decode( emptyKey, result );
                    if( _body == null || _body.length < 16 )
                    {
                        msg = "未知错误。";
                    }
                    else
                    {
                        msg = String.fromUTF8Bytes( _body, 16 );
                    }                        
                }
                else if( _body.length >= 16 )
                {
                    if( _body.length > 16 ) msg = String.fromUTF8Bytes( _body, 16 );
                    var zeroCount = 0;
                    for( var i = 0; i < 16; i ++ )
                    {
                        if( _body[ i ] == 0 ) zeroCount ++;
                    }
                    _isOK = zeroCount < 16;
                    if( _isOK ) context._sessionKey = _body.slice( 0, 16 );
                }
                else
                {
                    msg = "未知错误。";
                }
            }
            else
            {
                msg = "用户名或者密码无效。";
            }
            if( !_isOK )
            {
                var callBack = context.get_callBack();
                if( callBack ) callBack( 1, false, msg );
            }
            else
            {                    
                context._verify();
            }
        },
        function( error, context ){
            var callBack = context.get_callBack();
            if( callBack ) callBack( 1, false, error.get_message() );
        },
        this );
    },
    _verify: function(){
        var passwordData = BadBug.Security.Cryptography.QQCrypt.encode( this._passwordKey, this._sessionKey, 0, this._sessionKey.length );
        Sys.Net.WebServiceProxy.invoke( "../User/IdentityAuthentication.asmx",
        "Verify",
        false, {
            username: this._username,
            passwordData: passwordData
        },
        function( result, context ){
            var _body = BadBug.Security.Cryptography.QQCrypt.decode( context._sessionKey, result );
            var _isOK = false;
            if( _body != null )
            {
                if( _body.length > 16 )
                {
                    _isOK = _body[ 16 ] == 1;
                    if( _body.length > 17 )
                    {
                        msg = String.fromUTF8Bytes( _body, 17 );
                        context._sessionKey = _body.slice( 0, 16 );
                    }
                }
            }
            else
            {
                msg = "会话密钥不正确。";
            }
            if( _isOK )
            {
                
            }
            var callBack = context.get_callBack();
            if( callBack ) callBack( 2, _isOK, msg );
        },
        function( error ){
            var callBack = context.get_callBack();
            if( callBack ) callBack( 2, false, error.get_message() );
        },
        this );
    }
};

window._User.registerClass( "window._User", Sys.Component );
window.User = new window._User();

if ( typeof(Sys) !== "undefined" ) Sys.Application.notifyScriptLoaded();


