//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "NotificationADPanelUnit.h"

#define PERIOD_AD                    ( 3*1000 )
#define AD_SHOW_NUMBER 1

#define AD_CHANGE_WIDTH        		  10
#define AD_CHANGE_HEIGHT    	      6
#define AD_CHANGE_HEIGHT_TO_BOTTOM    5
#define AD_CHANGE_PIC2PIC   	   	  5

#define AD_PIC_LEFT      0
#define AD_PIC_TOP       0
#define AD_PIC_WIDTH     Width
#define AD_PIC_HEIGHT    (Height - AD_CHANGE_HEIGHT - 2*AD_CHANGE_HEIGHT_TO_BOTTOM)

const static COLORREF
		CLR_BKG   = RGB(39, 42, 47);
//---------------------------------------------------------------------------
#pragma package(smart_init)

__fastcall TNotiADPanel::TNotiADPanel( TComponent* Owner, VCTJSON   *VCJsonAD)
		: Vcl::Extctrls::TPanel( Owner )
{
	m_VCJsonAD = VCJsonAD;
	m_iIndex = 0;

	m_pADTimer           = new TTimer(this);
	m_pADTimer->Enabled  = false;
	m_pADTimer->Interval = PERIOD_AD;
	m_pADTimer->OnTimer  = NotificationTimerProc;

	this->BevelOuter = bvNone;

	ParentColor = true;
}
//---------------------------------------------------------------------------

__fastcall TNotiADPanel::~TNotiADPanel()
{
	if( m_pPaintBoxAD ) { delete m_pPaintBoxAD; m_pPaintBoxAD = NULL; }

	vector<TUrlPaintBox *>::iterator vc_pFindADChange;
	for( vc_pFindADChange = m_pPBADChange.begin();
		 vc_pFindADChange != m_pPBADChange.end();
		 vc_pFindADChange++ )
	{
		if( *vc_pFindADChange ) {
			delete *vc_pFindADChange;
			*vc_pFindADChange = NULL;
		}
	}

	vector<TPicture *>::iterator vc_pFindPIC;
	for( vc_pFindPIC = m_vcPIC.begin();
		 vc_pFindPIC != m_vcPIC.end();
		 vc_pFindPIC++ )
	{
		if( *vc_pFindPIC ) {
			delete *vc_pFindPIC;
			*vc_pFindPIC = NULL;
		}
	}

	if( m_pADTimer ) { delete m_pADTimer; m_pADTimer = NULL; }
}
//---------------------------------------------------------------------------

void __fastcall TNotiADPanel::CreateWnd()
{
	Vcl::Extctrls::TPanel::CreateWnd();

	SetAD();
}
//---------------------------------------------------------------------------

void __fastcall TNotiADPanel::WMEraseBkgnd(TWMEraseBkgnd &Message)
{
	TRect rect = GetClientRect();

	TCanvas* tempCanvas = new TCanvas;
	tempCanvas->Handle = Message.DC;

	tempCanvas->Brush->Color = CLR_BKG;		//±³¾°ÑÕÉ«
	tempCanvas->FillRect( rect );

	delete tempCanvas;
	tempCanvas = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TNotiADPanel::NotificationTimerProc(TObject* Sender)
{
	TTimer *NotiTimer = (TTimer *)Sender;

	int index = 0;
	int size  = (*m_VCJsonAD).size();

	if( size>AD_SHOW_NUMBER )
	{
		if( NotiTimer == m_pADTimer ) {
			m_iIndex = ( (m_iIndex+1)==size )? 0: (m_iIndex+1);
		}

		vector<TUrlPaintBox *>::iterator vc_pFindADChange;
		bool bFind = false;
		VCTJSON::iterator vc_pFindAD;
		for( vc_pFindAD  = (*m_VCJsonAD).begin(),
			 vc_pFindADChange = m_pPBADChange.begin();
			 vc_pFindAD != (*m_VCJsonAD).end(),
			 vc_pFindADChange != m_pPBADChange.end();
			 vc_pFindADChange++ )
		{
			if( index==m_iIndex ) {
				(*vc_pFindADChange)->State = 1;
				bFind = true;
			}else{
				(*vc_pFindADChange)->State = 0;
				if( !bFind ) {
                 	vc_pFindAD++;
				}
			}

			(*vc_pFindADChange)->Invalidate();
			index++;
		}

		vector<TPicture *>::iterator vc_pFindPIC = m_vcPIC.begin() + m_iIndex;

		m_pPaintBoxAD->GraphicImg = *vc_pFindPIC;
		m_pPaintBoxAD->StrUrl = (*vc_pFindAD).strUrl;
		m_pPaintBoxAD->Invalidate();

	}else {
		m_pADTimer->Enabled = false;
	}
}
//---------------------------------------------------------------------------

void __fastcall TNotiADPanel::SetAD()
{
	int adSize = (*m_VCJsonAD).size();

	if( adSize )
	{
		int startLoc = Width - adSize*(AD_CHANGE_WIDTH+AD_CHANGE_PIC2PIC)
					   + AD_CHANGE_PIC2PIC;

		TUrlPaintBox *tmpPB;
		TPicture *picImg;
        VCTJSON::iterator vc_pFindAD;
		for( vc_pFindAD  = (*m_VCJsonAD).begin();
			 vc_pFindAD != (*m_VCJsonAD).end();
			 vc_pFindAD++ )
		{
			picImg = new TPicture();
			String name = (*vc_pFindAD).strNamePic;
			picImg->LoadFromFile( (*vc_pFindAD).strNamePic );
			m_vcPIC.push_back(picImg);

			if( adSize>1 )
			{
				tmpPB = new TUrlPaintBox(this);
				tmpPB->Parent  = this;
				tmpPB->Width   = AD_CHANGE_WIDTH;
				tmpPB->Height  = AD_CHANGE_HEIGHT;
				tmpPB->Left    = startLoc;
				tmpPB->Top     = Height - AD_CHANGE_HEIGHT - AD_CHANGE_HEIGHT_TO_BOTTOM;
				tmpPB->StateCount = 2;

				tmpPB->OnMouseEnter = ADChangeMouseEnter;
				tmpPB->OnMouseLeave = ADChangeMouseLeave;

				if( vc_pFindAD == (*m_VCJsonAD).begin() ) {
					tmpPB->State = 1;
				}
				tmpPB->BringToFront();
				tmpPB->Visible = true;

				startLoc += AD_CHANGE_WIDTH + AD_CHANGE_PIC2PIC;
				m_pPBADChange.push_back(tmpPB);
			}
		}

		vc_pFindAD = (*m_VCJsonAD).begin();
		m_pPaintBoxAD = new TUrlPaintBox(this);
		m_pPaintBoxAD->Parent  = this;
		m_pPaintBoxAD->Width   = AD_PIC_WIDTH;
		m_pPaintBoxAD->Height  = AD_PIC_HEIGHT;
		m_pPaintBoxAD->Left    = AD_PIC_LEFT;
		m_pPaintBoxAD->Top     = AD_PIC_TOP;
		m_pPaintBoxAD->OnMouseEnter = PaintBoxMouseEnter;
		m_pPaintBoxAD->OnMouseLeave = PaintBoxMouseLeave;
		m_pPaintBoxAD->GraphicImg = *( m_vcPIC.begin() );
		m_pPaintBoxAD->StrUrl = (*vc_pFindAD).strUrl;
		m_pPaintBoxAD->Visible = true;
	}

	m_pADTimer->Enabled  = true;
}
//---------------------------------------------------------------------------

void __fastcall TNotiADPanel::PaintBoxMouseLeave(TObject *Sender)
{
	int adSize = (*m_VCJsonAD).size();
	if( adSize<=1 ){
    	return;
	}

	TPoint clientPos = ScreenToClient(Mouse->CursorPos);
	TUrlPaintBox *tmpPB = (TUrlPaintBox *)Sender;

	if( tmpPB==m_pPaintBoxAD ) {
		m_pADTimer->Enabled = true;
	}
}
//---------------------------------------------------------------------------

void __fastcall TNotiADPanel::PaintBoxMouseEnter(TObject *Sender)
{
	int adSize = (*m_VCJsonAD).size();
	if( adSize<=1 ){
    	return;
	}

	TUrlPaintBox *tmpPB = (TUrlPaintBox *)Sender;

	if( tmpPB==m_pPaintBoxAD ) {

		m_pADTimer->Enabled = false;
	}
}
//---------------------------------------------------------------------------

void __fastcall TNotiADPanel::ADChangeMouseEnter(TObject *Sender)
{
	m_pADTimer->Enabled = false;

	TUrlPaintBox *tmpPB = (TUrlPaintBox *)Sender;

	int index = 0;
	vector<TUrlPaintBox *>::iterator vc_pFindADChange;

	for( vc_pFindADChange = m_pPBADChange.begin();
		 vc_pFindADChange != m_pPBADChange.end();
		 vc_pFindADChange++ )
	{
		if( tmpPB==*vc_pFindADChange ) {
			break;
		}
		index++;
	}

	if( m_iIndex != index ) {
		m_iIndex = index;
		NotificationTimerProc(NULL);
    }
}
//---------------------------------------------------------------------------

void __fastcall TNotiADPanel::ADChangeMouseLeave(TObject *Sender)
{
 	m_pADTimer->Enabled = true;
}
//---------------------------------------------------------------------------


