// RYLLoginDlg.h : header file
//

#pragma once

#include "MSGDialog.h"
#include "afxwin.h"

#include "ZBitmapButton.h"
#include "CISBitmap.h"
#include "GradientStatic.h"
#include "DlgServerSelect.h"

#include <Network/clientsocket/LoginSocket.h>
#include <Network/Packet/PacketStruct/ServerInfo.h>

#include "UpdateManager.h"

#include "memdc.h"
#include "SplashDlg.h"

#include "RYLLOGIN_DEFINITIONS.H"

#include "tranlistbox.h"

#include <list>

// forward decl.
class ISoundObject;

enum ConnectionState
{
	CS_IDLE,
	CS_START,
	CS_FILEINFOGATHERING,
	CS_PATCHDOWN,
	CS_DECOMPRESS,
	CS_SERVERSELECT,
};

class ILoginDlgCommand
{
public:

    virtual ~ILoginDlgCommand() { }
    
    virtual bool IsProcessNow(DWORD dwCurrentTime) { return true; }
    virtual bool ProcessCommand() = 0;
};


// CRYLLoginDlg dialog
class CRYLLoginDlg : public CDialog
{
// Construction
public:
	
	CRYLLoginDlg(CWnd* pParent = NULL);

	enum { IDD = IDD_RYLLOGIN_DIALOG };

protected:

    // DDX/DDV support
	virtual void DoDataExchange(CDataExchange* pDX);

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();

	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()

	afx_msg void OnBnClickedButtonNewacc();
	afx_msg void OnBnClickedButtonPsw();
	afx_msg void OnBnClickedButtonEnd();
	afx_msg void OnMove(int x, int y);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);	
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnBnClickedButtonMusic();
	afx_msg void OnBnClickedButtonFilecheck();
	afx_msg void OnBnClickedButtonSetup();
	afx_msg LRESULT OnRylLauncherShutdown(WPARAM,LPARAM);
	afx_msg void OnDestroy();
	afx_msg void OnBnClickedButtonStart();
	afx_msg void OnLbnSelchangeListServer();
	afx_msg void OnLbnDblclkListServer();
	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);

public:	

	bool IsServerClosed() { return m_bServerClosed; }
    
	void RenderBack( CDC &rDC, CBitmap &rBitmap, int Px, int Py, int SizeX = -1 , int SizeY = -1 );
	void RenderAni( CDC &rDC, CBitmap &rBitmap, int Px, int Py , int SizeX, int SizeY, int nAniNum );

	void PlayBGM(void);
	void InitClientFoloder(void);
	void InitInterface(void);
	void InitWebBrowser(void);
	bool UpdateFile(bool bFileCheck = false);	// '파일오류검사'시에는 파라미터가 true
	
	void Execute(CString strExec, BOOL bWait);
	void ExecuteClient(int nServer);
		
	DWORD GetLocalClientVersion();
	void SetClientVer(DWORD dwVer);

    DWORD WaitForWebPageCreated(DWORD dwMilliseconds = INFINITE) const 
    { return WaitForSingleObject(m_hWebPageCreated, dwMilliseconds); }

    void SetLoginDisplayText(const char* szText, bool bInvalidate = true)
    { 
        m_stcLoginDisplay.SetWindowText(szText);
        if (bInvalidate) { m_stcLoginDisplay.Invalidate(); }
    }

    void SetDefaultLoginServerAddress(const char* szLoginAddress) 
    { m_strDefaultLoginServerAddr.SetString(szLoginAddress); }

    bool ConnectToLoginServer();
    bool RequestServerList();
    bool ProcessServerList(DWORD dwClientVersionFromServer, 
        const char* szPatchAddressFromLoginServer, const SERVER_LIST& serverList);

    LoginSocket* GetLoginSocket() { return m_pLoginSocket; }

    void PutCommand(ILoginDlgCommand* lpLoginDlgCommand) { m_LoginDlgCommandList.push_back(lpLoginDlgCommand); }

	// 패치가 완료가 되었는지 확인.

	bool GetPatchComplete()						{ return m_bPatchComplete;				}
	void SetPatchComplete(bool bPatchComplete)	{ m_bPatchComplete = bPatchComplete;	}

private:

    typedef std::list<ILoginDlgCommand*> LoginDlgCommandList;

    LoginDlgCommandList m_LoginDlgCommandList;

    // Graphic variables
	CZBitmapButton  m_btnNewAcc;
	CZBitmapButton  m_btnPsw;
	CZBitmapButton  m_btnSetup;
	CZBitmapButton  m_btnFileCheck;
	CZBitmapButton  m_btnMusic;
	CZBitmapButton  m_btnEnd;
	CZBitmapButton	m_btnServerStart;

	CGradientStatic m_stcLoginDisplay;
	CGradientStatic m_stcVersionView;

	CTranListBox	m_ServerList;
	
	CHtmlCtrl		m_WebPage;	
	CCISBitmap		m_btmBase;
	CBitmap			m_btmFILE;
	CBitmap			m_btmFTP;
	CBitmap			m_btmPatching;
	CBitmap			m_btmComplete;
	CBitmap			m_btmLoging;
	CBitmap			m_btmAlarmAni;

	CDC				m_memDC;
	CDC				m_BkDC;
	CBitmap			m_BkBitmap;	
	CSplashDlg		m_dlgSplash;
    CRect			m_rt;
    HICON           m_hIcon;

#ifdef _TW_
	CMSGDialog*		m_TMsgDlg;
#endif

    // String variable
    CString			m_strRYLFolder;
	CString			m_strLoginServerAddrFromRegistry;
	CString			m_strPatchUrlFromRegistry;
	CString			m_strDefaultLoginServerAddr;
    CString         m_strPatchUrlFromLoginServer;

    CUpdateManager	m_UpdateManager;

    CFont               *m_pBoldFont;
	ISoundObject	    *m_pStreamSound;
    LoginSocket		    *m_pLoginSocket;
	CBitmap			    *m_pOldBitmap;    

    const char          *m_szVersionSignature;

	SERVER_LIST		m_ServerListData;
    DEVMODE			m_DeviceMode;

    HANDLE          m_hWebPageCreated;

	volatile ConnectionState	m_ConnectionState;
    
	float	m_fBlueProgressPercentage;
	float	m_fRedProgressPercentage;

    DWORD   m_dwLocalClientVersion;
    DWORD   m_dwServerClientVersion;

	int		m_nLoginProcess;
	int		m_nFade;

	int		m_nUpdateAni;
	int		m_nUpdateLogoAni;

	int		m_nShowRateServerSelectDlg;		
	int		m_nServerClass;
	
	bool    m_bFileCheck;	        // file check 일 경우 종료시 종료표시 다이얼로그 박스 띄움
    bool    m_bServerClosed;
    bool    m_bPatchComplete;

    bool	m_bCheckSetup;
	bool	m_bCheckSucLoginData;

	bool	m_bBGMPlay;
	bool	m_bUpdating;
    bool    m_bShowServerSelectDlg;
};
