// DlgProBar.cpp : 实现文件
//
#include "stdafx.h"
#include "SATHelper.h"
#include "DlgProBar.h"
#include "afxdialogex.h"

#include "IMGCommon.h"
#include "SATClient.h"

// CDlgProBar 对话框

Image* CDlgProBar::m_spImg = NULL;
BOOL CDlgProBar::m_sbRuning = TRUE;
Brush *CDlgProBar::m_pBrush = NULL;
extern CDlgProBar* g_pDlgProBar;
IMPLEMENT_DYNAMIC(CDlgProBar, CDialogEx)

CDlgProBar::CDlgProBar(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_DLG_PROBAR, pParent)
{
	m_ulTimeCount = 0;
	m_nFrameCount = 0;
	m_nCurFrameIndex = 0;
	m_pPropertyItem = NULL;
}

CDlgProBar::~CDlgProBar()
{
}

void CDlgProBar::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDlgProBar, CDialogEx)
	ON_MESSAGE(WM_STOPTHREAD, &CDlgProBar::StopThread)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CDlgProBar 消息处理程序

BOOL CDlgProBar::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_sbRuning = TRUE;
	if ( m_spImg )
		delete m_spImg;
	m_spImg = NULL;
	m_spImg = IMGCommon::LoadImgFromResource(NULL, MAKEINTRESOURCE(GIF_PROBAR), _T("GIF"));

#if 1
	// 获取gif里的维度个数（gif就一个维度）;
	UINT nCount = m_spImg->GetFrameDimensionsCount();
	
	// 获得维度的GUID列表;
	GUID* pDimensionsIDs = (GUID*)new GUID[nCount];
	m_spImg->GetFrameDimensionsList(pDimensionsIDs, nCount);
	
	// 获得指定的维度的帧数;
	//WCHAR strGUID[39] = { 0 };
	//StringFromGUID2(pDimensionsIDs[0], strGUID, 39);
	m_nFrameCount = m_spImg->GetFrameCount(&pDimensionsIDs[0]);
	delete[] pDimensionsIDs;

	// 获得每帧之间的时间间隔;
	int size = m_spImg->GetPropertyItemSize(PropertyTagFrameDelay);	// 获取时间间隔属性item;
	m_pPropertyItem = new byte[size];
	PropertyItem*  pItem = (PropertyItem*)m_pPropertyItem;
	m_spImg->GetPropertyItem(PropertyTagFrameDelay, size, pItem);

	CClientDC dc(this);
	Graphics gc(dc);
	gc.DrawImage(m_spImg, 0, 0, m_spImg->GetWidth(), m_spImg->GetHeight());

	// 超时值;
	m_ulTimeCount = GetTickCount64();
	SetTimer(0, 0, NULL);
#endif

	MoveWindow(0, 0, m_spImg->GetWidth(), m_spImg->GetHeight());
	CenterWindow();

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

LRESULT CDlgProBar::StopThread(WPARAM wp, LPARAM lp)
{
	m_sbRuning = FALSE;
	return 0;
}

void CDlgProBar::OnCancel()
{
	g_pDlgProBar = NULL;
	// TODO: 在此添加专用代码和/或调用基类
	CDialogEx::OnCancel();
}

void CDlgProBar::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类

	//CDialogEx::OnOK();
}

DWORD g_dwTimeOut = 30000;
void CDlgProBar::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if ( nIDEvent == 0 ) {
		KillTimer(0);

		// 是否超时;
		if (GetTickCount64() - m_ulTimeCount > g_dwTimeOut) {
			m_sbRuning = FALSE;
			if (m_spImg)
				delete m_spImg;
			m_spImg = NULL;
			if (m_pPropertyItem)
				delete[]m_pPropertyItem;
			m_pPropertyItem = NULL;

			CSATClient::GetInstance()->Stop();
			//AfxMessageBox(_T("连接超时！"));

			PostMessage(WM_CLOSE);
			
			return;
		}

		GUID guid = FrameDimensionTime;
		CClientDC dc(this);
		Graphics gc(dc);
		gc.DrawImage(m_spImg, 0, 0, m_spImg->GetWidth(), m_spImg->GetHeight());
		// 选择要显示的帧;
		m_spImg->SelectActiveFrame(&guid, m_nCurFrameIndex++);
		if (m_nCurFrameIndex == m_nFrameCount)
		{
			m_nCurFrameIndex = 0;
		}

		PropertyItem* pItem = (PropertyItem*)m_pPropertyItem;
		long pause = ((long*)pItem->value)[m_nCurFrameIndex] * 1000;
		pause = pause ? pause : 39;
		
		if (m_sbRuning) {
			SetTimer(0, pause, NULL);
		}
		else {
			if (m_spImg)
				delete m_spImg;
			m_spImg = NULL;
			if (m_pPropertyItem)
				delete[]m_pPropertyItem;
			m_pPropertyItem = NULL;
			PostMessage(WM_CLOSE);
		}
	}
	CDialogEx::OnTimer(nIDEvent);
}
