//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "HelpFormUnit.h"
#include "GlobalUnit.h"
#include "MessageDef.h"
#include "MessageFormUnit.h"
#include "ProgressFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "PNGButton"
#pragma link "CapButton"
#pragma resource "*.dfm"
THelpForm *HelpForm;
//---------------------------------------------------------------------------
#define WINDOW_MAX 		0
#define WINDOW_NORMAL   1
#define WINDOW_MIN		2
#define WNDOW_CLOSE		3

//---------------------------------------------------------------------------
__fastcall TTabButton::TTabButton(System::Classes::TComponent* AOwner)
	:	TPNGButton( AOwner )
{
	Caption = "";
}
//---------------------------------------------------------------------------

__fastcall  TTabButton::~TTabButton(void)
{

}
//---------------------------------------------------------------------------

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

void __fastcall TTabButton::Paint(void)
{
	TPNGButton::Paint();

	Canvas->Brush->Style = bsClear;
	Canvas->Font->Color = RGB( 184,188,189 );
	Canvas->Font->Name = g_strTxtFontName;
	Canvas->Font->Quality = TFontQuality::fqClearTypeNatural;
//	if( !strcmp( g_strTxtFontName, "微软雅黑" ) ){
//		Canvas->Font->Size = 10;
//	}
//	else{
//		Canvas->Font->Size = 9;
//	}

	int nLength = Canvas->TextWidth( Caption );
	int nHeight = Canvas->TextHeight( Caption );
	Canvas->TextOutW( Width/2 - nLength/2 - 5,Height/2 - nHeight/2,Caption  );
}

//---------------------------------------------------------------------------

__fastcall THelpForm::THelpForm(TComponent* Owner)
	: TForm(Owner)
{
	Window_state = WINDOW_NORMAL;
	m_nUserNoticeNum = 0;
	m_PngImage = new TPngImage();
	m_PngImage->LoadFromResourceName(int(HInstance), L"HelpPNG");
	m_PanelShow = NULL;
}
//---------------------------------------------------------------------------


void __fastcall THelpForm::FormClose(TObject *Sender, TCloseAction &Action)
{
	PostMessage( Application->MainForm->Handle, UM_SHOWHELP, false, 0 );
}
//---------------------------------------------------------------------------


void __fastcall THelpForm::RestClick(TObject *Sender)
{

	SetSize( HLP_WINDOW_NORMAL );
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::MinClick(TObject *Sender)
{
	SetSize( HLP_WINDOW_MIN );
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::MaxClick(TObject *Sender)
{
	SetSize( HLP_WINDOW_MAX );
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::SetSize( int iState )
{
	Window_state = iState;

	switch( iState )
	{
		case HLP_WINDOW_MAX:
			SetBounds( Application->MainForm->Left, Application->MainForm->Top + 44,
						Application->MainForm->Width, Application->MainForm->Height - 44 );
		break;

		case HLP_WINDOW_MIN:
			SetBounds(  Application->MainForm->Left + Application->MainForm->Width - 160,
						Application->MainForm->Top + Application->MainForm->Height - 34,
						150, 24 );
		break;

		case HLP_WINDOW_NORMAL:
			SetBounds(  Application->MainForm->Left + Application->MainForm->Width/2 - 389,
						Application->MainForm->Top + Application->MainForm->Height/2 - 220,
						778, 440 );
		break;
	}

	m_btnMax->Visible = iState==HLP_WINDOW_NORMAL;
	m_btnMin->Visible = iState!= HLP_WINDOW_MIN;
	m_btnRest->Visible = iState!=HLP_WINDOW_NORMAL;
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::FormResize(TObject *Sender)
{
	switch( Window_state ){
		case HLP_WINDOW_MIN:
			m_lblGroupLabel->Top = 50;
			m_lblGroup->Top = 50;
			m_lblVideoLabel->Top = 50;
			m_lblVideo->Top = 50;
			m_lblUrlLabel->Top = 50;
			m_lblUrl->Top = 50;
		break;

		case HLP_WINDOW_MAX:
		case HLP_WINDOW_NORMAL:
			m_PanelShow->SetBounds( 40, 67, Width - 80, Height - 120 );
			m_memMsg->SetBounds( 15, 15, m_PanelShow->Width - 45, 130 );

			m_btnSubmit->Top = m_memMsg->Top + m_memMsg->Height + 5;
			m_btnSubmit->Left = m_memMsg->Left + m_memMsg->Width - m_btnSubmit->Width;

			m_lblGroupLabel->Top = Height - 35;
			m_lblGroup->Top = m_lblGroupLabel->Top;
			m_lblVideoLabel->Top = m_lblGroupLabel->Top;
			m_lblVideo->Top = m_lblGroupLabel->Top;
			m_lblUrlLabel->Top = m_lblGroupLabel->Top;
			m_lblUrl->Top = m_lblGroupLabel->Top;
		break;
	}
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::FormCreate(TObject *Sender)
{
	m_GenerPanel->Font->Size = g_szFont9;

	for( int i=0;i<TAB_NUM;i++ ){
		m_arrTabs[i] = new TTabButton(this);
		m_arrTabs[i]->Parent = m_GenerPanel;
		m_arrTabs[i]->ImgStates = 4;
		m_arrTabs[i]->PngImg = m_PngImage;//LoadFromResourceName( int(HInstance), L"HelpPNG" );
		m_arrTabs[i]->Top = 40;
		m_arrTabs[i]->SetFontSize(g_szFont10);
        m_arrTabs[i]->OnClick = TabClick;
        m_arrTabs[i]->Tag = i;

        if(i==2){
			m_arrTabs[i]->Left = 78;
            m_arrTabs[i]->Visible = false;
		}
		else if(i>2)
			m_arrTabs[i]->Left = 78 + 70*(i-1);
        else
        	m_arrTabs[i]->Left = 78 + 70*i;



//		if( i<=TAB_SW )
//			m_arrTabs[i]->OnClick = TabClick;
//		else if( i==TAB_MN )
//			m_arrTabs[i]->OnClick = HelpClick;
//		else if ( i==TAB_AB )
//			m_arrTabs[i]->OnClick = AboutClick;

		m_arrTabs[i]->SendToBack();
	}

	m_arrTabs[TAB_QA]->Caption = "常见问题";
	m_arrTabs[TAB_FB]->Caption = "我要提问";
	m_arrTabs[TAB_NT]->Caption = "网站消息";
	m_arrTabs[TAB_CN]->Caption = "唱作音符";
	m_arrTabs[TAB_SW]->Caption = "更多软件";
	m_arrTabs[TAB_MN]->Caption = "用户手册";
	m_arrTabs[TAB_AB]->Caption = "关于";
	m_arrTabs[TAB_QA]->Down = true;

	m_PanelShow = new THelpPanel(this);
	m_PanelShow->Parent = this;
	m_PanelShow->Left = 40;
	m_PanelShow->Top = 67;
	m_PanelShow->Width = 697;
	m_PanelShow->Height = 320;
	m_PanelShow->OnClick = panelShowClick;
	m_PanelShow->Visible = true;
	m_PanelShow->SetTextFontSize(m_memMsg->Font->Size);

	m_memMsg->Parent = m_PanelShow;
	m_memMsg->Left = 15;
	m_memMsg->Top = 15;
	m_memMsg->Width = m_PanelShow->Width - 45;
	m_memMsg->Height = 130;
	m_memMsg->Enabled = !g_bUpdateMandatory;

	m_btnSubmit->Parent =  m_PanelShow;
	m_btnSubmit->Left = m_memMsg->Left + m_memMsg->Width - m_btnSubmit->Width;
	m_btnSubmit->Top = m_memMsg->Top + m_memMsg->Height + 5;
	m_btnSubmit->FontSize = m_memMsg->Font->Size;

	m_lblMsgHead->Parent = m_PanelShow;
	m_lblMsgHead->Left = m_PanelShow->Left + 15;
	m_lblMsgHead->Top = 15;

	m_lblMsgNum->Parent = m_PanelShow;
	m_lblMsgNum->Left = m_lblMsgHead->Left + m_lblMsgHead->Width+1;
	m_lblMsgNum->Top = 15;

	m_lblMsgMiddle->Parent = m_PanelShow;
	m_lblMsgMiddle->Left = m_lblMsgNum->Left + m_lblMsgNum->Width+1;
	m_lblMsgMiddle->Top = 15;

	m_lblMsgURL->Parent = m_PanelShow;
	m_lblMsgURL->Left = m_lblMsgMiddle->Left + m_lblMsgMiddle->Width+1;
	m_lblMsgURL->Top = 15;

	m_lblMsgTail->Parent = m_PanelShow;
	m_lblMsgTail->Left = m_lblMsgURL->Left + m_lblMsgURL->Width+1;
	m_lblMsgTail->Top = 15;

	m_pImgNoticeTip = new TPngImage();
	m_pImgNoticeTip->LoadFromResourceName(int(HInstance), "NoticeBK");

	m_PanelShow->SetMode(0);

	SetTransparent( this );
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::FormActivate(TObject *Sender)
{
//	m_lblUrl->Caption = g_strServerURL;

//	m_pbUserNoticeNum->Visible = (m_nUserNoticeNum>0);

	m_PanelShow->InitialVars();
	m_PanelShow->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::FormDeactivate(TObject *Sender)
{
//	m_PanelShow->SaveFirstRow();
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::panelShowClick(TObject *Sender)
{
	m_PanelShow->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::SetNoticeNum( int num, int iType )
{
//	if( iType==1 && m_nUserNoticeNum!=num ){
//		m_nUserNoticeNum = num;
//
//		m_pbUserNoticeNum->Visible = (num>0);
//		if( num>0 ){
//			m_pbUserNoticeNum->Invalidate();
//		}
//
//		if( m_arrTabs[TAB_NT]->Down ){
//			m_lblMsgNum->Caption = m_nUserNoticeNum;
//
//			m_lblMsgHead->Visible = (num>0);
//			m_lblMsgMiddle->Visible = (num>0);
//			m_lblMsgNum->Visible = (num>0);
//			m_lblMsgURL->Visible = (num>0);
//			m_lblMsgTail->Visible = (num>0);
//
//			m_PanelShow->InitialVars();
//		}
//	}
}
//---------------------------------------------------------------------------

int __fastcall THelpForm::GetNoticeNum( int iType )
{
	if( iType!=0 )
		return m_nUserNoticeNum;

	return 0;
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::SetPage( int iPage )
{
	for( int i=0; i<TAB_NUM; i++ ){
		if( i==iPage ){
			m_arrTabs[i]->Down = true;
			m_arrTabs[i]->BringToFront();
		}
		else{
			if( m_arrTabs[i]->Down )
				m_arrTabs[i]->Down = false;
			m_arrTabs[i]->SendToBack();
		}
	}

//	m_pbUserNoticeNum->BringToFront();

	if( iPage==TAB_NT ){
		m_lblMsgNum->Caption = m_nUserNoticeNum;
	}
	else if( iPage==TAB_FB && ( m_memMsg->Text.IsEmpty()
					|| m_memMsg->Font->Color==clGray )){
		 m_bMemoHint = true;
		 m_memMsg->Text = "请在此处输入您的问题";
		 m_memMsg->Font->Color = clGray;
	}

	m_lblMsgHead->Visible = (iPage==TAB_NT && m_nUserNoticeNum>0);
	m_lblMsgMiddle->Visible = (iPage==TAB_NT && m_nUserNoticeNum>0);
	m_lblMsgNum->Visible = (iPage==TAB_NT && m_nUserNoticeNum>0);
	m_lblMsgURL->Visible = (iPage==TAB_NT && m_nUserNoticeNum>0);
	m_lblMsgTail->Visible = (iPage==TAB_NT && m_nUserNoticeNum>0);

	m_btnSubmit->Visible = (iPage==TAB_FB);
	m_memMsg->Visible = (iPage==TAB_FB);

	m_PanelShow->SetMode( iPage );
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::NoticeNumPaint(TObject *Sender)
{
	TPaintBox* pPB = (TPaintBox*)Sender;
	String strText = IntToStr( m_nUserNoticeNum );

	pPB->Canvas->Draw(0, 0, m_pImgNoticeTip);
	pPB->Canvas->Brush->Style = bsClear;
	pPB->Canvas->Font->Color = clWhite;

	int txtWidth = pPB->Canvas->TextWidth( strText );
	int txtHeight = pPB->Canvas->TextHeight( strText );

	pPB->Canvas->TextOut( (pPB->Width - txtWidth)/2.0+0.5,
						  (pPB->Height -txtHeight)/2.0,
						   strText );
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::TabClick(TObject *Sender)
{
	int mode = ((TTabButton*)Sender)->Tag;

    if(mode==TAB_SW){
    	PostMessage(Application->MainForm->Handle, UM_SHOWMORE, 0, 0);
    }
    else if(mode==TAB_MN){
    	PostMessage(Application->MainForm->Handle, UM_OPENMANUAL, 0, 0);
    }
    else if(mode==TAB_AB){
        PostMessage(Application->MainForm->Handle, UM_SHOWABOUT, 0, 0);
    }
    else{
		SetPage( mode );
    }
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::UrlClick(TObject *Sender)
{
	if( Sender==m_lblUrl && m_lblUrl->Caption.Length()>0 ){
		ShellExecute(Handle,NULL, m_lblUrl->Caption.c_str(), NULL,NULL,SW_SHOWNORMAL);
	}
	else if( Sender==m_lblVideo ){
		ShellExecute(Handle,NULL, L"http://v.qq.com/boke/page/t/8/p/t01266qkg8p.html",
						NULL,NULL,SW_SHOWNORMAL);
	}
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::SubmitClick(TObject *Sender)
{
	TProgressForm *frmProgress = new TProgressForm(NULL);

	int ret = frmProgress->User_SubmitQuestin( GBToUTF8(AnsiString(m_memMsg->Text.Trim().c_str()).c_str()).c_str() );
	int eExtra = frmProgress->GetOpertaionResult();
	delete frmProgress;

	if( ret==mrOk && eExtra==0 ){
		m_PanelShow->AddFeedback( m_memMsg->Text.Trim().c_str() );
		m_memMsg->Text = "";
	}
	else if( eExtra!=0 && eExtra!=e_UserCanceled ){
		MessageForm->ShowMessage( "由于网络故障，信息提交失败。\n\n请稍后重试!",
										MB_ICONERROR|MB_OK );
	}
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::MemoMsgChange(TObject *Sender)
{
//
	if( !m_bMemoHint && !m_memMsg->Text.Trim().IsEmpty() ){
		m_btnSubmit->Enabled = true;
	}
	else{
 		m_btnSubmit->Enabled = false;
    }
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::MemoMsgClick(TObject *Sender)
{
//
	if( m_bMemoHint ){
		m_memMsg->Text = "";
		m_memMsg->Font->Color = clBlack;
		m_bMemoHint = false;
	}
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::MsgURLClick(TObject *Sender)
{
	char strURL[256];
	f_CS_GetWebPageURL( URL_USERNOTICE, strURL );
	ShellExecute(Handle, NULL, ((String)strURL).c_str(), NULL, NULL, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
	case VK_ESCAPE:
		{
			PostMessage( Application->MainForm->Handle, UM_SHOWHELP, false, 0 );
		}
		break;
	}
}
//---------------------------------------------------------------------------

void __fastcall THelpForm::WMNCLButtonDown(Winapi::Messages::TWMNCLButtonDown &Message)
{
	if( Window_state==HLP_WINDOW_NORMAL )
		TForm::Dispatch( &Message );
}
//---------------------------------------------------------------------------



void __fastcall THelpForm::GenerPanelMouseEnter(TObject *Sender)
{
	THelpForm::SetFocus();
	Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------


