// KeyMakingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "psat.h"
#include "KeyMakingDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CKeyMakingDlg dialog

#define MAX_CHAR_SIZE	30
#define MIN_FILE_SIZE	1000

CKeyMakingDlg::CKeyMakingDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKeyMakingDlg::IDD, pParent)
{
	m_lMaxSize	= 0;
	m_lCurSize	= 0;
	m_nPrevPer	= 0;
	m_strPath	= _T("파일 리스트 생성 중...");	 
	m_strPer	= _T("0%");
	m_bIsFirst	= TRUE;
	m_bDrawing = FALSE; 
	//{{AFX_DATA_INIT(CKeyMakingDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CKeyMakingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKeyMakingDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	DDX_Control(pDX, IDC_STATIC_PATH, m_staticPath);
	DDX_Control(pDX, IDC_STATIC_PER, m_staticPer);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CKeyMakingDlg, CDialog)
	//{{AFX_MSG_MAP(CKeyMakingDlg)
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeyMakingDlg message handlers

void CKeyMakingDlg::SetMaxSize( unsigned long lMaxSize ) 
{
	m_lMaxSize = lMaxSize;
}

void CKeyMakingDlg::SetCurSize( unsigned long lCurSize ) 
{
	m_lCurSize = lCurSize;

	if ( m_lCurSize && m_lMaxSize ) 
	{
		int nTempPer = m_lCurSize * 100 / m_lMaxSize; 
		if ( m_nPrevPer == nTempPer ) 
		{
			m_nPrevPer = nTempPer;
			return;
		}
		m_nPrevPer = nTempPer;
		m_strPer.Format( "%d%%", m_nPrevPer );
	}

	PrintSize();
	OnPaint();
}

void CKeyMakingDlg::SetCurPath( char* szPath )
{
	if ( strlen ( szPath ) <  MAX_CHAR_SIZE ) 
	{
		m_strPath = (LPSTR)(LPCSTR)szPath; 
	}
	else
	{
		m_strPath = _T("");
		CString strTemp = (LPSTR)(LPCSTR)szPath;
		int i = strTemp.ReverseFind( '\\' ) + 1;

		if ( ( strTemp.GetLength() - i ) > MAX_CHAR_SIZE ) 
		{
			m_strPath.Insert( 0, "..." );	
			m_strPath.Insert( 3, strTemp.Right( MAX_CHAR_SIZE - 3 ) );
		}
		else
		{
			m_strPath.Insert( 0, ( szPath + i ) );
		}
		/*m_strPath.Insert( 0, ( szPath + i ) );
		m_strPath.Insert( i + strlen ( szPath ), "..." );*/
	}
	PrintPath();
}

void CKeyMakingDlg::PrintProgress()
{	
	OnPaint();
	PrintPath();
	PrintSize();
}

void CKeyMakingDlg::PrintPath()
{
	m_staticPath.SetWindowText( m_strPath );
}

void CKeyMakingDlg::PrintSize()
{
	if ( m_lCurSize == 0 ) 
	{
		m_staticPer.SetWindowText( "0%" );
	}
	else
	{
		m_staticPer.SetWindowText( m_strPer );
	}
}

void CKeyMakingDlg::SetDefault()
{
	m_lMaxSize	= 0;
	m_lCurSize	= 0;
	m_nPrevPer	= 0;
	m_strPath	= _T("파일 리스트 생성 중...");	 
	m_strPer	= _T("0%");

	PrintProgress();
}

BOOL CKeyMakingDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
/* ColorKey 설정 */
	ModifyStyleEx ( 0 , WS_EX_LAYERED );
//	TRACE ( "%d %d\n" , GetLastError() , GetWindowLong ( GetSafeHwnd() , GWL_EXSTYLE ) );
	HMODULE h_user32_dll = GetModuleHandle("USER32.DLL"); 
    if(h_user32_dll != NULL){ 
		BOOL (WINAPI *fp_set_layered_window_attributes)(HWND, COLORREF, BYTE, DWORD) = 
			(BOOL (WINAPI *)(HWND, COLORREF, BYTE, DWORD))GetProcAddress(h_user32_dll, 
			"SetLayeredWindowAttributes"); 
		if(fp_set_layered_window_attributes != NULL){ 
			(*fp_set_layered_window_attributes)(m_hWnd, RGB(255, 0, 0), 0, LWA_COLORKEY); 
		} 
    } 
	if ( m_bIsFirst ) 
	{
		m_bmBackground.LoadBitmap ( IDB_KEYMAKINGDLG_BACKGROUND );
		m_bIsFirst = FALSE;
	}
	m_staticPath.SetWindowPos( NULL, 12, 100, 232, 17, NULL );
	m_staticPer.SetWindowPos( NULL, 122, 37, 28, 15, NULL );
	SetWindowPos(&wndTop, m_ptPosition.x, m_ptPosition.y, 256, 125, NULL);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CKeyMakingDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rect;
	GetClientRect ( &rect );
	CDC*	pDC = &dc;
	CDC*	pDrawDC;

	READY_BACKBUFFER ( GetSafeHwnd() , pDC , pDrawDC , RGB ( 255 , 0 , 0 ) );
	{
		DrawBackground ( pDrawDC );
	//	DrawReliability( pDC );
	//	pDrawDC->FillSolidRect( CRect( 13, 62, 14 + ( 228 * m_nPrevPer / 100 ), 72 ), RGB( 164, 255, 112 ) );	
	}
	BLIT_OPAQUE_BACKBUFFER ( pDC , pDrawDC );

}

void CKeyMakingDlg::DrawBackground ( CDC* pDC )
{
	CDC cdc;
	cdc.CreateCompatibleDC ( pDC );

	BITMAP bm;
	CBitmap* pOldBitmap = NULL;

	pOldBitmap = ( CBitmap *)cdc.SelectObject ( &m_bmBackground );
	m_bmBackground.GetBitmap( &bm );
	pDC->BitBlt ( 0 , 0 , bm.bmWidth , bm.bmHeight , &cdc , 0 , 0 , SRCCOPY );
	
	cdc.SelectObject ( pOldBitmap );

	cdc.FillSolidRect( CRect( 13, 62, 14 + ( 228 * m_nPrevPer / 100 ), 72 ), RGB( 164, 255, 112 ) );	
}

void CKeyMakingDlg::SetWindowPosition ( CRect rtPosition )
{
	m_ptPosition.x =  ( rtPosition.left + rtPosition.right ) / 2 - 127;
	m_ptPosition.y =  ( rtPosition.top + rtPosition.bottom ) / 2 - 62;
}

void CKeyMakingDlg::OnTimer(UINT nIDEvent) 
{
	switch ( nIDEvent ) 
	{
	case 0 :
		break;
	default : 
		break;
	}
	
	CDialog::OnTimer(nIDEvent);
}

HBRUSH CKeyMakingDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	if ( nCtlColor == CTLCOLOR_STATIC ) 
	{
		pDC->SetBkMode ( TRANSPARENT );
		return (HBRUSH)(::GetStockObject( WHITE_BRUSH ));
	}
	return hbr;
}

void CKeyMakingDlg::DrawReliability ( CDC* pDC )
{
//	int nRed, nGreen, nBlue;
	//int nRedStep, nGreenStep, nBlueStep;
	int nSize;

	// 게스트 신뢰도 
	nSize = 228 * m_nPrevPer / 100;
	CRect rect = CRect( 13, 62, 14 + nSize, 72);

	//nRed = 164, nGreen = 255, nBlue = 112;
	//nRedStep = MyRound((double)nSize/151), nGreenStep = MyRound((double)nSize/94), nBlueStep = MyRound((double)nSize/112);
//	pDC->FillSolidRect( CRect( 13, 62, 14 + ( 228 * m_nPrevPer / 100 ), 72, RGB( 164, 255, 112 ) );	
/*	for (i = 1; i <= nSize; i++ )
	{
	

		//rect = CRect ( 13 + i, 62, 14 + i, 72 );
		//rect = CRect ( 13, 62, 14, 72 );
		if ( nRedStep != 0 && i % nRedStep == 0 ) 
		{
			nRed--;
		}
		if ( nGreenStep != 0 && i % nGreenStep == 0 ) 
		{
			nGreen--;
		}
		if ( nBlueStep != 0 && i % nBlueStep == 0 ) 
		{
			nBlue--;
		}

	}*/
}

int CKeyMakingDlg::MyRound( double i )
{
	return floor ( i + 0.5 );
}
