
// TouchBoardDlg.h : 헤더 파일
//

#pragma once

#include "TouchSetupDlg.h"
#include "TouchButton.h"
#include "LogEdit.h"
#include "afxwin.h"
#include "ColorButton.h"
#include "RoundButton2.h"
#include "RoundButtonStyle.h"

#undef PSAPI_VERSION
#define PSAPI_VERSION 1

#include <Psapi.h>
#pragma comment(lib, "psapi.lib")

#define USE_ROUND_BUTTON2		1
//#define USE_DO_PACKET_DUMP		1
//#define USE_PACKET_DUMP_FILE		1
//#define USE_DUMMY_DATA_FOR_UI	1

#define TBD_SETTING_FILE			"tbd.ini"
#define TBD_CLIENT_DEFAULT_PORT		17080
#define TBD_RECV_BUFSIZE			(1024 * 512)
#define TBD_SEND_BUFSIZE			(1024 * 512)
#define TBD_STANDARD_WIDTH			1280
#define TBD_STANDARD_HEIGHT			1024

#if TBD_BUTTON_UI_STYLE == 2
#define TBD_BTN_WIDTH			152
#define TBD_BTN_HEIGHT			45
#define TBD_BTN_ICON_WIDTH		30
#define TBD_BTN_ICON_HEIGHT		45
#else
#define TBD_BTN_WIDTH			152
#define TBD_BTN_HEIGHT			45
#define TBD_BTN_ICON_WIDTH		30
#define TBD_BTN_ICON_HEIGHT		45
//#define TBD_BTN_HEIGHT			60
//#define TBD_BTN_ICON_WIDTH		15
//#define TBD_BTN_ICON_HEIGHT		15
#endif


#if TBD_BUTTON_UI_STYLE == 2
#define TBD_BTN_SUB_MARGIN_X	6
#define TBD_BTN_SUB_MARGIN_Y	6
#if USE_ROUND_BUTTON2
#define TBD_BTN_GROUP_START_X	206
#else
#define TBD_BTN_GROUP_START_X	235
#endif
#define TBD_BTN_GROUP_START_Y	95

#define TBD_BTN_SUB_START_X		210
#if USE_ROUND_BUTTON2
#define TBD_BTN_SUB_START_Y		230
#else
#define TBD_BTN_SUB_START_Y		220
#endif
#define TBD_BTN_SUB_COLUMN_SIZE	5
#define TBD_BTN_GROUP_MARGIN_Y	12
#else
#define TBD_BTN_SUB_MARGIN_X	8
#define TBD_BTN_SUB_MARGIN_Y	8
#define TBD_BTN_GROUP_START_X	240
#define TBD_BTN_GROUP_START_Y	110
#define TBD_BTN_SUB_START_X		210
#define TBD_BTN_SUB_START_Y		260
#define TBD_BTN_SUB_COLUMN_SIZE	5
#define TBD_BTN_GROUP_MARGIN_Y	20
#endif

#define TBD_BTN_GROUP_LINE_BASE_COLOR	RGB(150, 150, 150)
#define TBD_BTN_GROUP_LINE_CHECK_COLOR	RGB(200, 0, 0)

//#define ACT_BTN_REGION_BACK_COLOR		RGB(0, 50, 150)
//#define ACT_BTN_REGION_BACK_COLOR		RGB(20, 40, 65)
#define ACT_BTN_REGION_BACK_COLOR		RGB(58, 58, 67)
#define STATUS_BTN_GOOD_BACK_COLOR		RGB(57, 119, 206)

#define TOPBAR_BACK_COLOR				RGB(58, 58, 67)
#define GLOBAL_BACK_COLOR				RGB(232, 242, 247)

#define TBD_MAX_STATION_COUNT	1000
#define TBD_MAX_GROUP_COUNT		30

#define TBD_SERVER_COMMAND_STATION_INFORM		10000
#define TBD_SERVER_COMMAND_STATION_NOTIFY		10001
#define TBD_SERVER_COMMAND_CAST_STATUS			10002
#define TBD_SERVER_COMMAND_DEVICE_STATUS		10003

#define TBD_REQUEST_CAST_TYPE_ALL				100
#define TBD_REQUEST_CAST_TYPE_FIRE				101
#define TBD_REQUEST_CAST_TYPE_RESQUE			102
#define TBD_REQUEST_CAST_TYPE_EMERGENCY			103
#define TBD_REQUEST_CAST_TYPE_GENERAL			104
#define TBD_REQUEST_CAST_TYPE_WHOLE				105

#define TBD_TIMER_EVENT_SOCKET					1000

typedef struct _tMonitor
{
	int nWidth;					// Width
	int nHeight;				// Height
	int nWidthVirtual;			// Width Virtual
	int nHeightVirtual;			// Height Virtual
	int nBitPerPixel;			// BitPerPixel
	int nRefresh;				// Refresh
	int nMonitor;				// Monitors
	int nMegaPixel;				// MegaPixel
	BOOL bSameDisplayFormat;	// SameDisplayFormat
	RECT rcVirtual;
	CArray< MONITORINFOEX, MONITORINFOEX > aryMonitors;
} TMONITOR;

class CStationInfo 
{
public:
	int mGroupIdx;
	int mSubGroupId;
    int mWardId;			// 서ID
	int mUpWardId;			// 상위 스테이션 ID
    int mStationId;			// 스테이션 ID
	int mLevel;			
	int mSubCount;			// 하위 스테이션 개수

	int mCallerNo;			// 연결중인 접수대 번호
	int mCallerCviNo;		// 연결중인 CT CVI 번호

	int mNickIndex;			// 터치 그룹 정렬 인덱스
    boolean mIsHeader;
    CString mNickName;
    CString mIpAddr;

	int mBtnPosX;
	int mBtnPosY;
	int mBtnGroupRow;		// added by ritoseo - 2022-02-03
	int mBtnShiftRow;		// added by ritoseo - 2022-02-03
	boolean mGrouping;
	RECT mGroupBox;
	CTouchButton* m_pButton;
};


// CTouchBoardDlg 대화 상자
class CTouchBoardDlg : public CDialogEx
{
// 생성입니다.
public:
	CTouchBoardDlg(CWnd* pParent = NULL);	// 표준 생성자입니다.

// 대화 상자 데이터입니다.
	enum { IDD = IDD_TOUCHBOARD_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 지원입니다.


// 구현입니다.
protected:
	HICON m_hIcon;

	CString m_strServerAddr;
	CString m_strESAddr;
	CString m_strPSAddr;
	SOCKET m_sock;
	INT32 m_port;
	TMONITOR m_infoMonitor;
	CString m_strDeviceName;
	CString m_strCviNo;
	CString m_strWardId;
	CMutex m_infoMutex;

	char m_sendBuffer[TBD_SEND_BUFSIZE];
	char m_recvBuffer[TBD_RECV_BUFSIZE];
	INT32 m_sendSize;
	INT32 m_recvSize;
	INT32 m_iRequestWaitFlag;
	BOOL m_bIsActive;
	CMutex m_sockMutex;
	CWinThread *m_pReceiveThread;
	CWinThread *m_pPingThread;
	CTouchSetupDlg m_setupDlg;

	CxImage m_ciSymbol;
	CxImage m_ciBackground;
	CDC* m_pMemDC;
	CBitmap* m_pDCBitmap;
	CPen* m_pDidLinePen;

	INT32 m_iSelectGroup;
	CStationInfo *m_pStationList[TBD_MAX_STATION_COUNT];
	INT32 m_stationCount;

	INT32 m_iGroupBtnCount;
	CTouchButton *m_pGroupBtn[TBD_MAX_GROUP_COUNT];

	INT32 m_iScreenWidth;
	INT32 m_iScreenHeight;
	BOOL m_bIsFullHD;

	// 생성된 메시지 맵 함수
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()

	void WideToMultiByte(const wchar_t *src, char* dest);
	void MultiByteToWide(const char* src, wchar_t *dest, int buffersize);
	in_addr* atoaddr(char *address);
	SOCKET ConnectRequest(char *pAddress, int port);
	INT32 SocketSend(char *sendData, int datasize);
	INT32 PacketDizest(char *pBuffer);
	int GetMonitorInformation(void);
	void UpdateSettingToVariable();
	void UpdateSubGroupList();
	void UpdateTouchButtonList();
	void CalcButtonPositioning();
	void RequestCastOrder(INT32 iCastType);
	ULONG ProcIDFromWnd(HWND hwnd);
	HWND GetWinHandle(ULONG pid);
	void PrintProcessNameAndID(DWORD processID, CString& strPath);
	BOOL EnumProcessAndFocus(CString processPath);
	void MoveWindowDirection(CWnd *pWnd, INT32 xpos, INT32 ypos);

	static UINT SocketReceiveThread(LPVOID aParam);
	static UINT SocketConnectionThread(LPVOID aParam);
	static UINT PingCheckThread(LPVOID aParam);
public:
	CFont m_titleFont;
	CFont m_topFont;
	CFont m_castFont;
	CFont m_btnFont;
	CFont m_btnSmallFont;
	CFont m_logFont;
	CFont m_btnUnderFont;
	CLogEdit m_logEdit;

#if USE_ROUND_BUTTON2
	CRoundButtonStyle m_tButtonStyle;
	CRoundButtonStyle m_tTopStyle[5];
	CRoundButtonStyle m_tActStyle[8];
	CRoundButton2 *m_pTopButton[5];
	CRoundButton2 *m_pActButton[8];
#else
	CColorButton *m_pTopButton[5];
	CColorButton *m_pActButton[8];
#endif
	CColorButton *m_pStatusButton[3];

	CString getStrWardId() {
		CString strResult;
		m_infoMutex.Lock();
		strResult = m_strWardId;
		m_infoMutex.Unlock();

		return strResult;
	}

	CString getStrDeviceName() {
		CString strResult;
		m_infoMutex.Lock();
		strResult = m_strDeviceName;
		m_infoMutex.Unlock();

		return strResult;
	}

	INT32 SocketSendExceptionHandle(char *sendData, int datasize);

	afx_msg void OnBnClickedButtonSetup();
	afx_msg void OnDestroy();
	afx_msg LRESULT OnTbdSetupSave(WPARAM wParam, LPARAM lParam);
	afx_msg LRESULT OnTbdUpdateLevel2(WPARAM wParam, LPARAM lParam);
	afx_msg LRESULT OnTbdLevel2Push(WPARAM wParam, LPARAM lParam);
	afx_msg LRESULT OnTbdLogUpdate(WPARAM wParam, LPARAM lParam);
	afx_msg LRESULT OnTbdSubGrouping(WPARAM wParam, LPARAM lParam);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);

	afx_msg void OnBnClickedButtonCastAll();
	afx_msg void OnBnClickedButtonCastFire();
	afx_msg void OnBnClickedButtonCastRescue();
	afx_msg void OnBnClickedButtonCastEmergency();
	afx_msg void OnBnClickedButtonCastNormal();
	afx_msg void OnBnClickedButtonRestore();
	afx_msg void OnBnClickedButtonWireless();
	afx_msg void OnBnClickedButtonCall();
	afx_msg void OnClose();
	afx_msg void OnBnClickedButtonCastWhole();
	
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	afx_msg void OnBnClickedButtonMicvolMinus();
	afx_msg void OnBnClickedButtonMicvolPlus();
	afx_msg void OnBnClickedButtonSpkvolMinus();
	afx_msg void OnBnClickedButtonSpkvolPlus();
	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
