// VideoLive.cpp : implementation file
//

#include "stdafx.h"
#include "NetSdkDemo.h"
#include "VideoLive.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CVideoLive::CVideoLive()
{
	m_nWorkMode = 1;
	m_pDlgZoomIn = NULL;
	m_nZoomType  = 0;
}

CVideoLive::~CVideoLive()
{
}

BEGIN_MESSAGE_MAP(CVideoLive, CWnd)
	//{{AFX_MSG_MAP(CVideoLive)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CVideoLive message handlers

int CVideoLive::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	for(int i = 0; i < VCHN_MAX; i++)
	{
		m_VChn[i].m_pLiveScren = this;
		m_VChn[i].Create(
			NULL, 
			NULL, 
			WS_VISIBLE | WS_CHILD,
			CRect(0, 0, 0, 0),
			this,
			1979,
			NULL);
		//AddPage(&m_VChn[i]);
	}
	return 0;
}

void CVideoLive::OnDestroy() 
{
	if(m_bFullScreen)
		SetFullScreen(FALSE);
	startzoomin(FALSE);
	
	CWnd::OnDestroy();
	
	for(int i=0; i < VCHN_MAX; i++)
	{
		m_VChn[i].DestroyWindow();
	}
}
void CVideoLive::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	UpdateWnd();
}

BOOL CVideoLive::OnEraseBkgnd(CDC* pDC) 
{
	CRect rt;
	GetClientRect(&rt);
	CBrush br;
	br.CreateSolidBrush(m_clrBack);
	//br.CreateSolidBrush(RGB(255,255,255));
	pDC->FillRect(&rt,&br);
	
	//AfxMessageBox("OnEraseBkgnd");
	return TRUE;
	//return CWnd::OnEraseBkgnd(pDC);
}

int CVideoLive::SetSlipMode(int nMode, int nStart)
{
	int nNum = 16;
	int nBegin = 0;
	int i;
	if(nMode == VIDEO_MODE_1)
	{
		nNum   = 1;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode == VIDEO_MODE_2)
	{
		nNum   = 2;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode == 3 || nMode == VIDEO_MODE_4)
	{
		nNum   = VIDEO_MODE_4;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode == 5 || nMode == VIDEO_MODE_6)
	{
		nNum   = VIDEO_MODE_6;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode == 7 || nMode == VIDEO_MODE_8)
	{
		nNum   = VIDEO_MODE_8;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode == VIDEO_MODE_9)
	{
		nNum   = VIDEO_MODE_9;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode >= 10 && nMode <= VIDEO_MODE_13)
	{
		nNum   = VIDEO_MODE_13;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode >= 14 && nMode <= VIDEO_MODE_16)
	{
		nNum   = VIDEO_MODE_16;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode >= 17 && nMode <= VIDEO_MODE_20)
	{
		nNum   = VIDEO_MODE_20;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode >= 21 && nMode <= VIDEO_MODE_25)
	{
		nNum   = VIDEO_MODE_25;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode >= 26 && nMode <= VIDEO_MODE_36)
	{
		nNum   = VIDEO_MODE_36;
		nBegin = ((nStart / nNum) * nNum);
	}
	else if(nMode >= VIDEO_MODE_PAN_2 && nMode <= VIDEO_MODE_PAN_8)
	{
		nNum   = nMode - VIDEO_MODE_PAN;
		nBegin = ((nStart / nNum) * nNum);
	}
	else
	{
		DPERROR(("not support mode:%d\n", nMode));
		return 0;
	}
	
	//if( m_bDrawActive ) 
	//	DrawActivePage(FALSE);
	
	m_nCurViewMode = nNum;
	m_nCurStartChn = nStart;

	CList<VideoWndChn*, VideoWndChn*> templist;
	
	POSITION pos = m_PageList.GetHeadPosition();
	while(pos != NULL)
	{
		VideoWndChn* pWnd = m_PageList.GetNext(pos);
		if (pWnd)
		{
			templist.AddTail(pWnd);
		}
	}
	m_PageList.RemoveAll();
	
	if( (nBegin + nNum) > VCHN_MAX)
	{
		int addno = nBegin + nNum - VCHN_MAX;
		for(i=nBegin; i < VCHN_MAX; i++)
			m_PageList.AddTail(&m_VChn[i]);
		for(i=0; i < addno; i++)
			m_PageList.AddTail(&m_VChn[i]);
	}
	else
	{
		for(i=nBegin; i < (nBegin+nNum); i++)
			m_PageList.AddTail(&m_VChn[i]);
	}
	
	pos = templist.GetHeadPosition();
	while(pos != NULL)
	{
		VideoWndChn* pWnd = templist.GetNext(pos);
		if (pWnd)
		{
			POSITION pos2 = m_PageList.Find(pWnd);
			if(!pos2)
				pWnd->ShowWindow(SW_HIDE);
		}
	}
	templist.RemoveAll();
	m_pActivePage = &m_VChn[nBegin];
	/*{
		CRect rtContainer;
		GetRefrenRect(rtContainer);
		
		m_bMultiScreen = nMode == VIDEO_MODE_1 ? FALSE : TRUE;
		InvalidateRect(rtContainer);
	}*/
	SetMultiScreen(nMode == VIDEO_MODE_1 ? FALSE : TRUE);
	//SetActivePage(&m_VChn[nStart]);
	return 0;
}

void CVideoLive::SetAutoScan(BOOL bAutoScan)
{
	if(m_bAutoSizeAdj != bAutoScan)
	{
		int i;
		m_bAutoSizeAdj = bAutoScan;
		for(i=0; i<VCHN_MAX; i++)
		{
			m_VChn[i].SetWndSize(m_bAutoSizeAdj);
		}
	}
}

int CVideoLive::startzoomin(BOOL bStart)
{
	if(bStart)
	{
		CLiveWndChn *pCurWnd = (CLiveWndChn*)GetActivePage();
		if(!pCurWnd || !pCurWnd->m_lHandle)
			return -1;
		if(m_nZoomType && m_nZoomType != 1)
			return -1;
		if(m_pDlgZoomIn)
		{
			m_pDlgZoomIn->DestroyWindow();
			delete m_pDlgZoomIn;
			m_pDlgZoomIn = NULL;
		}
		
		m_pDlgZoomIn = new CDlgZoomIn();
		m_pDlgZoomIn->m_lChnHandle = pCurWnd->m_lHandle;
		m_pDlgZoomIn->Create(IDD_DLG_ZOOMIN, this);
		m_pDlgZoomIn->ShowWindow(SW_SHOW);
		m_nZoomType = 1;
	}
	else if(m_pDlgZoomIn)
	{
		m_pDlgZoomIn->DestroyWindow();
		delete m_pDlgZoomIn;
		m_pDlgZoomIn = NULL;
		m_nZoomType  = 0;
	}
	return 0;
}
