#include "httpfile.h"
#include "afxwin.h"

CHTTPFile::CHTTPFile(void)
{
	m_pProgress = NULL;
	//m_pStatic = NULL;
	m_pfProgressPercentage = NULL;
}

CHTTPFile::~CHTTPFile(void)
{
	if( NULL != m_hIntSession )
	{
		InternetCloseHandle( m_hIntSession );
	}

	if( NULL != m_hHTTPSession )
	{
		InternetCloseHandle( m_hHTTPSession );
	}
}

void CHTTPFile::Connect( CString strAddres, CString strDirectory, int nPort, CString strUsername, CString strPassword )
{
	m_hIntSession = InternetOpen( NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL );

	if( m_hIntSession == NULL )
	{
#ifdef _NATION_KR_
		MessageBox( NULL, "네트웍을 사용할수없습니다.", 0, 0 );
#else
		MessageBox( NULL, "Can't use Network.", 0, 0 );
#endif
	}

	CString strURL;
	strURL.Format( "%s%s:%d", strAddres, strDirectory, nPort );	
	
	m_hHTTPSession = InternetOpenUrl( m_hIntSession, strURL, NULL, 0, INTERNET_FLAG_HYPERLINK, 0 );

	if( m_hHTTPSession == NULL )
	{
#ifdef _NATION_KR_
		MessageBox( NULL, "패치서버에 연결할수없습니다.", 0, 0 );
#else
		MessageBox( NULL, "Can't connect Patch Server.", 0, 0 );
#endif
	}	
}

bool CHTTPFile::TransferFile( CString strServerFilename, CString strClientFilename , CString strFilename )
{
	HINTERNET hSession = InternetOpenUrl( m_hIntSession, strServerFilename, NULL, 0, INTERNET_FLAG_HYPERLINK, 0 );

	if( hSession == NULL )
	{	
		CString strError;
#ifdef _NATION_KR_
		strError.Format( " 파일이 존재하지 않습니다. : %s", strServerFilename );
#else
		strError.Format( " Not exist File. : %s", strServerFilename );
#endif
		MessageBox( NULL, strError, 0, 0 );
		return false;
	}

	char httpbuff[1024];

	DWORD dwReadedBytes;

	CString strProgress;	

	FILE *fp = fopen( strClientFilename, "wb" );
	if( NULL == fp )
	{
		return false;
	}

	DWORD dwTotalByte = 0, dwTotalSize = 0;

	
	char szQueryBuf[16];
	DWORD dwQueryBufLen = sizeof(szQueryBuf);
	BOOL bQuery = ::HttpQueryInfo(hSession, HTTP_QUERY_CONTENT_LENGTH, szQueryBuf, &dwQueryBufLen, NULL);
	
	if (bQuery)
	{
		// The query succeeded, specify memory needed for file
		dwTotalSize = (DWORD)atol(szQueryBuf);
	}
	else
	{
		dwTotalSize = 0;
	}
		

	if( fp )
	{
		int nUpdate = 0;
		while( InternetReadFile( hSession, httpbuff, 1024, &dwReadedBytes ) && dwReadedBytes != 0 )
		{
			if( dwReadedBytes == 0 )
			{
				strProgress.Format( " %d / %d Download File = %s", dwTotalByte, dwTotalSize, strFilename );
				m_pInfoWnd->SetWindowText( strProgress );
				m_pInfoWnd->Invalidate( FALSE );

				*m_pfProgressPercentage = 100.0f;
				CRect rect;
				rect.left = 22;
				rect.right = 536;
				rect.top = 405;
				rect.bottom = 440;
				m_pProgress->InvalidateRect( rect, FALSE );

				break;
			}

			dwTotalByte += dwReadedBytes;

			fwrite( httpbuff, dwReadedBytes, 1, fp );

			//if( m_dwTrans )
			//	*m_dwTrans += dwReadedBytes;
			
			
			if( m_pProgress && ( nUpdate % 30 )== 0)
			{
				//m_pProgress->Invalidate();
				//m_pProgress->Invalidate( FALSE );

				*m_pfProgressPercentage = (dwTotalByte*100.0f)/dwTotalSize;

				CRect rect;
				rect.left = 22;
				rect.right = 536;
				rect.top = 405;
				rect.bottom = 440;
				m_pProgress->InvalidateRect( rect, FALSE );

				//strProgress.Format( " %d / %d Download File = %s", *m_dwTrans, dwTotalSize, strFilename );
				strProgress.Format( " %d / %d Download File = %s", dwTotalByte, dwTotalSize, strFilename );
				m_pInfoWnd->SetWindowText( strProgress );
				m_pInfoWnd->Invalidate( FALSE );

			}		
			nUpdate++;
			
//			if( m_pStatic )
//			{
//				strProgress.Format( "Patch Info %d", dwTotalByte );
//				m_pStatic->SetWindowText( strProgress );
//				m_pStatic->Invalidate( FALSE );
//			}
		}
		fclose( fp );
	}	
//	else
//	{
//		AfxMessageBox( strClientFilename );
//	}

	InternetCloseHandle( hSession );

	return true;
}

void CHTTPFile::Init(void)
{	
	m_hIntSession = InternetOpen( NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL );

	if( m_hIntSession == NULL )
	{
		MessageBox( NULL, "Failed to establish network connection.", 0, 0 );
	}	
}
