// TouchButton.cpp : implementation file
//

#include "stdafx.h"
#include "TouchBoard.h"
#include "TouchBoardDlg.h"
#include "TouchButton.h"

const static int TBD_BTN_TYPE = 1;

// CTouchButton
extern CTouchBoardDlg *g_pMainDlg;
CxImage CTouchButton::m_ciStatus[MAX_STATUS_COUNT];
CxImage CTouchButton::m_ciCheck;
BOOL CTouchButton::m_bGlobalInit = false;
INT32 CTouchButton::m_iLongPressMode = TOUCH_BTN_LONG_PRESS_MODE_PRESSING;

IMPLEMENT_DYNAMIC(CTouchButton, CWnd)

CTouchButton::CTouchButton()
{
	m_pMemDC = NULL;
	m_pDCBitmap = NULL;
	m_pDidLinePen = NULL;
	m_pFillBrush = NULL;
	m_pFont = NULL;
	m_pFontSub = NULL;

	m_buttonUiStyle = 2;		// 1 : 4개 아이콘 동시 표시 지원 스타일, 2 : 아이콘 1개씩 표시 가능 스타일
	m_iUnderNumber = -1;
	m_bPressable = true;
	m_bPressed = false;
	m_bChecked = false;
	m_bCreated = false;
	m_bVisible = true;
	m_uiPressMessage = 0;
	m_iBtnType = TOUCH_BTN_TYPE_LEVEL4;
	m_iBtnFlag = TOUCH_BTN_FLAG_STATE;

	m_strName = L"세영119안전센터";

	for(int i = 0;i < MAX_STATUS_COUNT;i++)
		m_bStatus[i] = false;
}

CTouchButton::~CTouchButton()
{
}


BEGIN_MESSAGE_MAP(CTouchButton, CWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()



// CTouchButton message handlers

void CTouchButton::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CWnd::OnPaint() for painting messages
	CBitmap *pOldBitmap;
	CPen *pOldPen;
	bool isOnAir = false;

	if(m_pMemDC == NULL) {
		m_pMemDC = new CDC();
		m_pMemDC->CreateCompatibleDC(&dc);
	}

	if(m_pDCBitmap == NULL) {
		m_pDCBitmap = new CBitmap();
		m_pDCBitmap->CreateCompatibleBitmap(&dc, m_width, m_height);
	}

	if(m_pDidLinePen == NULL) {
		m_pDidLinePen = new CPen();
		m_pDidLinePen->CreatePen(PS_SOLID, 1, TOUCH_BTN_LINE_COLOR);
	}

	if(m_pFillBrush == NULL) {
		m_pFillBrush = new CBrush();
		m_pFillBrush->CreateSolidBrush(RGB(0, 200, 200));
	}

	pOldBitmap = m_pMemDC->SelectObject(m_pDCBitmap);
	pOldPen = m_pMemDC->SelectObject(m_pDidLinePen);

	if(m_iBtnType != TOUCH_BTN_TYPE_GROUP) {
		if(m_iUnderNumber >= 0)
			isOnAir = true;
		for(INT32 i = 0;i < MAX_STATUS_COUNT;i++) {
			if(m_bStatus[i]) {
				isOnAir = true;
			}
		}
	}
	

	if(isEnableButton()) {
		if(m_bPressed) {
			m_pMemDC->FillSolidRect(0, 0, m_width, m_height, TOUCH_BTN_PRESS_COLOR);
		} else if(isOnAir) {
			m_pMemDC->FillSolidRect(0, 0, m_width, m_height, TOUCH_BTN_ONAIR_COLOR);
		} else {
			m_pMemDC->FillSolidRect(0, 0, m_width, m_height / 2, TOUCH_BTN_GENERAL_COLOR_UP);
			m_pMemDC->FillSolidRect(0, m_height / 2, m_width, m_height / 2, TOUCH_BTN_GENERAL_COLOR_DOWN);
		}
	} else {
		if(m_iBtnType == TOUCH_BTN_TYPE_GROUP) {
			m_pMemDC->FillSolidRect(0, 0, m_width, m_height, TOUCH_BTN_GROUP_COLOR_DISABLE);
		} else {
			m_pMemDC->FillSolidRect(0, 0, m_width, m_height, TOUCH_BTN_GENERAL_COLOR_DISABLE);
		}
	}

	m_pMemDC->SetBkMode( TRANSPARENT );

	if(m_iBtnType == TOUCH_BTN_TYPE_LEVEL2 || m_iBtnType == TOUCH_BTN_TYPE_LEVEL3 || m_iBtnType == TOUCH_BTN_TYPE_LEVEL4) {
		if(m_buttonUiStyle == 1) {
			m_pMemDC->FillSolidRect(0, 0, m_ciStatus[0].GetWidth() * 2, m_ciStatus[0].GetHeight() * 4, TOUCH_BTN_CHECK_BG_COLOR);

			if(m_bStatus[0])
				m_ciStatus[0].Draw2(m_pMemDC->GetSafeHdc(), 0, 0);
			if(m_bStatus[1])
				m_ciStatus[1].Draw2(m_pMemDC->GetSafeHdc(), m_ciStatus[0].GetWidth(), 0);
			if(m_bStatus[2])
				m_ciStatus[2].Draw2(m_pMemDC->GetSafeHdc(), 0, m_ciStatus[0].GetHeight());
			if(m_bStatus[3])
				m_ciStatus[3].Draw(m_pMemDC->GetSafeHdc(), m_ciStatus[1].GetWidth(), m_ciStatus[1].GetHeight());
		} else if(m_buttonUiStyle == 2) {
			//TRACE("BTN WIDTH : %d, HEIGHT : %d\n", m_ciStatus[0].GetWidth(), m_ciStatus[0].GetHeight());
			m_pMemDC->FillSolidRect(0, 0, m_ciStatus[0].GetWidth(), m_ciStatus[0].GetHeight() * 2, TOUCH_BTN_CHECK_BG_COLOR);
			for(INT32 i = 0;i < MAX_STATUS_COUNT;i++) {
				if(m_bStatus[i]) {
					m_ciStatus[i].Draw2(m_pMemDC->GetSafeHdc(), 0, (m_height - m_ciStatus[0].GetHeight()) / 4);
				}
			}
		}

		
		if(m_bChecked) {
			//m_pMemDC->FillSolidRect(0 + 4, m_ciStatus[0].GetHeight() * 2 + 4, m_ciStatus[0].GetWidth() * 2 - 8, m_ciStatus[0].GetHeight() * 2 - 8, TOUCH_BTN_PRESS_COLOR);
			m_ciCheck.Draw(m_pMemDC->GetSafeHdc(), 3, m_ciStatus[1].GetHeight() * 2 + 3);
		}
	}

	RECT rt;
	COLORREF crOldColor = SetTextColor(m_pMemDC->GetSafeHdc(), RGB(0, 0, 0));
	CFont *pOldFont = m_pMemDC->SelectObject(m_pFont);
	GetClientRect(&rt);
	
	if(isEnableButton()) {
		if (m_bPressed) {
			SetTextColor(m_pMemDC->GetSafeHdc(), TOUCH_BTN_PRESS_FONT_COLOR);
		} else {
			SetTextColor(m_pMemDC->GetSafeHdc(), TOUCH_BTN_GENERAL_FONT_COLOR);
		}
	} else {
		if(m_iBtnType == TOUCH_BTN_TYPE_GROUP) {
			//SetTextColor(m_pMemDC->GetSafeHdc(), RGB(100, 100, 100));
			SetTextColor(m_pMemDC->GetSafeHdc(), TOUCH_BTN_GENERAL_FONT_COLOR);
		} else {
			SetTextColor(m_pMemDC->GetSafeHdc(), RGB(220, 220, 220));
		}
	}
	
	if(m_iBtnType == TOUCH_BTN_TYPE_LEVEL2 || m_iBtnType == TOUCH_BTN_TYPE_LEVEL3 || m_iBtnType == TOUCH_BTN_TYPE_LEVEL4) {
		rt.left = 30;
	}
	//rt.top = rt.bottom - 40;
	::DrawText(m_pMemDC->GetSafeHdc(), m_strName, m_strName.GetLength(), &rt, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	::SetTextColor(m_pMemDC->GetSafeHdc(), crOldColor);

	if(m_iUnderNumber >= 0) {
		TCHAR strUnder[64];
		RECT rtFill;
		GetClientRect(&rt);
		GetClientRect(&rtFill);
		if(m_buttonUiStyle == 1) {
			rt.top = 30;
			rt.right = 30;
			rtFill.top = 32;
			rtFill.right = 30;
		} else if(m_buttonUiStyle == 2) {
			rt.right = 30;
			rtFill.right = 30;
		}

		if(m_iUnderNumber > 0) {
			wsprintf(strUnder, L"%d", m_iUnderNumber);
		} else {
			wsprintf(strUnder, L"서");
		}
		m_pMemDC->SelectObject(m_pFontSub);
		m_pMemDC->FillRect(&rtFill, m_pFillBrush);
		::DrawText(m_pMemDC->GetSafeHdc(), strUnder, lstrlen(strUnder), &rt, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	}

	m_pMemDC->SelectObject(pOldPen);

	dc.BitBlt(0, 0, m_width, m_height, m_pMemDC, 0, 0, SRCCOPY);
	m_pMemDC->SelectObject(pOldBitmap);
	m_pMemDC->SelectObject(pOldFont);
}

BOOL CTouchButton::isEnableButton()
{
	BOOL bResult = TRUE;
	if(m_iBtnType == TOUCH_BTN_TYPE_GROUP) {
		if(m_bPressed) {
			bResult = TRUE;
		} else {
			bResult = FALSE;
		}
	} else {
		if(!m_bEnabled) {
			bResult = FALSE;
		} else {
			bResult = TRUE;
		}
	}

	return bResult;
}

void CTouchButton::initialize(int x, int y, int total_width, int total_height, int ico_width, int ico_height, int id, CWnd *pParent)
{
	m_btnId = id;
	m_width = total_width;
	m_height = total_height;
	RECT size;
	size.left = x;
	size.top = y;
	size.right = x + m_width;
	size.bottom = y + m_height;
	//Create(L"TB", L"TB", WS_CHILD, size, pParent, id);
	DWORD dwStyle = WS_CHILD | WS_BORDER;
	if(m_bVisible)
		dwStyle |= WS_VISIBLE;

	if(m_bCreated) {
		DestroyWindow();
	}

	BOOL ret = Create(NULL, NULL, dwStyle, size, pParent, m_btnId);
	if(ret != TRUE) {
		TRACE("FAIL CREATE BUTTON!\n");
		MessageBox(L"버튼 생성에 실패했습니다", L"오류");
		PostMessage(WM_CLOSE);
	}

	m_bCreated = TRUE;

	if(!m_bGlobalInit)
	{
		for(INT32 i = 0;i < MAX_STATUS_COUNT;i++) {
			m_ciStatus[i].Create(TBD_BTN_ICON_WIDTH, TBD_BTN_ICON_HEIGHT, 32);
			m_ciStatus[i].Clear();
		}

		RGBQUAD color;
		m_ciStatus[0].FloodFill(0, 0, RGB_QUAD_RED);
		m_ciStatus[1].FloodFill(0, 0, RGB_QUAD_GREEN);
		m_ciStatus[2].FloodFill(0, 0, RGB_QUAD_BLUE);

		RGB2RGBQURD(RGB(0, 255, 255), color);
		m_ciStatus[3].FloodFill(0, 0, color);

		RGB2RGBQURD(RGB(10, 10, 255), color);
		m_ciStatus[4].FloodFill(0, 0, color);

		HINSTANCE hInst = AfxGetInstanceHandle();
		HRSRC res = FindResource(hInst, MAKEINTRESOURCE(IDB_PNG_CHECK), L"PNG");
		m_ciCheck.LoadResource(res, CXIMAGE_FORMAT_PNG, hInst);
	
		res = FindResource(hInst, MAKEINTRESOURCE(IDB_PNG_ICON_FIRE), L"PNG");
		m_ciStatus[0].LoadResource(res, CXIMAGE_FORMAT_PNG, hInst);
		res = FindResource(hInst, MAKEINTRESOURCE(IDB_PNG_ICON_RESCUE), L"PNG");
		m_ciStatus[1].LoadResource(res, CXIMAGE_FORMAT_PNG, hInst);
		res = FindResource(hInst, MAKEINTRESOURCE(IDB_PNG_ICON_EMERGENCY), L"PNG");
		m_ciStatus[2].LoadResource(res, CXIMAGE_FORMAT_PNG, hInst);
		res = FindResource(hInst, MAKEINTRESOURCE(IDB_PNG_ICON_MIC), L"PNG");
		m_ciStatus[3].LoadResource(res, CXIMAGE_FORMAT_PNG, hInst);

		for(INT32 i = 0;i < 4;i++) {
			BOOL res = m_ciStatus[i].Resample(TBD_BTN_ICON_WIDTH, TBD_BTN_ICON_HEIGHT, 0, 0);
			if(!res) {
				TRACE("RESIZING FAILED...\n");
			}
		}

		m_bGlobalInit = true;
		m_iLongPressMode = TOUCH_BTN_LONG_PRESS_MODE_PRESSING;
	}

	if(!m_bVisible)
		ShowWindow(SW_HIDE);
#if 0
	for(int i = 0;i < MAX_STATUS_COUNT;i++) {
		m_ciStatus[i].AlphaCreate();
		m_ciStatus[i].AlphaSet(100);
	}
#endif
}


void CTouchButton::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_iBtnType == TOUCH_BTN_TYPE_GROUP) {
		if(m_uiPressMessage) {
			AfxGetMainWnd()->PostMessageW(m_uiPressMessage, m_btnId, 0);
		} else {
			m_bPressed = true;
			Invalidate(false);
		}
	} else {
		if(!m_bEnabled)
			return;

		m_pressTime = GetTickCount64();
		if(m_iBtnFlag & TOUCH_BTN_FLAG_CHECKABLE) {
			SetTimer(1, 500, NULL);
		}
	}

	CWnd::OnLButtonDown(nFlags, point);
}


void CTouchButton::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	UINT64 curTick = GetTickCount64();

	if(m_iBtnType == TOUCH_BTN_TYPE_GROUP) {
	} else {
		if(curTick - m_pressTime < 500) {
			TRACE("CLICK\n");
			SetPressState(~m_bPressed);
			//m_bPressed = ~m_bPressed;
		} else {
			TRACE("LONG CLICK\n");
		}

		if(m_uiPressMessage) {
			AfxGetMainWnd()->PostMessageW(m_uiPressMessage, m_btnId, 0);
		}

		KillTimer(1);
		m_pressTime = 0;
		Invalidate(false);
	}

	CWnd::OnLButtonUp(nFlags, point);
	
}


void CTouchButton::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 1)
	{
		if(m_pressTime > 0) {
			if(m_iLongPressMode == TOUCH_BTN_LONG_PRESS_MODE_GROUPING)
			{
				m_bChecked = ~m_bChecked;
				Invalidate(false);
				AfxGetMainWnd()->Invalidate(false);
			} else if(m_iLongPressMode == TOUCH_BTN_LONG_PRESS_MODE_PRESSING) {
				BOOL bNextPressed = ~m_bPressed;
				AfxGetMainWnd()->PostMessageW(WM_TBD_SUB_GROUPING, m_btnId, bNextPressed);
			}
		}
	}

	KillTimer(1);
	CWnd::OnTimer(nIDEvent);
}


BOOL CTouchButton::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	//return false;

	return CWnd::OnEraseBkgnd(pDC);
}


void CTouchButton::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CWnd::OnLButtonDblClk(nFlags, point);
}


void CTouchButton::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_iBtnType == TOUCH_BTN_TYPE_GROUP) {
		m_bPressed = true;
		if(m_uiPressMessage) {
			AfxGetMainWnd()->PostMessageW(m_uiPressMessage, m_btnId, 0);
		}
		Invalidate(false);
	} else {
		if(m_iBtnFlag & TOUCH_BTN_FLAG_CHECKABLE) {
			if(m_iLongPressMode == TOUCH_BTN_LONG_PRESS_MODE_GROUPING)
			{
				m_bChecked = ~m_bChecked;
				Invalidate(false);
				AfxGetMainWnd()->Invalidate(false);
			} else if(m_iLongPressMode == TOUCH_BTN_LONG_PRESS_MODE_PRESSING) {
				BOOL bNextPressed = ~m_bPressed;
				AfxGetMainWnd()->PostMessageW(WM_TBD_SUB_GROUPING, m_btnId, bNextPressed);
			}
		}
	}

	CWnd::OnRButtonDown(nFlags, point);
}
