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

#pragma hdrstop

#include "ShowUserInfoPanel.h"
#include "GlobalUnit.h"
#include "ImgCopperFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

#define BORDER_COLOR		TColor(RGB(100, 108, 119))
#define BK_COLOR			TColor(RGB(39, 42, 47))
#define SEPARATOR_COLOR		TColor(RGB(152, 154, 156))
#define FONT_COLOR			TColor(RGB(152, 154, 156))
#define FONT_COLOR_HI		TColor(RGB(255, 255, 255))

#define VIPHEIGHT  24
#define SHOWHEIGHT 190
//---------------------------------------------------------------------------
const static String LabelStr[] = {
	L"唱作音符数",
    L"魅力值",
    L"粉丝数",
	L"歌曲数",
	L"空间未读消息",
	L"注销",
	L"个人空间"
};
const static TPoint FrameRoute[] = {
	TPoint(0, 5),
	TPoint(225, 5),
	TPoint(230, 0),
	TPoint(235, 5),
	TPoint(250, 5),
	TPoint(250, SHOWHEIGHT),
	TPoint(0, SHOWHEIGHT)
};
const static String ResStr[] = {
	L"UserInfo_Coin",
    L"UserInfo_Pops",
    L"UserInfo_Fans",
	L"UserInfo_Music",
	L"UserInfo_Zone",
	L"UserInfo_Icon",
};
//---------------------------------------------------------------------------
__fastcall TShowUserInfo::TShowUserInfo(TComponent *Owner)
	: TWinControl(Owner)
{
    Parent = Application->MainForm;

    ApplicationEvents = new TApplicationEvents(Application->MainForm);
    ApplicationEvents->OnMessage = DoHandleMessage;

//    FBalance = 0;
//    FPops = 0;
//    FFans = 0;
//	FSongs = 0;

    m_ImageList = new TList();
    m_ValueList = new TList();

	TLabel *Label = new TLabel(this);
	//Label->Color =
	Label->Transparent = true;
    Label->Top = 102;//+VIPHEIGHT;
	Label->Left = 50;// + 25;
    Label->Width = 100;
    Label->WordWrap = false;
    Label->AutoSize = false;
    Label->Font->Color = FONT_COLOR;
    Label->Font->Size = g_szFont10;
    Label->Alignment = taCenter;
    Label->Caption = "";
    Label->Parent = this;
    Label->ShowHint = true;
	Label->EllipsisPosition = epEndEllipsis;
    Label->OnMouseEnter = DoLabelMouseEnter;
    Label->OnMouseLeave = DoLabelMouseLeave;
    Label->Cursor  = crHandPoint;
    Label->OnClick = DoObjectClick;
    m_ValueList->Add(Label);

	TPngImage *pngImage = new TPngImage();
	pngImage->LoadFromResourceName(int(HInstance), ResStr[5]);

	m_UserIcon = new TImage(this);
	m_UserIcon->Picture->Graphic = pngImage;
	m_UserIcon->Width = 64;
	m_UserIcon->Height = 64;
	m_UserIcon->Left = 68;// + 25;
	m_UserIcon->Top = 30;
	m_UserIcon->ShowHint = true;
	m_UserIcon->Hint = "";
	m_UserIcon->Parent = this;
//	m_UserIcon->AutoSize = false;
//	m_UserIcon->Center = true;
	m_UserIcon->Stretch = true;
	m_UserIcon->Cursor  = crHandPoint;
	m_UserIcon->OnClick = DoObjectClick;

	m_imgVip = new TImage(this);
	m_imgVip->Width = 35;
	m_imgVip->Height = VIPHEIGHT - 8;
	m_imgVip->Top = 30 + 2; //m_UserIcon->Top+m_UserIcon->Height+10;
	m_imgVip->Left = 132 + 15;//(Width-m_imgVip->Width)/2;
	m_imgVip->Parent = this;
	m_imgVip->OnClick = DoObjectClick;
	m_imgVip->Stretch = true;
	m_imgVip->Cursor  = crHandPoint;
	m_imgVip->ShowHint = true;

	TPngImage *pImgVip = new TPngImage();
//	m_imgVip->Visible = false;
	if(-1==g_infoUser.accountInfo.vipPeriod){
		pImgVip->LoadFromResourceName(int(HInstance), L"vipForever");
		m_imgVip->Hint = L"终身VIP会员";

	}else if( 0==g_infoUser.accountInfo.vipPeriod ) {
		pImgVip->LoadFromResourceName(int(HInstance), L"vipNone");
		m_imgVip->Hint = L"点击成为VIP会员\n成为VIP之后请重新登录";
	}else {
		pImgVip->LoadFromResourceName(int(HInstance), L"vipTimer");
		String strDay = L"VIP权限剩余";
		int nDay = g_infoUser.accountInfo.vipPeriod/(60*60*24);
		int nAdd = g_infoUser.accountInfo.vipPeriod%(60*60*24)==0?0:1;
		strDay += IntToStr(nDay+nAdd);
		strDay += L"天\n点击延期，之后请重新登录";
		m_imgVip->Hint = strDay;
	}
	m_imgVip->Picture->Assign(pImgVip);
	SAFE_DELETE(pImgVip)

//	for(int i = 0; i < 4; i++)
	{
		TPngImage *png = new TPngImage();
        png->LoadFromResourceName(int(HInstance), ResStr[0]);

        TImage *Image = new TImage(this);
        Image->Picture->Graphic = png;
        Image->Top = 60+16;//125+VIPHEIGHT;
		Image->Left = 132 + 10;//23 + i * 60;
		Image->Width = 20;
		Image->Height = 16;
		Image->Parent = this;
        Image->Stretch = true;
		Image->ShowHint = true;
		Image->Hint = "唱作音符数\n点击充值兑换，之后请重新登录";
   	    Image->Cursor  = crHandPoint;
        Image->OnClick = DoObjectClick;
		m_ImageList->Add(Image);

		Label = new TLabel(this);
		Label->Transparent = true;
		Label->Top = 60 +16;//155+VIPHEIGHT;
		Label->Left = 132 + 30 + 2; //5 + i * 60;
        Label->Width = 60;
		Label->WordWrap = false;
        Label->AutoSize = false;
		Label->EllipsisPosition = epEndEllipsis;
		Label->Font->Color = clYellow;
        Label->Font->Name = g_strTxtFontName;
		Label->Font->Size = g_szFont11;
		Label->Alignment = taLeftJustify;
		Label->Caption = IntToStr(g_infoUser.accountInfo.balance);
		Label->Parent = this;
        Label->OnMouseEnter = DoLabelMouseEnter;
    	Label->OnMouseLeave = DoLabelMouseLeave;
	    Label->Cursor  = crHandPoint;
        Label->OnClick = DoObjectClick;
        Label->Hint = "唱作音符数\n点击充值兑换，之后请重新登录";
        Label->ShowHint = true;
		m_ValueList->Add(Label);
	}

	m_LogoutBtn = new TTextButton(this);
	m_LogoutBtn->Width = 60;
	m_LogoutBtn->Text = LabelStr[5];
	m_LogoutBtn->Top = 125+VIPHEIGHT;
	m_LogoutBtn->Left = 95;// + 25;
	m_LogoutBtn->Parent = this;
	m_LogoutBtn->ShowHint = true;
	m_LogoutBtn->Hint = "退出登录";
	m_LogoutBtn->FontSize = g_szFont9;
	m_LogoutBtn->OnClick = DoObjectClick;


//	m_ZoneBtn = new TTextButton(this);
//	m_ZoneBtn->Width = 60;
//	m_ZoneBtn->Text = LabelStr[6];
//	m_ZoneBtn->Top = 190+VIPHEIGHT;
//	m_ZoneBtn->Left = 105 + 25;
//	m_ZoneBtn->Parent = this;
//	m_ZoneBtn->FontSize = g_szFont9;
//	m_ZoneBtn->OnClick = DoButtonClick;

	SetTransparent( this );
}
//---------------------------------------------------------------------------
__fastcall TShowUserInfo::~TShowUserInfo()
{
	if(m_ImageList){
		m_ImageList->Clear();
		delete m_ImageList;
		m_ImageList = NULL;
	}
	if(m_ValueList){
		m_ValueList->Clear();
		delete m_ValueList;
		m_ValueList = NULL;
	}
	if(m_LogoutBtn){
		delete m_LogoutBtn;
		m_LogoutBtn = NULL;
	}
//	if(m_ZoneBtn){
//		delete m_ZoneBtn;
//		m_ZoneBtn = NULL;
//	}
	if(ApplicationEvents){
		delete ApplicationEvents;
		ApplicationEvents = NULL;
	}

	if(m_imgVip){
		delete m_imgVip;
		m_imgVip = NULL;
	}

}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::CreateParams(TCreateParams &Params)
{
    TWinControl::CreateParams(Params);

    Params.Style = 0;
    Params.Style = WS_POPUP;
}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::CreateWnd()
{
	Width = 250;
	Height = SHOWHEIGHT;

	TWinControl::CreateWnd();

	HRGN hRgn;
	hRgn = CreatePolygonRgn(FrameRoute,7, ALTERNATE);
	SetWindowRgn(Handle, hRgn, true);
    DeleteObject(hRgn);
}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::DoObjectClick(TObject *Sender)
{
    if(Sender == m_LogoutBtn)
    	m_ModalResult = 1;
    else if(Sender == m_UserIcon || Sender == m_ValueList->Items[0])
		m_ModalResult = 2;
    else if(Sender == m_imgVip )
        m_ModalResult = 3;
    else if(Sender == m_ValueList->Items[1] || Sender==m_ImageList->Items[0])
        m_ModalResult = 4;

	Hide();
}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::DoHandleMessage(MSG &Msg, bool &Handled)
{
	if(Msg.message == WM_NCLBUTTONDOWN || Msg.message == WM_NCRBUTTONDOWN)
    {
        m_ModalResult = -1;
        Hide();
    }
    if(Msg.message == WM_LBUTTONDOWN || Msg.message == WM_RBUTTONDOWN)
    {
        if(Msg.hwnd != Handle)
		{
           m_ModalResult = -1;
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::DoLabelMouseEnter(TObject *Sender)
{
    ((TLabel *)Sender)->Font->Color = FONT_COLOR_HI;
    ((TLabel *)Sender)->Font->Style = TFontStyles()<<fsUnderline;
}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::DoLabelMouseLeave(TObject *Sender)
{
    ((TLabel *)Sender)->Font->Color = FONT_COLOR;
    ((TLabel *)Sender)->Font->Style = TFontStyles();
}
//---------------------------------------------------------------------------

void __fastcall TShowUserInfo::SetUserName(String Value)
{
	if(FUserName != Value) {
        FUserName = Value;
        ((TLabel *)m_ValueList->Items[0])->Caption = Value;

        String strHint = "用户名：";
        strHint += g_infoUser.strUserName;
        strHint += "\n昵   称：";
        strHint += g_infoUser.strNickName;
        strHint += "\n音符数：";
        strHint += IntToStr(g_infoUser.accountInfo.balance);
        strHint += "\n\n点击查看详情";
		m_UserIcon->Hint = strHint;

        ((TLabel *)m_ValueList->Items[0])->Hint = strHint;
    }
}
//---------------------------------------------------------------------------

//void __fastcall TShowUserInfo::SetBalance(int Value)
//{
//	if(FBalance != Value) {
//    	FBalance = Value;
//		((TLabel *)m_ValueList->Items[1])->Caption = IntToStr(Value);
//    }
//}
////---------------------------------------------------------------------------
//void __fastcall TShowUserInfo::SetPops(int Value)
//{
//	if(FPops != Value) {
//    	FPops = Value;
//    	((TLabel *)m_ValueList->Items[2])->Caption = IntToStr(Value);
//    }
//}
////---------------------------------------------------------------------------
//void __fastcall TShowUserInfo::SetFans(int Value)
//{
//	if(FFans != Value) {
//    	FFans = Value;
//    	((TLabel *)m_ValueList->Items[3])->Caption = IntToStr(Value);
//    }
//}
////---------------------------------------------------------------------------
//void __fastcall TShowUserInfo::SetSongs(int Value)
//{
//	if(FSongs != Value) {
//    	FSongs = Value;
//    	((TLabel *)m_ValueList->Items[4])->Caption = IntToStr(Value);
//    }
//}
////---------------------------------------------------------------------------
//void __fastcall TShowUserInfo::SetZones(int Value)
//{
//	if(FZones != Value){
//		FZones = Value;
//		((TLabel *)m_ValueList->Items[5])->Caption = IntToStr(Value);
//    }
//}
////---------------------------------------------------------------------------
int __fastcall TShowUserInfo::ShowModal()
{
	Application->ModalStarted();

    m_bakActiveWnd = GetActiveWindow();

    TWinControl::Show();

    String nickName = String(g_infoUser.strNickName);

	if(nickName.IsEmpty()) {
		UserName = String(g_infoUser.strUserName);//L"ID:" + IntToStr(g_infoUser.accountInfo.ID);
	}
	else {
    	UserName = nickName;
    }

//    Balance = g_infoUser.accountInfo.balance;
//    Pops = g_infoUser.accountInfo.pops;
//    Fans = g_infoUser.accountInfo.fans;
//	Songs = g_infoUser.accountInfo.songs;
//	Zones = f_CS_GetNewNoticeNum( g_infoUser.accountInfo.ID );

	String strHeaderPath = g_strUserPath.c_str();
	strHeaderPath += L"header\\";

	String strIconFile = strHeaderPath + g_infoUser.strIconFile;
	if( FileExists( strIconFile ) ) {
		GetPic(strIconFile, true);
		m_UserIcon->Picture->LoadFromFile( strIconFile );
	}

    m_ModalResult = 0;

    m_IsActive = false;
    SendMessage(Handle, WM_ACTIVATE, WA_ACTIVE, 0);
    m_IsActive = true;

    while(m_ModalResult == 0) {
    	Application->HandleMessage();

        if(Application->Terminated)
        	m_ModalResult = -1;
    }

    TWinControl::Hide();

    SetActiveWindow(m_bakActiveWnd);

    Application->ModalFinished();

    return m_ModalResult;
}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::WMEraseBkgnd(TWMEraseBkgnd &Message)
{
    TCanvas *canvas = new TCanvas();
    canvas->Handle = Message.DC;
    /* 绘制背景 */
	canvas->Brush->Color = BK_COLOR;
    canvas->FillRect(GetClientRect());
    /* 绘制边框 */
    canvas->Pen->Color = BORDER_COLOR;
    canvas->MoveTo(FrameRoute[0].X, FrameRoute[0].Y);
    canvas->LineTo(FrameRoute[1].X, FrameRoute[1].Y);
    canvas->MoveTo(FrameRoute[1].X, FrameRoute[1].Y);
    canvas->LineTo(FrameRoute[2].X, FrameRoute[2].Y);
    canvas->MoveTo(FrameRoute[2].X, FrameRoute[2].Y);
    canvas->LineTo(FrameRoute[3].X, FrameRoute[3].Y);
    canvas->MoveTo(FrameRoute[3].X, FrameRoute[3].Y);
    canvas->LineTo(FrameRoute[4].X-1, FrameRoute[4].Y);
    canvas->MoveTo(FrameRoute[4].X-1, FrameRoute[4].Y);
    canvas->LineTo(FrameRoute[5].X-1, FrameRoute[5].Y-1);
    canvas->MoveTo(FrameRoute[5].X-1, FrameRoute[5].Y-1);
    canvas->LineTo(FrameRoute[6].X, FrameRoute[6].Y-1);
    canvas->MoveTo(FrameRoute[6].X, FrameRoute[6].Y-1);
    canvas->LineTo(FrameRoute[0].X, FrameRoute[0].Y);
	/* 绘制分割线 */
    canvas->Pen->Color = SEPARATOR_COLOR;
	canvas->MoveTo(10, 108+VIPHEIGHT);
	canvas->LineTo(240, 108+VIPHEIGHT);

    delete canvas;
}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::WMKillFocus(TMessage &Message)
{
	if(m_IsActive && m_ModalResult == 0)
    	m_ModalResult = -1;
}
//---------------------------------------------------------------------------
void __fastcall TShowUserInfo::WMPaint(TWMPaint &Message)
{
    PaintHandler(Message);
}
//---------------------------------------------------------------------------
//void __fastcall TShowUserInfo::UserIconClick(TObject *Sender)
//{
//	m_ModalResult = 2;
//	Hide();
//
//	String strHeaderPath = g_strUserPath.c_str();
//	strHeaderPath += L"header\\";
//
//	String strIconFile = strHeaderPath + g_infoUser.strIconFile;
//	if( !FileExists( strIconFile ) )
//		strIconFile = strHeaderPath + L"default.jpg";
//	GetPic(strIconFile, true);
//
//	TPicture *pPic = new TPicture();
//	pPic->LoadFromFile( strIconFile );
//
//	TImgCopperForm *pTImgCopperForm = new TImgCopperForm(this, TRect(0, 0, 90, 90), pPic);
//	pTImgCopperForm->Caption = L"编辑头像";
//	pTImgCopperForm->Width  = 260;
//	pTImgCopperForm->Height = 300;
//
//	if( mrOk==pTImgCopperForm->ShowModal() ) {
//
//		bool bUploadUsrIcon = false;
//		if( NULL==g_pTThreadHttpPostUsrIcon ) {
//
//			bUploadUsrIcon = true;
//
//		}else if(  !g_pTThreadHttpPostUsrIcon->Started ||
//				   (g_pTThreadHttpPostUsrIcon->Started && g_pTThreadHttpPostUsrIcon->Finished) )
//		{
//			bUploadUsrIcon = true;
//		}
//
//		if( bUploadUsrIcon ){
//
//			SAFE_DELETE(g_pTThreadHttpPostUsrIcon)
//			g_pTThreadHttpPostUsrIcon
//			= new TThreadHttpPostUsrIcon(true, this,
//										 pTImgCopperForm->m_strCopperPath);
//			g_pTThreadHttpPostUsrIcon->Priority = tpIdle;
//			g_pTThreadHttpPostUsrIcon->Start();
//		}
//	}
//
//	SAFE_DELETE(pTImgCopperForm);
//	SAFE_DELETE(pPic);
//}
//---------------------------------------------------------------------------

//void __fastcall TShowUserInfo::VIPClick(TObject *Sender)
//{
//	char strURL[256];
//	f_CS_GetWebPageURL( URL_VIPMEMBER, strURL );
//	String urlVIP = strURL;
//	if( urlVIP.Length() ) {
//		ShellExecute(Handle, NULL, urlVIP.c_str(), NULL, NULL, SW_SHOWNORMAL);
//	}
//}
////---------------------------------------------------------------------------
