#include "StdAfx.h"
#include "Global.h"
#include "LoginForm.h"
#include <stdio.h>
#include "DotNetString.h"
//Environment::MachineName

using namespace ObjDataEdit;

void LoginForm::LoginForm_LoadIni()
{
	TB_Account->Text = Global::GetIniString( "Account" );

	char* Pwd = Global::GetIniString( "Password" );
	std::string MachineName = DotNetStr::StringToChar( Environment::MachineName );
	Pwd = Global::DecodePassword( Pwd , MachineName.c_str() );
	TB_PassWord->Text = Pwd;
}

void LoginForm::LoginFormSaveIni( )
{
	char*	Path = (char*)Global::IniRootPath.c_str(); 
	std::string	Txt;

	Txt = DotNetStr::StringToChar( TB_Account->Text );
    WritePrivateProfileString( "BOOT" , "Account", Txt.c_str() , Path );

	Txt = DotNetStr::StringToChar( TB_PassWord->Text );	
	Txt = Global::EncodePassword( Txt.c_str() , DotNetStr::StringToChar( Environment::MachineName ).c_str() );

    WritePrivateProfileString( "BOOT" , "Password", Txt.c_str() , Path );

    this->Close();
}

bool LoginForm::CheckAccountInfo( )
{

    SqlDataReader* myReader;

    sqlCommand->CommandText = String::Format( "Select Password , Admin , ValueTable , AddPower , Armor , BodyObj , Player , Item , KeyItem , Magic , QuestCollect , QuestDetail , Rune , Title , Weapon , NPC , Image , Flag , QuestNPC , Recipe , Script , LearnMagic , Shop , Suit, PE From AccountDB WHERE Account_ID = '{0}'" , Account() );
    myReader = sqlCommand->ExecuteReader();        

    if( myReader->Read() == false ) 
    {
        myReader->Close();
        return false;
    }

    //if( myReader->Read() ) 
    {

        String* Str =  myReader->GetString( 0 ) ;
        if( String::Compare( Str , Password() ) != 0 )
        {
            myReader->Close();
            return false;
        }

        Str =  myReader->GetString( 1 );
        if( String::Compare( Str , "¬O" ) == 0 )
        {
            Global::AccountInfo.Admin       = EM_AccountState_Write;
            for( int i = 0 ; i < 40 ; i++ )
            {
                Global::AccountInfo.ArrList[i] = EM_AccountState_Write;
            }
            return true;
        }
        else
            Global::AccountInfo.Admin       = EM_AccountState_None;
        
        for( int i = 2 ; i < myReader->FieldCount ; i++ )
        {
            Str =  myReader->GetString( i );
            if( String::Compare( "¥iÅª¼g" , Str ) == 0 )
            {
                Global::AccountInfo.ArrList[ i - 1 ] = EM_AccountState_Write;
            }
            else if( String::Compare( "¥iÅª" , Str  ) == 0 )
            {
                Global::AccountInfo.ArrList[ i - 1 ] = EM_AccountState_Read;
            }
            else
                Global::AccountInfo.ArrList[ i - 1 ] = EM_AccountState_None;
        }      
    }

    myReader->Close();

    return true;
}