// MemberJoinDlg.cpp : implementation file
//

#include "stdafx.h"
#include "psat.h"
#include "MemberJoinDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMemberJoinDlg dialog


CMemberJoinDlg::CMemberJoinDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMemberJoinDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMemberJoinDlg)
	m_strPass1 = _T("");
	m_strPass2 = _T("");
	m_strId = _T("");
	m_strMail = _T("");
	//}}AFX_DATA_INIT
	m_bFirst = TRUE; 
}


void CMemberJoinDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMemberJoinDlg)
	DDX_Control(pDX, IDC_EDIT_PASS2, m_edtPass2);
	DDX_Control(pDX, IDC_EDIT_MAIL, m_edtMail);
	DDX_Control(pDX, IDC_EDIT_PASS1, m_edtPass1);
	DDX_Control(pDX, IDC_EDIT_ID, m_edtId);
	DDX_Control(pDX, IDC_BUTTON_JOIN, m_btnJoin);
	DDX_Control(pDX, IDC_BUTTON_CANCEL, m_btnCancel);
	DDX_Text(pDX, IDC_EDIT_PASS1, m_strPass1);
	DDX_Text(pDX, IDC_EDIT_PASS2, m_strPass2);
	DDX_Text(pDX, IDC_EDIT_ID, m_strId);
	DDX_Text(pDX, IDC_EDIT_MAIL, m_strMail);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMemberJoinDlg, CDialog)
	//{{AFX_MSG_MAP(CMemberJoinDlg)
	ON_BN_CLICKED(IDC_BUTTON_CANCEL, OnButtonCancel)
	ON_BN_CLICKED(IDC_BUTTON_JOIN, OnButtonJoin)
	ON_WM_PAINT()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMemberJoinDlg message handlers

void CMemberJoinDlg::OnButtonCancel() 
{
	m_btnCancel.m_bOver = 0;
	CDialog::OnCancel();	
}

void CMemberJoinDlg::OnButtonJoin() 
{
	UpdateData( TRUE );
	if ( m_strId == "" ) 
	{
		MessageBox( "아이디를 입력해주세요.", "가입" );
		m_edtPass1.SetWindowText("");
		m_edtPass2.SetWindowText("");
		m_edtMail.SetWindowText("");
		m_edtId.SetFocus();
		return;
	}
	else if ( ( m_strPass1 == "" ) || ( m_strPass2 == "" ) )
	{
		MessageBox( "패스워드를 입력해주세요.", "가입" );
		m_edtPass1.SetWindowText("");
		m_edtPass2.SetWindowText("");
		m_edtMail.SetWindowText("");
		m_edtPass1.SetFocus();
		return;
	}
	else if ( m_strMail == "" )
	{
		MessageBox( "메일주소를 입력해주세요.", "가입" );
		m_edtMail.SetWindowText("");
		m_edtMail.SetFocus();
		return;
	}

	if ( m_strPass1 != m_strPass1 ) 
	{
		MessageBox( "패스워드를 다시 확인해주세요.", "가입" );
		return;
	}

	KJoinMemberPacket Packet;
	strncpy ( Packet.m_szId, (LPSTR)(LPCSTR)m_strId , m_strId.GetLength() );
	strncpy ( Packet.m_szPassword, (LPSTR)(LPCSTR)m_strPass1 , m_strPass1.GetLength() );
	strncpy ( Packet.m_szMailAddress, (LPSTR)(LPCSTR)m_strMail , m_strMail.GetLength() );

//	::SendMessage( m_pHwnd, WM_OTHER_DLG_RECEIVED_DATA,
	if ( m_pSocket->IsConnected() )
	{
		m_pSocket->SendData ( &Packet );
	}
	else
	{
		m_pSocket->Connect ( IP_ADDR, PORT_NUM );
		m_Packet = Packet;
		SetTimer ( 1, 5, NULL );
	}
}

BOOL CMemberJoinDlg::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_bFirst ) 
	{
		m_bmBackground.LoadBitmap ( IDB_JOINDLG_BACKGROUND );
		m_btnCancel.LoadBitmaps ( IDB_JOINDLG_BTN_CANCEL_NORMAL , IDB_JOINDLG_BTN_CANCEL_CLICK , IDB_JOINDLG_BTN_CANCEL_NORMAL );
		m_btnJoin.LoadBitmaps ( IDB_JOINDLG_BTN_JOIN_NORMAL , IDB_JOINDLG_BTN_JOIN_CLICK , IDB_JOINDLG_BTN_JOIN_NORMAL );
		m_bFirst = FALSE; 
	}
	m_btnCancel.SetWindowPos ( NULL , 150, 131, 75, 22, NULL );
	m_btnJoin.SetWindowPos ( NULL , 56, 131, 75, 22, NULL );

	m_edtMail.SetWindowPos ( NULL , 84, 104, 169, 21, NULL );
	m_edtPass2.SetWindowPos ( NULL , 84, 81, 169, 21, NULL );
	m_edtPass1.SetWindowPos ( NULL , 84, 58, 169, 21, NULL );
	m_edtId.SetWindowPos ( NULL , 84, 35, 169, 21, NULL );
	
	SetWindowPos(&wndTop, m_ptPosition.x, m_ptPosition.y, 272, 160, NULL);

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

void CMemberJoinDlg::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 );
	}
	BLIT_OPAQUE_BACKBUFFER ( pDC , pDrawDC );
}

void CMemberJoinDlg::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 );
}

void CMemberJoinDlg::SetWindowPosition ( CPoint ptPosition )
{
	m_ptPosition.x = ptPosition.x - 8;
	m_ptPosition.y = ptPosition.y - 17;
}

void CMemberJoinDlg::SetWnd(HWND hwnd)
{
	m_pHwnd = hwnd;
}

void CMemberJoinDlg::SetSocket ( CClientSocket* pClientSocket )
{
	m_pSocket = pClientSocket;
}

void CMemberJoinDlg::OnTimer(UINT nIDEvent) 
{
	m_pSocket->SendData ( &m_Packet );	
	KillTimer ( nIDEvent );
	CDialog::OnTimer(nIDEvent);
}

void CMemberJoinDlg::GiveJoinMemberAck ( int nResult )
{
	if ( nResult == MEMBER_JOIN_SUCCESS ) 
	{
		MessageBox( "가입에 성공하였습니다.", "가입" );
		CDialog::OnOK();
	}
	else
	{
		MessageBox( "가입에 실패하였습니다.", "가입" );
	}
}