class netChargesWidgetGameController extends inkHUDGameController
{
	private var m_bbPlayerStats : weak< IBlackboard >;
	private var m_bbPlayerEventId1 : CallbackHandle;
	private var m_bbPlayerEventId2 : CallbackHandle;
	private var m_bbPlayerEventId3 : CallbackHandle;
	private var networkName : weak< inkText >;
	private var networkStatus : weak< inkText >;
	private var chargesList : array< weak< inkCompoundWidget > >;
	private var chargesCurrent : Int32;
	private var chargesMax : Int32;
	private var networkNameText : String;
	private var networkStatusText : String;
	private var rootWidget : weak< inkWidget >;
	private var chargeList : weak< inkHorizontalPanel >;

	protected event OnInitialize()
	{
		rootWidget = ( ( inkWidget )( GetRootWidget() ) );
		rootWidget.SetVisible( false );
		chargeList = ( ( inkHorizontalPanel )( GetWidget( 'net/networkStatus/chargesList' ) ) );
		networkName = ( ( inkText )( GetWidget( 'net/networkStatus/networkName' ) ) );
		networkStatus = ( ( inkText )( GetWidget( 'net/networkStatus/statusText' ) ) );
		networkNameText = "CYBERDECK ERROR: NO NETWORK CONNECTION";
		networkStatusText = "Malware hooks installed :";
		chargesMax = -1;
		chargesCurrent = 0;
		RefreshCharges();
		m_bbPlayerStats = GetBlackboardSystem().Get( GetAllBlackboardDefs().UI_PlayerBioMonitor );
		m_bbPlayerEventId1 = m_bbPlayerStats.RegisterListenerInt( GetAllBlackboardDefs().UI_PlayerBioMonitor.CurrentNetrunnerCharges, this, 'OnCurrentChargesChanged' );
		m_bbPlayerEventId2 = m_bbPlayerStats.RegisterListenerInt( GetAllBlackboardDefs().UI_PlayerBioMonitor.NetworkChargesCapacity, this, 'OnMaxChargesChanged' );
		m_bbPlayerEventId3 = m_bbPlayerStats.RegisterListenerName( GetAllBlackboardDefs().UI_PlayerBioMonitor.NetworkName, this, 'OnNameChanged' );
	}

	protected event OnUninitialize()
	{
		if( m_bbPlayerStats )
		{
			m_bbPlayerStats.UnregisterListenerInt( GetAllBlackboardDefs().UI_PlayerBioMonitor.CurrentNetrunnerCharges, m_bbPlayerEventId1 );
			m_bbPlayerStats.UnregisterListenerInt( GetAllBlackboardDefs().UI_PlayerBioMonitor.NetworkChargesCapacity, m_bbPlayerEventId2 );
			m_bbPlayerStats.UnregisterListenerName( GetAllBlackboardDefs().UI_PlayerBioMonitor.NetworkName, m_bbPlayerEventId3 );
		}
	}

	private function RefreshCharges()
	{
		var i : Int32;
		networkStatusText = "Installed malware hooks status:";
		if( chargesCurrent == 0 )
		{
			networkStatusText = "No malware hooks installed!";
		}
		for( i = 0; i < chargesList.Size(); i += 1 )
		{
			chargeList.RemoveChild( chargesList[ i ] );
		}
		chargesList.Clear();
		for( i = 0; i < chargesMax; i += 1 )
		{
			chargesList.PushBack( ( ( inkCompoundWidget )( SpawnFromLocal( chargeList, 'charge' ) ) ) );
			SetChargeNumber( chargesList[ i ], -1 );
			SetChargeState( chargesList[ i ], false );
			chargesList[ i ].SetVisible( true );
		}
		for( i = 0; i < chargesCurrent; i += 1 )
		{
			SetChargeState( chargesList[ i ], true );
			SetChargeNumber( chargesList[ i ], i + 1 );
			chargesList[ i ].SetVisible( true );
		}
		networkName.SetText( networkNameText );
		networkStatus.SetText( networkStatusText );
		if( chargesCurrent < 1 )
		{
			rootWidget.SetVisible( false );
		}
		else
		{
			rootWidget.SetVisible( true );
		}
	}

	private function SetChargeState( chargeWidget : weak< inkCompoundWidget >, state : Bool )
	{
		var tempRef : weak< inkRectangle >;
		tempRef = ( ( inkRectangle )( chargeWidget.GetWidget( 'fill' ) ) );
		tempRef.SetVisible( state );
	}

	private function SetChargeNumber( chargeWidget : weak< inkCompoundWidget >, number : Int32 )
	{
		var tempRef : weak< inkText >;
		tempRef = ( ( inkText )( chargeWidget.GetWidget( 'number' ) ) );
		if( number != -1 )
		{
			tempRef.SetText( IntToString( number ) );
		}
		else
		{
			tempRef.SetText( "" );
		}
	}

	protected event OnMaxChargesChanged( value : Int32 )
	{
		chargesMax = value;
		RefreshCharges();
	}

	protected event OnCurrentChargesChanged( value : Int32 )
	{
		chargesCurrent = value;
		RefreshCharges();
	}

	protected event OnNameChanged( value : CName )
	{
		networkNameText = NameToString( value );
		RefreshCharges();
	}

}

