#include "stdafx.h"
#include "../Verbpo.h"
#include "ResultTrend.h"


INT CTrendResult::m_i32Width = 160;
INT CTrendResult::m_i32Height = 114;

// CTrendResult

IMPLEMENT_DYNAMIC(CTrendResult, CStatic)

CTrendResult::CTrendResult(COLORREF color)
{
	int i = 0;
	m_color = color;
	
	m_i16WaveDateLength = 0;
	penLine = new CPen(PS_SOLID, 1, m_color);
	penBKLine = new CPen(PS_SOLID, 1, RGB(15, 100, 200));

	font.CreateFont(18,                                          //   nHeight     
		0,                                                   //   nWidth     
		0,                                                   //   nEscapement   
		0,                                                   //   nOrientation     
		FW_NORMAL,                                   //   nWeight     
		FALSE,                                           //   bItalic     
		FALSE,                                           //   bUnderline     
		0,                                                   //   cStrikeOut     
		ANSI_CHARSET,                             //   nCharSet     
		OUT_DEFAULT_PRECIS,                 //   nOutPrecision     
		CLIP_DEFAULT_PRECIS,               //   nClipPrecision     
		DEFAULT_QUALITY,                       //   nQuality     
		DEFAULT_PITCH | FF_SWISS,     //   nPitchAndFamily       
		_T("宋体"));


	for (i = 0; i < 8;i++)
	{
		m_i16Date[i] = new INT16[101];
		memset(m_i16Date[i], 0, 101 * 2);
		m_i16DateW[i] = 0, m_i16DateR[i] = 0, m_i16DateL[i] = 0;
	}
}

CTrendResult::~CTrendResult()
{
	int i = 0;
	
	//delete m_i16WaveDate;
	
	delete penLine;
	delete penBKLine;
	for (i = 0; i < 8; i++)
	{
		delete m_i16Date[i];
	}

	font.DeleteObject();
}


BEGIN_MESSAGE_MAP(CTrendResult, CStatic)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()



// CTrendResult 消息处理程序

int CTrendResult::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	CString str;
	INT i, j;
	// 获得控件区域
	GetClientRect(&m_rectCtrl);
	//客户区DC，可以绘画及显示
	CClientDC pDC(this);

	//依附窗口DC创建兼容内存DC
	dcMem.CreateCompatibleDC(&pDC);
	//创建兼容位图
	bmp.CreateCompatibleBitmap(&pDC, CTrendResult::m_i32Width, CTrendResult::m_i32Height);
	dcMem.SelectObject(&bmp); //将位图选择进内存DC
	dcMem.FillSolidRect(0, 0, CTrendResult::m_i32Width, CTrendResult::m_i32Height, RGB(0, 0, 0));

	//依附窗口DC创建兼容内存DC
	dcMemBK.CreateCompatibleDC(&pDC);
	//创建兼容位图
	bmpBK.CreateCompatibleBitmap(&pDC, CTrendResult::m_i32Width, CTrendResult::m_i32Height);
	dcMemBK.SelectObject(&bmpBK); //将位图选择进内存DC
	dcMemBK.FillSolidRect(0, 0, CTrendResult::m_i32Width, CTrendResult::m_i32Height, RGB(0, 0, 0));

	//清空所有图像
	dcMemBK.FillSolidRect(0, 0, CTrendResult::m_i32Width, CTrendResult::m_i32Height, RGB(0, 0, 0));

	dcMemBK.SelectObject(penBKLine);
	
	//画竖线
	for (i = 0; i <=100; i += 25)
	{
		for (j = 1; j <=100; j += 2)
		{
			dcMemBK.MoveTo(i + 59, CTrendResult::m_i32Height-(j + 5));
			dcMemBK.LineTo(i + 59, CTrendResult::m_i32Height - (j + 1 + 5));
		}
	}
	//画横线
	for (i = 0; i <=100; i += 25)
	{
		for (j = 1; j <=100; j += 2)
		{
			dcMemBK.MoveTo(j + 59, CTrendResult::m_i32Height - (i + 5));
			dcMemBK.LineTo(j + 59 + 1, CTrendResult::m_i32Height - (i + 5));
		}
	}

	
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(m_color);

	m_i32Show = 0;
	m_i32DestroyFlag = 0;

	Erase();
	Draw_Info();
	draw_wave();
	
	return TRUE;
}

//控件鼠标左键双击
void  CTrendResult::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	m_i32Show = (m_i32Show + 1) % m_stResultType.size();
	Erase();
	Draw_Info();
	draw_wave();
	Invalidate();
}

void CTrendResult::OnDestroy()
{
	CStatic::OnDestroy();

	m_i32DestroyFlag = 1;
	// TODO:  在此处添加消息处理程序代码

	dcMem.DeleteDC(); 
	bmp.DeleteObject(); 

	dcMemBK.DeleteDC(); 
	bmpBK.DeleteObject(); 
}


void CTrendResult::OnPaint()
{
	CPaintDC pDC(this);

	pDC.BitBlt(m_rectCtrl.left, m_rectCtrl.top, CTrendResult::m_i32Width, CTrendResult::m_i32Height, &dcMem, 0, 0, SRCCOPY);
	//	CStatic::OnPaint();
}


void CTrendResult::Draw_Info()
{
	INT m_i32SaveDC = 0;
	INT i = 0, j = 0;
	INT16 a_i16H, a_i16L, a_i16Type, a_i16Flag = 0;
	float a_fData = 0.0f;
	list<struct TREND_RESULT_TYPE>::iterator index;
	CString a_cs;

	if (m_stResultType.size())
	{
		for (index = m_stResultType.begin(); (index != m_stResultType.end()) && (j <= m_i32Show); index++, j++)
		{
			a_i16Type = index->i16Type;
			a_i16Flag = index->i16Flag;
			a_i16H = index->i16H, a_i16L = index->i16L;
			wcscpy_s(m_cTypeName, 8, index->cTypeName);
		}
		m_i32SaveDC = dcMem.SaveDC();
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(m_color);
		dcMem.FillSolidRect(0, 0, 59, CTrendResult::m_i32Height, RGB(0, 0, 0));

		if (a_i16Flag == 0)
		{
			a_cs.Format(_T("%d"), a_i16H);
			dcMem.TextOut(0, 10, a_cs);

			a_cs.Format(_T("%d"), a_i16L);
			dcMem.TextOut(0, 90, a_cs);
		}
		else if (a_i16Flag == 1)
		{
			a_fData = a_i16H;
			a_fData /= 10.0f;
			a_cs.Format(_T("%0.1f"), a_fData);
			dcMem.TextOut(0, 10, a_cs);
			a_fData = a_i16L;
			a_fData /= 10.0f;
			a_cs.Format(_T("%0.1f"), a_fData);
			dcMem.TextOut(0, 90, a_cs);
		}
		else if (a_i16Flag == 2)
		{
			a_fData = a_i16H;
			a_fData /= 100.0f;
			a_cs.Format(_T("%0.2f"), a_fData);
			dcMem.TextOut(0, 10, a_cs);
			a_fData = a_i16L;
			a_fData /= 100.0f;
			a_cs.Format(_T("%0.2f"), a_fData);
			dcMem.TextOut(0, 90, a_cs);
		}

		dcMem.TextOut(0, 50, m_cTypeName);
		dcMem.RestoreDC(m_i32SaveDC);
	}
}


void CTrendResult::Draw()
{
	INT m_i32SaveDC = 0;
	INT i = 0, j = 0,t=0, a_i32PointNum=0;
	INT16 a_i16H, a_i16L, a_i16Type, a_i16Flag = 0, a_i16Step = 0,a_i16Data=0;
	list<struct TREND_RESULT_TYPE>::iterator index;
	CString a_cs;

	if (m_stResultType.size())
	{
		for (index = m_stResultType.begin(); (index != m_stResultType.end()) && (j <= m_i32Show); index++, j++)
		{
			a_i16Type = index->i16Type;
			a_i16H = index->i16H, a_i16L = index->i16L;
			a_i16Flag = 1;

			break;
		}
		if (a_i16Flag)
		{
			m_i32SaveDC = dcMem.SaveDC();
			dcMem.SelectObject(penLine);
			a_i16Step = (a_i16H - a_i16L) / 4;

			for (i = 0; (i < 100) && (i<m_i16DateL[m_i32Show]); i++)
			{
				t = (m_i16DateW[j] + 100 - i - 1) % 100;
				
				switch (index->i16Type)
				{
				case RESULT_TYPE_HR:
					if (m_i16Date[m_i32Show][t] == CData::m_i16HRInvalidValue)
					{
						a_i16Data = 1000;
					} 
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data>100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data<0)
						{
							a_i16Data = 0;
						}
					}
					break;

				case RESULT_TYPE_ST:
					if (m_i16Date[m_i32Show][t] == CData::m_i16STInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;

				case RESULT_TYPE_PVCs:
					if (m_i16Date[m_i32Show][t] == CData::m_i16PVCsInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;

				case RESULT_TYPE_SBP:
					if (m_i16Date[m_i32Show][t] == CData::m_i16SBPInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;

				case RESULT_TYPE_DBP:
					if (m_i16Date[m_i32Show][t] == CData::m_i16DBPInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;

				case RESULT_TYPE_RR:
					if (m_i16Date[m_i32Show][t] == CData::m_i16RRInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;


				case RESULT_TYPE_SPO2:
					if (m_i16Date[m_i32Show][t] == CData::m_i16PRInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;

				case RESULT_TYPE_PR:
					if (m_i16Date[m_i32Show][t] == CData::m_i16PRInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;

				case RESULT_TYPE_TEMP:
					if (m_i16Date[m_i32Show][t] == CData::m_i16TempInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;

				case RESULT_TYPE_AIROXY:
					if (m_i16Date[m_i32Show][t] == CData::m_i16AirOxyInvalidValue)
					{
						a_i16Data = 1000;
					}
					else
					{
						a_i16Data = (m_i16Date[m_i32Show][t] - a_i16L) * 25 / a_i16Step;
						if (a_i16Data > 100)
						{
							a_i16Data = 100;
						}
						else if (a_i16Data < 0)
						{
							a_i16Data = 0;
						}
					}
					break;
				default:
					break;
				}
				
				a_i32PointNum++;
				dcMem.SetPixel(a_i32PointNum + 60, 109 - a_i16Data, m_color);
				dcMem.RestoreDC(m_i32SaveDC);
			}
			
		}
	}

}


void CTrendResult::Erase()
{
	INT m_i32SaveDC = 0;
	m_i32SaveDC = dcMem.SaveDC();
	dcMem.BitBlt(m_rectCtrl.left , m_rectCtrl.top, CTrendResult::m_i32Width, CTrendResult::m_i32Height, &dcMemBK, 0, 0, SRCCOPY);
	dcMem.RestoreDC(m_i32SaveDC);
}

void CTrendResult::draw_wave()
{
	Erase();
	Draw_Info();
	Draw();
	Invalidate();
}

void CTrendResult::Add_TrendResultType(struct TREND_RESULT_TYPE *st)
{
	struct TREND_RESULT_TYPE a_stTREND_RESULT_TYPE;

	memcpy(&a_stTREND_RESULT_TYPE, st, sizeof(struct TREND_RESULT_TYPE));
	if (m_stResultType.size() <=8)
	{
		m_stResultType.push_back(a_stTREND_RESULT_TYPE);
		//m_stResultType.push_back(a_stTREND_RESULT_TYPE);
	}
	else
	{
		m_stResultType.pop_front();
		m_stResultType.push_back(*st);
	}
}

void CTrendResult::Add_TrendResult(INT16 type, INT16 r)
{
	INT  j = 0,i;
	INT16  a_i16Flag = 0;
	list<struct TREND_RESULT_TYPE>::iterator index;
	CString a_cs;
	i = m_stResultType.size();
	if (m_stResultType.size())
	{
		for (index = m_stResultType.begin(); (index != m_stResultType.end()); index++, j++)
		{
			if (type == index->i16Type)
			{
				a_i16Flag = 1;

				break;
			}
		}
		if (a_i16Flag)
		{
			m_i16Date[j][m_i16DateW[j]] = r;
			m_i16DateW[j] = (m_i16DateW[j] + 1) % 100;
			if (m_i16DateL[j]<100)
			{
				m_i16DateL[j]++;
			}
		}
	}
}