//
// Tools_ProgressIndicator.h
//
// Created by Richard Rice - 13/02/2004
// 
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//

#ifdef ASURA_USE_PRAGMA_ONCE
    #pragma once
#endif

#ifndef _TOOLS_PROGRESSINDICATOR_H_
#define _TOOLS_PROGRESSINDICATOR_H_

//Class for displaying progress messages during long frames that would otherwise
//have no other feedback.

class Tools_ProgressIndicator
{
public :
	//If bPreserveScreen then the screen will be captured and the text rendered over that , instead of blacking it out
	Tools_ProgressIndicator(const char* pszText = 0, bool bPreserveScreen = false);
	~Tools_ProgressIndicator();

	//Set the text to show with this progress indicator , and activates it
	void SetText(const char* pszText,...);
    // If a maximum value is specified then by default progress will be shown
    // as a percentage. This method can be used to change that to a 'n / N'
    // display instead.
    void SetShowAsPercentage( bool bPercentage ) { m_bShowAsPercentage = bPercentage; }

	// Show time elapsed/remaining
    void SetShowTimers( bool bShowTimers ) { m_bShowTimeRemaining = bShowTimers; }

	//Update the progress values , and renders if enough time has passed (or if bForceRender)
	//If uMax is non-zero then a percentage pregress wil be shown
	bool Update(u_int uProgress = 0,u_int uMax = 0,bool bForceRender = false);
	//Deactivates this progress indicator
	void Finish();

	//Adds a single message above the progress indicators. this will
	//gradually get scrolled off as other messages are added
	static void AddMessage(const char* pszText,...);
	//Force the progress text to be rendered regardless of how much time
	//has passed since it was last done.
	static void ForceRender(){Render(true);}

private :
	void CalculateTimeRemaining();
	LONGLONG ProcessSpeedHistory( u_int uProgressLeft ); // Returns the approximate time needed to complete uProgressLeft amount of progress

	static void UpdateTimers();
	static void Render(bool bForceRender);	

	char m_szText[256];
	u_int m_uProgress;
	u_int m_uMax;
    bool m_bShowAsPercentage;
	bool m_bActive;
	bool m_bPreserveScreen;
	bool m_bHasCapturedScreen;
	bool m_bShowTimeRemaining;

	enum { NUM_STORED_HISTORY = 200 };
	struct ProgressSpeedHistory
	{
		u_int uProgress;	// Amount of progress completed during the iLength interval
		LONGLONG iLength;	// The length of the interval during which uProgress amount of progress was completed (in ticks)

		void Reset()
		{
			uProgress = 0;
			iLength = 0;
		}
	};

	ProgressSpeedHistory m_axProgressSpeedHistory[NUM_STORED_HISTORY];

	u_int m_uLastProgress;
	LONGLONG m_iLastProgressTimeStamp;

	LONGLONG m_iTimeElapsed;
	LONGLONG m_iTimeRemaining;
};



#endif //_TOOLS_PROGRESSINDICATOR_H_
