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

#include <Vcl.Imaging.pngimage.hpp>
#include "PayMentPanelUnit.h"
#include "GlobalUnit.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
#define LINE_HEIGHT       	  (20)
#define SCOLLBAR_WIDTH    	  (14)
#define STYLE_NAME_WIDTH      (Width/2)
#define STYLE_PAY_WIDTH       (Width - STYLE_NAME_WIDTH - SCOLLBAR_WIDTH)
#define PANEL_HEIGHT 		   150

const static COLORREF
		CLR_TXT_HOT 			    = RGB(171, 186, 207),
		CLR_BKG_INTERNAL 	        = RGB(47, 50, 56),
		CLR_BKG_INTERNAL_LIGHT 	    = RGB(73, 77, 86),
		CLR_CONTENT_DB_LINE_DARK    = RGB(32, 35, 39),
		CLR_CONTENT_DB_LINE_LIGHT   = RGB(76, 81, 87);
//---------------------------------------------------------------------------
__fastcall TPayMentPanel::TPayMentPanel( TComponent* Owner)
		: Vcl::Extctrls::TPanel( Owner )
{
	m_ScrollBar = NULL;
	m_imgInfo = NULL;

	m_iTopLine  = 0;
	m_iShowLine = 1;
	bStylePayShow = true;
	DoubleBuffered = true;
	ParentCtl3D = false;
	Ctl3D = false;
}
//---------------------------------------------------------------------------

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

	SetShowLine();
}
//---------------------------------------------------------------------------

__fastcall TPayMentPanel::~TPayMentPanel()
{
	if( m_ScrollBar ){
		delete m_ScrollBar;
		m_ScrollBar = NULL;
	}

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

void __fastcall TPayMentPanel::WMEraseBkgnd(TWMEraseBkgnd &Message)
{

	TRect rect = GetClientRect();

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

	tempCanvas->Brush->Color = CLR_BKG_INTERNAL;		//背景颜色
	tempCanvas->FillRect( rect );
	tempCanvas->Brush->Color = CLR_CONTENT_DB_LINE_DARK;
	tempCanvas->FrameRect( rect );

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

void __fastcall TPayMentPanel::WMVScroll(Winapi::Messages::TWMScroll &Message)
{
	int minpos, maxpos, page, curpos;
	m_ScrollBar->GetScrollRange(&minpos, &maxpos);
	page = m_ScrollBar->GetPageSize();
	// Get the current position of scroll box.
	curpos = m_ScrollBar->GetScrollPos();

	// Determine the new position of scroll box.
	switch (Message.ScrollCode)
	{
	case SB_TOP:      // Scroll to far top.
		curpos = minpos;
		break;

	case SB_BOTTOM:      // Scroll to far bottom.
		curpos = maxpos-page+1;
		break;

	case SB_ENDSCROLL:   // End scroll.
		break;

	case SB_LINEUP:      // Scroll left.
		if (curpos > minpos)
			curpos--;
		break;

	case SB_LINEDOWN:   // Scroll right.
		if (curpos < maxpos-page+1)
			curpos++;
		break;

	case SB_PAGEUP:    // Scroll one page left.
		if (curpos > minpos)
			curpos = (minpos > curpos-page) ? minpos : curpos-page;
			//max(minpos, curpos-page);
		break;

	case SB_PAGEDOWN:      // Scroll one page right.
		if( curpos<maxpos )
			curpos = (maxpos-page+1 < curpos+page) ? maxpos-page+1 : curpos+page;
			//min(maxpos-page+1, curpos+page);
		break;

	case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
		curpos = Message.Pos;      // of the scroll box at the end of the drag operation.
		break;

	case SB_THUMBTRACK:   // Drag scroll box to specified position. nPos is the
		curpos = Message.Pos;     // position that the scroll box has been dragged to.
		break;
	}

	// Set the new position of the thumb (scroll box).
	if( Message.ScrollBar!=m_ScrollBar->Handle ){
		m_ScrollBar->SetPosition(curpos);
//		Paint();
	}
	if( m_iTopLine != curpos ){
		m_iTopLine = curpos;
//		Invalidate();
		if( m_nAddCost>0 ) {
			InvalidateRect(this->Handle,
						   &TRect(0, LINE_HEIGHT, Width, Height-2*LINE_HEIGHT),
						   false);
		}else{
			InvalidateRect(this->Handle,
						   &TRect(0, LINE_HEIGHT, Width, Height-LINE_HEIGHT),
						   false);
		}
	}

	Message.Result = true;
}
//---------------------------------------------------------------------------

void __fastcall TPayMentPanel::WMMouseWheel(TWMMouseWheel &Message)
{
	if( m_ScrollBar && m_ScrollBar->Visible ){
		UINT nSBCode;

		if( Message.WheelDelta>0 ) nSBCode = SB_LINEUP;
		else nSBCode = SB_LINEDOWN;

		Perform( WM_VSCROLL, MAKELONG(nSBCode,0), NULL );
	}
}
//---------------------------------------------------------------------------

void __fastcall TPayMentPanel::SetContent(int iOperation, int nIdCount,
										  UINT *aIdStyle, int *aStyleCost,
										  int nCost, int nBalance)
{
	int nCostTotal = 0;
	TPROSTYLE  *tStyle = NULL;

	STYLEPAY stylePayName;
	stylePayName.strStyleName = L"风格";
	stylePayName.strStyleCost   = L"唱作音符";
	m_VCStylePay.push_back(stylePayName);

	if( !(iOperation & e_PO_Pay4Compose ) ){
		bStylePayShow = false;
	}
	else{
		for(int i=0; i<nIdCount; i++){
			tStyle = GetStyleInfo( 0, aIdStyle[i] );

			STYLEPAY stylePayStyle;
			stylePayStyle.strStyleName = UTF8ToMBCS(tStyle->pOrigin->detail.strStyleName).c_str();

			if( aStyleCost[i]>0 ){
				stylePayStyle.strStyleCost = IntToStr(aStyleCost[i]);
				nCostTotal += aStyleCost[i];

				m_VCStylePay.insert( m_VCStylePay.begin()+1, stylePayStyle );
			}
			else{
				if( aStyleCost[i]==0 )
					stylePayStyle.strStyleCost = "免费";
				else if( aStyleCost[i]==-2 )
					stylePayStyle.strStyleCost = "已支付";

				m_VCStylePay.push_back(stylePayStyle);
			}
		}
	}

	m_nAddCost = nCost - nCostTotal;
	if( m_nAddCost>0 ) {
		STYLEPAY stylePayDefault;
		stylePayDefault.strStyleName = L"功能费";
		stylePayDefault.strStyleCost   = IntToStr(m_nAddCost);
		m_VCStylePay.push_back(stylePayDefault);

		TPngImage *pPng = new TPngImage();
		m_imgInfo = new TImage(this);
		pPng->LoadFromResourceName(int(HInstance), L"imgInfo");
		m_imgInfo->Picture->Assign(pPng);
		m_imgInfo->Width = pPng->Width;
		m_imgInfo->Height = pPng->Height;
		m_imgInfo->Parent = this;
		m_imgInfo->ShowHint = true;
		SAFE_DELETE(pPng)

		String strInfo = L"功能费明细：\n";
		if( iOperation<=(e_PO_Pay4Basic|e_PO_Pay4Compose) ){
			strInfo +=     L"\n基本费           ";
			strInfo += IntToStr(m_nAddCost);
		}
		else if( iOperation & e_PO_Pay4Midi ){
			if( iOperation & e_PO_Pay4Compose ){
				strInfo += L"\n导出MIDI伴奏    ";
				strInfo += IntToStr(m_nAddCost);
			}
			else{
				strInfo += L"\n音频转MIDI      ";
				strInfo += IntToStr(m_nAddCost);
			}
		}
		else{
			int cost = m_nAddCost;
			if( iOperation & e_PO_Pay4Audios ){
				strInfo += L"\n复数音频轨道    ";
				strInfo += IntToStr( 30 );
				cost -= 30;
			}

			if( iOperation & e_PO_Pay4Fonts ){
				strInfo += L"\n加载额外音源    ";
				strInfo += IntToStr( cost );
			}
		}

		m_imgInfo->Hint = strInfo;
	}

	STYLEPAY stylePayTotal;
	stylePayTotal.strStyleName = L"合计：";
	stylePayTotal.strStyleCost   = IntToStr(nCost);
	m_VCStylePay.push_back(stylePayTotal);

	if( m_nAddCost>0 )
		m_nStyleNum = m_VCStylePay.size()-3;
	else
		m_nStyleNum = m_VCStylePay.size()-2;
}
//---------------------------------------------------------------------------

void __fastcall TPayMentPanel::CutOffString(String &cutOffStr)
{
	String strTmp  = cutOffStr;
	TSize size     = Canvas->TextExtent( strTmp );
	int   nLen     = strTmp.Length();

	if( size.Width >= STYLE_NAME_WIDTH )
	{
		nLen  = nLen*(STYLE_NAME_WIDTH)/size.Width;
		nLen -= 1;

		if(nLen <= 0){
			strTmp  = strTmp.SubString(0, 1);
		}else{
			strTmp  = strTmp.SubString(0, nLen);
			strTmp += "..";
		}

		cutOffStr = strTmp;
	}
}
//---------------------------------------------------------------------------

int __fastcall TPayMentPanel::GetLineNumber()
{
	return m_nStyleNum;
}
//---------------------------------------------------------------------------

void __fastcall TPayMentPanel::SetShowLine()
{
	if( bStylePayShow ) {
		if( m_nAddCost>0 )
			m_iShowLine = Height/LINE_HEIGHT - 3;
		else
			m_iShowLine = Height/LINE_HEIGHT - 2;
		int totalLine = GetLineNumber();
		if( totalLine>m_iShowLine  ){
			m_ScrollBar = new TSkinScrollBar(this);
			m_ScrollBar->m_pWndCtrl = this;
			m_ScrollBar->Parent = this;
			if( m_nAddCost>0 )
				m_ScrollBar->Height = Height-3*LINE_HEIGHT-2;
			else
				m_ScrollBar->Height = Height-2*LINE_HEIGHT-2;
			m_ScrollBar->Left = Width - m_ScrollBar->Width - 1;
			m_ScrollBar->Top  = LINE_HEIGHT+1;
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TPayMentPanel::SetSB()
{
	vector<STYLEPAY>::iterator vc_pFind;
	for(vc_pFind = m_VCStylePay.begin(); vc_pFind != m_VCStylePay.end(); vc_pFind++){
		CutOffString(vc_pFind->strStyleName);
	}

	if( m_ScrollBar ){
		int totalLine = GetLineNumber();
		m_ScrollBar->SetRange( 0, totalLine-1 );
		m_ScrollBar->SetPageSize( m_iShowLine );
		m_ScrollBar->SetPosition( m_iTopLine  );
		m_ScrollBar->Visible = true;
	}
}
//---------------------------------------------------------------------------

void __fastcall TPayMentPanel::SetTextFontSize(int sz)
{
    Canvas->Font->Size = sz;
}
//---------------------------------------------------------------------------

void __fastcall TPayMentPanel::Paint(void)
{
	vector<STYLEPAY>::iterator vc_pFind = m_VCStylePay.begin();
	TSize sizeFont;

	Canvas->Font->Color  = CLR_TXT_HOT;
	Canvas->Brush->Style = bsClear;

	int curX, curY;


	///////////////////////////////
	sizeFont = Canvas->TextExtent(vc_pFind->strStyleName);
	curX  = (STYLE_NAME_WIDTH - sizeFont.Width)/2;
	curY  = (LINE_HEIGHT - sizeFont.Height)/2 ;
	Canvas->TextOutW( curX, curY, vc_pFind->strStyleName );

	sizeFont = Canvas->TextExtent(vc_pFind->strStyleCost);
	curX  = STYLE_NAME_WIDTH + (STYLE_PAY_WIDTH - sizeFont.Width)/2;
	Canvas->TextOutW( curX, curY, vc_pFind->strStyleCost );

	curY = LINE_HEIGHT;

	/////////////////////////////////
	if( m_nAddCost>0 )
		curY = Height - 2*LINE_HEIGHT;
	else
		curY = Height - LINE_HEIGHT;
	Canvas->Brush->Color = CLR_BKG_INTERNAL_LIGHT;		//背景颜色
	Canvas->FillRect( TRect(1, LINE_HEIGHT,
							Width-1, curY ) );

	/////////////////////////////////
	Canvas->Font->Color  = CLR_TXT_HOT;
	Canvas->Brush->Style = bsClear;
	if( bStylePayShow ){

		vc_pFind++;
		vc_pFind = vc_pFind + m_iTopLine;
		sizeFont = Canvas->TextExtent(vc_pFind->strStyleName);
		curY = LINE_HEIGHT + (LINE_HEIGHT - sizeFont.Height)/2;

		int nShowAdd = 1;
		if( m_nAddCost>0 ) nShowAdd = 2;

		for( int i=m_iTopLine;
			 (m_nStyleNum > 0) &&
			 (i < m_nStyleNum ) &&
			 (vc_pFind != m_VCStylePay.end()-nShowAdd) &&
			 (i <= m_iTopLine + m_iShowLine -1);
			 i++, vc_pFind++ )
		{
			sizeFont = Canvas->TextExtent(vc_pFind->strStyleName);
			curX  = (STYLE_NAME_WIDTH - sizeFont.Width)/2;
			Canvas->TextOutW( curX, curY, vc_pFind->strStyleName );

			sizeFont = Canvas->TextExtent(vc_pFind->strStyleCost);
			curX  = STYLE_NAME_WIDTH + (STYLE_PAY_WIDTH - sizeFont.Width)/2;
			Canvas->TextOutW( curX, curY, vc_pFind->strStyleCost);

			curY += LINE_HEIGHT;
		}

		/////////////////////////////////
		curY = Height - nShowAdd*LINE_HEIGHT;

		Canvas->Pen->Color = CLR_CONTENT_DB_LINE_DARK;
		Canvas->MoveTo( 0, 	   curY-1 );
		Canvas->LineTo( Width, curY-1 );
		Canvas->Pen->Color = CLR_CONTENT_DB_LINE_LIGHT;
		Canvas->MoveTo( 0, 	   curY );
		Canvas->LineTo( Width, curY );
	}

	if( m_nAddCost>0 ) {
		m_imgInfo->Top = curY + 1;

		vc_pFind = m_VCStylePay.end() -2;
		sizeFont = Canvas->TextExtent(vc_pFind->strStyleName);
		curX  = (STYLE_NAME_WIDTH - sizeFont.Width)/2;
		curY  += (LINE_HEIGHT - sizeFont.Height)/2 ;
		Canvas->TextOutW( curX, curY, vc_pFind->strStyleName );
//		m_lbVip->Left = curX+sizeFont.Width+5;
//		m_lbVip->Top = curY;
		m_imgInfo->Left = curX + sizeFont.Width + 6;

		sizeFont = Canvas->TextExtent(vc_pFind->strStyleCost);
		curX  = STYLE_NAME_WIDTH + (STYLE_PAY_WIDTH - sizeFont.Width)/2;
		Canvas->TextOutW( curX, curY, vc_pFind->strStyleCost );
	}

    /////////////////////////////
	vc_pFind = m_VCStylePay.end()-1;
	sizeFont = Canvas->TextExtent(vc_pFind->strStyleName);
	curX  = (STYLE_NAME_WIDTH - sizeFont.Width)/2;
	curY  = Height - LINE_HEIGHT +
			(LINE_HEIGHT - sizeFont.Height)/2 ;

	Canvas->TextOutW( curX, curY, vc_pFind->strStyleName );

    sizeFont = Canvas->TextExtent(vc_pFind->strStyleCost);
	curX  = STYLE_NAME_WIDTH + (STYLE_PAY_WIDTH - sizeFont.Width)/2;
	Canvas->TextOutW( curX, curY, vc_pFind->strStyleCost );

	curY = Height - LINE_HEIGHT;
	Canvas->Pen->Color = CLR_CONTENT_DB_LINE_DARK;
	Canvas->MoveTo( 0, 	   curY-1 );
	Canvas->LineTo( Width, curY-1 );
	Canvas->Pen->Color = CLR_CONTENT_DB_LINE_LIGHT;
	Canvas->MoveTo( 0, 	   curY );
	Canvas->LineTo( Width, curY );
}
//---------------------------------------------------------------------------