#define UTIL_STATISTICS_H aaa
//----------------------------------------------------------------------------
//  @brief
//        this file include procedures for working with after-level statistics
//        

//  @ingroup
    
//  2005Jan24 created. Zmanovsky Peter.
//  2005Jul12 *modified for Theseus.
//  (C) 2005    Sigma-Team
//-----------------------------------------------------------------------------

#ifndef MONSTERS_LIST_H
    #include "maps\monsters_list.lgc"
#endif


//------------------------------------------------------------------------------------------
#define false				0
#define true				1
#define bool				int

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

static int StartEffectTime2 = 0;
static int ShowStatTime = 0;

//vid's of the needed dummies (здесь пока не надо)
//#define NVID_SECRET_DUMMY						1358
//#define NVID_MISSION_DUMMY						1357

//------------------------------------------------------------------------------------------
#define VSTAT_MPEOPLEFOUND			0
#define VSTAT_MPEOPLECNT			1
#define VSTAT_MMONSTERSKILLED		2
#define VSTAT_MMONSTERSCNT			3
#define VSTAT_MTIME					4
//#define VSTAT_MMONEY				5
#define VSTAT_MMONEYMISSION			6
#define VSTAT_MMONEYMISSION2		7
#define VSTAT_POINTS				8

#define VSTAT_MCNT					9


//------------------------------------------------------------------------------------------
static string VSTAT_NAMES		[VSTAT_MCNT];


//------------------------------------------------------------------------------------------
#define STAT_PEOPLE					0
#define STAT_MONSTERS				1
#define STAT_TIME					2
#define STAT_MONEYMISSION			3
#define STAT_MONEYMISSION2			4
#define STAT_POINTS					5

#define STAT_CNT					6


//------------------------------------------------------------------------------------------
static int STATBTN_DIRS  [STAT_CNT];
static int STATBTN_DIRS2 [STAT_CNT];


//------------------------------------------------------------------------------------------
static int OldStatData[ VSTAT_MCNT ];
//is used for some values
static int InitStatData[ VSTAT_MCNT ];
//
static int CurStatData[ VSTAT_MCNT ];
//
static string StatData[ STAT_CNT ];
static string StatData2[ STAT_CNT ];
static string StatDebugData[ STAT_CNT ];
static bool bStatShown = false;

#define STAT_TYPE_CAMPAIGN	0
#define STAT_TYPE_SURVIVE	1

static int  iStatType = STAT_TYPE_CAMPAIGN;

//------------------------------------------------------------------------------------------
ShowStatScreen( int StatType = STAT_TYPE_CAMPAIGN )
{
	iStatType = StatType;
	//
	ShowStatTime = GetTime();
	SetScrollType( SCROLL_NONE );
	Pause( 1 );
	SetCursor( CURSOR_NORMAL );
	StartEffectTime2 = GetTime() - FADE_GAME_TIME / 2 - 1;
	Effect(EFF_FADE,0,0,FADE_GAME_TIME);
	bStatShown = true;
	Log("zm: show stat");
}


//------------------------------------------------------------------------------------------
HideStatScreen()
{
	if( GetTime() - ShowStatTime < 1500 )	//задержка перед исчезновением
		return;
	//
	//MenuRelease();
	//SetScrollType( SCROLL_TO_FLAGMAN_WITH_DIRECTION );
	//Pause( 0 );
	bStatShown = false;
	Log("zm: hide stat");
	//HideAllSprites( 0 );
}


//------------------------------------------------------------------------------------------
ToggleStatScreen()
{
	if( bStatShown )
		HideStatScreen();
	else
		ShowStatScreen();
}


//------------------------------------------------------------------------------------------
InitStatIfNeeded()
{
	iff( 1 )
	{
		int i;
		for( i = 0; i < VSTAT_MCNT; i++ )
		{
			InitStatData[ i ] = 0;
			OldStatData[ i ] = 0;
			CurStatData[ i ] = 0;
		}
		
		VSTAT_NAMES[0] = "VSTAT_MPEOPLEFOUND";
		VSTAT_NAMES[1] = "VSTAT_MPEOPLECNT";
		VSTAT_NAMES[2] = "VSTAT_MMONSTERSKILLED";
		VSTAT_NAMES[3] = "VSTAT_MMONSTERSCNT";
		VSTAT_NAMES[4] = "VSTAT_MTIME";
		VSTAT_NAMES[5] = "VSTAT_MMONEY";
		VSTAT_NAMES[6] = "VSTAT_MMONEYMISSION";
		VSTAT_NAMES[7] = "VSTAT_MMONEYMISSION2";
		VSTAT_NAMES[8] = "VSTAT_POINTS";
		//
		
		STATBTN_DIRS[STAT_PEOPLE]        =  102;
		STATBTN_DIRS[STAT_MONSTERS]      =  100;
		STATBTN_DIRS[STAT_TIME]          =  105;
		STATBTN_DIRS[STAT_MONEYMISSION]  =  104;
		STATBTN_DIRS[STAT_MONEYMISSION2] =  106;
		STATBTN_DIRS[STAT_POINTS]        =  103;
	
		STATBTN_DIRS2[STAT_PEOPLE]        =  200;
		STATBTN_DIRS2[STAT_MONSTERS]      =  201;
		STATBTN_DIRS2[STAT_TIME]          =  202;
		STATBTN_DIRS2[STAT_MONEYMISSION]  =  203;
		STATBTN_DIRS2[STAT_MONEYMISSION2] =  204;
		STATBTN_DIRS2[STAT_POINTS]        =  205;
	}
}


//------------------------------------------------------------------------------------------
RememberStat()
{
	InitStatIfNeeded();
	//
	InitStatData[ VSTAT_MPEOPLECNT ] = FindPeople;
	InitStatData[ VSTAT_MTIME ] = GetTime();
	//
	//теперь всегда подсчитываем после прохождения
	if( false )//LevelNumber != 4 && LevelNumber != 6 && LevelNumber != 10 && LevelNumber > 0 )
		InitStatData[ VSTAT_MMONSTERSCNT ] = GetUnitInMap();
	else
	{
		InitStatData[ VSTAT_MMONSTERSCNT ] = -1;	//флаг, что значение должно быть подсчитано после уровня
		Log("4th level");
	}
	//
	//InitStatData[ VSTAT_MMONEY	] = PlayerMoney;
	InitStatData[ VSTAT_POINTS	] = PlayerScores;
}


//------------------------------------------------------------------------------------------
LoadFullRankFromRegistry()
{
	InitStatIfNeeded();
	int i;
//ztodo123
//	for( i = 0; i < VSTAT_MCNT; i++ )
//		OldStatData[ i ] = GetReg( GetDefaultRegPath(), VSTAT_NAMES[ i ], 0 );
}


//------------------------------------------------------------------------------------------
ResetFullRankInRegistry()
{
	InitStatIfNeeded();
	Log("ResetFullRankInRegistry");
	int i;
//ztodo123
//	for( i = 0; i < VSTAT_MCNT; i++ )
//		SetReg( GetDefaultRegPath(), VSTAT_NAMES[ i ], 0 );
}


//------------------------------------------------------------------------------------------
SaveFullRankToRegistry()
{
	InitStatIfNeeded();
	int i;
//ztodo123
//	for( i = 0; i < VSTAT_MCNT; i++ )
//		SetReg( GetDefaultRegPath(), VSTAT_NAMES[ i ], itoa( OldStatData[ i ] ) );
}


//------------------------------------------------------------------------------------------
OutputStat( int iType, int *aiSrc )
{
	static string asDest[ STAT_CNT ];
	//
	asDest[ STAT_PEOPLE ] = itoa( aiSrc[ VSTAT_MPEOPLEFOUND ] ) + " / "
											+ itoa( aiSrc[ VSTAT_MPEOPLECNT ] );
	//
	asDest[ STAT_MONSTERS ] = itoa( aiSrc[ VSTAT_MMONSTERSKILLED ] )
										+ " / " + itoa( aiSrc[ VSTAT_MMONSTERSCNT ] );
	//
	int Secs, Mins, Hours, buf;
	Secs = aiSrc[ VSTAT_MTIME ] / 1000;
	Mins = Secs / 60;
	Hours = Mins / 60;
	asDest[ STAT_TIME ] = Printf( "%02d", Hours ) + ":" +
							Printf( "%02d", Mins % 60 ) + ":" +
							Printf( "%02d", Secs % 60 );
	//
	if( iType == 0 && aiSrc[ VSTAT_MMONEYMISSION2 ] != -1 )
		asDest[ STAT_MONEYMISSION2 ] = "+$" + itoa( aiSrc[ VSTAT_MMONEYMISSION2 ] );
	else
		asDest[ STAT_MONEYMISSION2 ] = "";
	if( aiSrc[ VSTAT_MMONEYMISSION2 ] == -1 )//clear flag
		aiSrc[ VSTAT_MMONEYMISSION2 ] = 0;
	//
	if( iStatType == STAT_TYPE_CAMPAIGN )
	{
		if( iType == 0 )
		{
			if( aiSrc[ VSTAT_MMONEYMISSION ] != -1 )
				asDest[ STAT_MONEYMISSION ] = "+$" + itoa( aiSrc[ VSTAT_MMONEYMISSION ] );
			else
			{
				asDest[ STAT_MONEYMISSION ] = "";
				aiSrc[ VSTAT_MMONEYMISSION ] = 0;
			}
		}
		else
		{
			int iCurM;
			iCurM = PlayerMoney + aiSrc[ VSTAT_MMONEYMISSION ] + aiSrc[ VSTAT_MMONEYMISSION2 ];
			asDest[ STAT_MONEYMISSION ] = "$" + itoa( iCurM );
		}
	}
	asDest[ STAT_POINTS ] = itoa( aiSrc[ VSTAT_POINTS ] );

	//
	int i;
	for( i = 0; i < STAT_CNT; i++ )
	{
		if( iType == 0 )
			StatData[ i ] = asDest[ i ];
		else
		if( iType == 1 )
			StatData2[ i ] = asDest[ i ];
		else
			StatDebugData[ i ] = asDest[ i ];
	}
}


//------------------------------------------------------------------------------------------
//получить кол-во монстров, стоящих на карте в данный момент
GetMonstCnt( int iType )
{
	int buf, i, size;
	buf = 0;
	size = sizeof( MonstersVid ) / sizeof(int);
	for( i = 0; i < size; i++ )
		buf += GetVidData( MonstersVid[ i ], iType );
	return buf;
}


//------------------------------------------------------------------------------------------
GetCurStatData()
{
	//search for people and quests number
	if( InitStatData[ VSTAT_MPEOPLECNT ] )
		CurStatData[ VSTAT_MPEOPLEFOUND ] = InitStatData[ VSTAT_MPEOPLECNT ] - FindPeople;
	CurStatData[ VSTAT_MPEOPLECNT ] = InitStatData[ VSTAT_MPEOPLECNT ];
	//
	//
	CurStatData[ VSTAT_MMONSTERSKILLED ] = GetMonstCnt(VID_KILLEDUNIT);	//zmNEW 2005Jul14
	//если флаг, что значение должно быть подсчитано после уровня
	if( InitStatData[ VSTAT_MMONSTERSCNT ] == -1 || iStatType == STAT_TYPE_CAMPAIGN )
	{
		CurStatData[ VSTAT_MMONSTERSCNT ] = CurStatData[ VSTAT_MMONSTERSKILLED ] + GetUnitInMap();//zmNEW 2005Jul29
		Log("4th level: " + itoa(CurStatData[ VSTAT_MMONSTERSKILLED ]) + " - "+ itoa(GetUnitInMap()));
	}
	else
		CurStatData[ VSTAT_MMONSTERSCNT ] = InitStatData[ VSTAT_MMONSTERSCNT ];

	//zmNew 2005Aug02: на всякий случай, вдруг баг с подсчетом общего количества монстров остался
	if( CurStatData[ VSTAT_MMONSTERSCNT ] < CurStatData[ VSTAT_MMONSTERSKILLED ] )
		CurStatData[ VSTAT_MMONSTERSCNT ] = CurStatData[ VSTAT_MMONSTERSKILLED ];

	//
	CurStatData[ VSTAT_MTIME ] = GetTime() - InitStatData[ VSTAT_MTIME ];
	//
	if( iStatType == STAT_TYPE_CAMPAIGN )
	{
		if( LevelNumber >= 10 )
			CurStatData[ VSTAT_MMONEYMISSION ] = -1;
		else
			CurStatData[ VSTAT_MMONEYMISSION ] = LevelMoney[LevelNumber];
	}

	if( !LevelMoneyAdd[LevelNumber] || LevelNumber >= 10 )
		CurStatData[ VSTAT_MMONEYMISSION2 ] = -1; //flag to hide text about 2nd mission
	else
	if( FindPeople == 0 && iStatType == STAT_TYPE_CAMPAIGN )
		CurStatData[ VSTAT_MMONEYMISSION2 ] = LevelMoneyAdd[LevelNumber];
	else
		CurStatData[ VSTAT_MMONEYMISSION2 ] = 0;
	//
	CurStatData[ VSTAT_POINTS ] = PlayerScores - InitStatData[ VSTAT_POINTS ];
}


//------------------------------------------------------------------------------------------
//find stat data
GetStatData( int nArmy )
{
	int i;
	for( i = 0; i < STAT_CNT; i++ )
	{
		StatData[ i ] = "----------";
		StatData2[ i ] = "----------";
	}

	GetCurStatData();

	//
	//if( MustAddRank() )
	//	SendCommandA( nArmy, EXT_ADDSKILL, RPGHERO_RANK, 1 );
	//zmTODO?
	//SendCommandA( nArmy, EXT_ADDSKILL, RPGHERO_MONEY, InitStatData[ VSTAT_MMONEYMISSION ] );

	OutputStat( 0, CurStatData );

	//output full statistics
	LoadFullRankFromRegistry();
	for( i = 0; i < VSTAT_MCNT; i++ )
		OldStatData[ i ] = OldStatData[ i ] + CurStatData[ i ];

	//
	OldStatData[ VSTAT_MMONEYMISSION ] = CurStatData[ VSTAT_MMONEYMISSION ];
	OldStatData[ VSTAT_MMONEYMISSION2 ] = CurStatData[ VSTAT_MMONEYMISSION2 ];
	OutputStat( 1, OldStatData );
	//
	if( iStatType == STAT_TYPE_CAMPAIGN )
		SaveFullRankToRegistry();
}


//------------------------------------------------------------------------------------------
StatScreenTact()
{
	if( !bStatShown )
		return;
	//
	if( GetEffectState(EFF_FADE) > 45 && StartEffectTime2 )
	{
		//load statistics
		StartEffectTime2 = 0;
		//GetUserName();//zmTODO?
		//
		MenuRelease();
		//HideAllSprites( 1 );//zmTODO?
		Log("zm: loading stat");

		if( iStatType == STAT_TYPE_CAMPAIGN )
			MenuLoad("maps\\statistics.men");
		else
			MenuLoad("maps\\statistics_survive.men");

		GetStatData(0);
		
		//find text sprites
		string buf;
		int i, sprite, sprite2, invis;
		for( i = 0; i < STAT_CNT; i++ )
		{
			sprite = MenuFind( 6, STATBTN_DIRS[ i ] );
			if( sprite )
			{
				buf = StatData[ i ];
				Action( sprite, ACT_SET_TEXT, &buf );
			}
			//
			sprite = MenuFind( 6, STATBTN_DIRS2[ i ] );
			if( sprite )
			{
				buf = StatData2[ i ];
				Action( sprite, ACT_SET_TEXT, &buf );
			}
		}
		//find mission-name label
		sprite = MenuFind( 6, 99 );
		buf = itoa(LevelNumber);
		if(sprite)
			Action( sprite, ACT_SET_TEXT, &buf );

		//find 2-nd mission labels and hide or show if needed
		sprite = MenuFind( 4, 109 );
		sprite2 = MenuFind( 6, STATBTN_DIRS[ STAT_MONEYMISSION2 ] );
		if( StatData[ STAT_MONEYMISSION2 ] == "" )
			invis = 1;
		else
			invis = 0;
		if(sprite)
			Action( sprite, ACT_SET_INVISIBLE, invis );
		if(sprite2)
			Action( sprite2, ACT_SET_INVISIBLE, invis );
		//find 1-st mission labels and hide or show if needed
		sprite = MenuFind( 4, 149 );
		sprite2 = MenuFind( 6, STATBTN_DIRS[ STAT_MONEYMISSION ] );
		if( StatData[ STAT_MONEYMISSION ] == "" )
			invis = 1;
		else
			invis = 0;
		if(sprite)
			Action( sprite, ACT_SET_INVISIBLE, invis );
		if(sprite2)
			Action( sprite2, ACT_SET_INVISIBLE, invis );
	}
	else
	{
		//сказали, что в тезее не надо
		//control buttons
		/*int btn;
		if( btn = MenuLClick() && MenuNVidUnderCursor() == 712 )
		{
			if( MenuNDirUnderCursor() == 0 )
				LoadNextMap("maps\\mainmenu.map");
			else
			if( MenuNDirUnderCursor() == 1 )
				LoadNextMap(GetMapName());//RestartLevel(); неправильно LevelNumber в реестре
			else
			if( MenuNDirUnderCursor() == 2 )
				HideStatScreen();
		}*/
	}

}


//------------------------------------------------------------------------------------------
//output debug statistics
static int	StatDebugSprites[ STAT_CNT ];
static bool bDebugStatShown = false;

//------------------------------------------------------------------------------------------
DebugStatOutput()
{
	return;
	//
	if( !OneTimeInSecond() )
		return;

	GetCurStatData();
	OutputStat( 2, CurStatData );
	//
	static bool bCreated = false;
	int i;
	if( !bCreated )
	{
		bCreated = true;
		for( i = 0; i < STAT_CNT; i++ )
			StatDebugSprites[ i ] = 0;
	}
	//
	for( i = 0; i < STAT_CNT; i++ )
	{
		if( StatDebugSprites[ i ] )
			Destroy( StatDebugSprites[ i ] );
		StatDebugSprites[ i ] = 0;
	}
	//
	if( bDebugStatShown )
	{
		for( i = 0; i < STAT_CNT; i++ )
		{
			StatDebugSprites[ i ] = CreateText( 3, ScreenX() - 75,
									200 + i * 30 + 5000, 5000, StatDebugData[ i ], BEH_CENTER );
		}
	}
}


//------------------------------------------------------------------------------------------
ToggleDebugStat()
{
	bDebugStatShown ^= 1;
}

