class CyberwareMainGameController extends inkGameController
{
	private editable var m_MainViewRoot : inkWidgetRef;
	private editable var m_CyberwareColumnLeft : inkCompoundRef;
	private editable var m_CyberwareColumnRight : inkCompoundRef;
	private editable var m_personalStatsList : inkCompoundRef;
	private editable var m_attributesList : inkCompoundRef;
	private editable var m_resistancesList : inkCompoundRef;
	private editable var m_TooltipsManagerRef : inkWidgetRef;
	private var m_TooltipsManager : weak< gameuiTooltipsManager >;
	private var m_InventoryManager : InventoryDataManagerV2;
	private var m_player : weak< PlayerPuppet >;
	private var m_resistanceView : CName;
	private var m_statView : CName;
	private var m_toolTipOffset : inkMargin;
	private var m_rawStatsData : array< StatViewData >;

	protected event OnInitialize()
	{
		m_TooltipsManager = ( ( gameuiTooltipsManager )( inkWidgetRef.GetControllerByType( m_TooltipsManagerRef, 'gameuiTooltipsManager' ) ) );
		m_player = ( ( PlayerPuppet )( GetPlayerControlledObject() ) );
		m_InventoryManager = new InventoryDataManagerV2;
		m_InventoryManager.Initialize( m_player );
		m_InventoryManager.GetPlayerStats( m_rawStatsData );
		inkCompoundRef.RemoveAllChildren( m_CyberwareColumnLeft );
		inkCompoundRef.RemoveAllChildren( m_CyberwareColumnRight );
		m_toolTipOffset.left = 60.0;
		m_toolTipOffset.top = 5.0;
		inkCompoundRef.RemoveAllChildren( m_personalStatsList );
		inkCompoundRef.RemoveAllChildren( m_attributesList );
		inkCompoundRef.RemoveAllChildren( m_resistancesList );
		m_resistanceView = 'resistanceView';
		m_statView = 'statView';
		SetupBB();
		PrepareTooltips();
		PrepareCyberwareSlots();
		PopulateStats();
		OnIntro();
	}

	protected event OnUninitialize()
	{
		m_InventoryManager.UnInitialize();
		RemoveBB();
	}

	private function SetupBB() {}

	private function RemoveBB() {}

	private function PrepareCyberwareSlots()
	{
		AddCyberwareSlot( gamedataEquipmentArea.SystemReplacementCW, m_CyberwareColumnRight );
		AddCyberwareSlot( gamedataEquipmentArea.EyesCW, m_CyberwareColumnRight );
		AddCyberwareSlot( gamedataEquipmentArea.HandsCW, m_CyberwareColumnRight );
		AddCyberwareSlot( gamedataEquipmentArea.ArmsCW, m_CyberwareColumnRight );
		AddCyberwareSlot( gamedataEquipmentArea.LegsCW, m_CyberwareColumnRight );
		AddCyberwareSlot( gamedataEquipmentArea.MusculoskeletalSystemCW, m_CyberwareColumnLeft );
		AddCyberwareSlot( gamedataEquipmentArea.NervousSystemCW, m_CyberwareColumnLeft );
		AddCyberwareSlot( gamedataEquipmentArea.CardiovascularSystemCW, m_CyberwareColumnLeft );
		AddCyberwareSlot( gamedataEquipmentArea.ImmuneSystemCW, m_CyberwareColumnLeft );
		AddCyberwareSlot( gamedataEquipmentArea.IntegumentarySystemCW, m_CyberwareColumnLeft );
	}

	public function PopulateStats()
	{
		AddStat( gamedataStatType.Health, m_personalStatsList );
		AddStat( gamedataStatType.Evasion, m_personalStatsList );
		AddStat( gamedataStatType.Accuracy, m_personalStatsList );
		AddStat( gamedataStatType.CarryCapacity, m_personalStatsList );
		AddStat( gamedataStatType.Reflexes, m_attributesList );
		AddStat( gamedataStatType.Intelligence, m_attributesList );
		AddStat( gamedataStatType.TechnicalAbility, m_attributesList );
		AddStat( gamedataStatType.Strength, m_attributesList );
		AddStat( gamedataStatType.Cool, m_attributesList );
		AddStat( gamedataStatType.PhysicalResistance, m_resistancesList, m_resistanceView );
		AddStat( gamedataStatType.ThermalResistance, m_resistancesList, m_resistanceView );
		AddStat( gamedataStatType.ElectricResistance, m_resistancesList, m_resistanceView );
		AddStat( gamedataStatType.ChemicalResistance, m_resistancesList, m_resistanceView );
	}

	private function AddStat( statType : gamedataStatType, list : inkCompoundRef, optional viewElement : CName )
	{
		var statData : StatViewData;
		var statView : weak< StatsViewController >;
		if( viewElement == '' )
		{
			viewElement = m_statView;
		}
		statData = RequestStat( statType );
		statView = ( ( StatsViewController )( SpawnFromLocal( inkCompoundRef.Get( list ), viewElement ).GetControllerByType( 'StatsViewController' ) ) );
		statView.Setup( statData );
	}

	private function AddCyberwareSlot( equipArea : gamedataEquipmentArea, parentRef : inkCompoundRef )
	{
		var cybSlot : weak< CyberwareSlot >;
		var numSlots : Int32;
		cybSlot = ( ( CyberwareSlot )( SpawnFromLocal( inkCompoundRef.Get( parentRef ), 'cyberware_slot' ).GetControllerByType( 'CyberwareSlot' ) ) );
		if( cybSlot )
		{
			numSlots = m_InventoryManager.GetNumberOfSlots( equipArea );
			cybSlot.Setup( equipArea, numSlots );
			cybSlot.RegisterToCallback( 'OnHoverOver', this, 'OnCyberwareSlotHoverOver' );
			cybSlot.RegisterToCallback( 'OnHoverOut', this, 'OnCyberwareSlotHoverOut' );
		}
	}

	protected event OnCyberwareSlotHoverOver( evt : inkPointerEvent )
	{
		var cyberwareSlot : weak< CyberwareSlot >;
		cyberwareSlot = GetCyberwareSlotControllerFromTarget( evt );
		OnCyberwareRequestTooltip( cyberwareSlot );
	}

	protected event OnCyberwareSlotHoverOut( evt : inkPointerEvent )
	{
		HideTooltips();
	}

	private function PrepareTooltips()
	{
		m_TooltipsManager = ( ( gameuiTooltipsManager )( inkWidgetRef.GetControllerByType( m_TooltipsManagerRef, 'gameuiTooltipsManager' ) ) );
		m_TooltipsManager.Setup( ETooltipsStyle.Menus );
	}

	private function OnCyberwareRequestTooltip( slot : weak< CyberwareSlot > )
	{
		var tooltipsData : CyberwareTooltipData;
		var i : Int32;
		tooltipsData = new CyberwareTooltipData;
		if( slot )
		{
			tooltipsData.label = ( ( String )( slot.GetEquipmentArea() ) );
			for( i = 0; i < slot.GetNumSlots(); i += 1 )
			{
				tooltipsData.AddCyberwareSlotItemData( m_InventoryManager.GetItemDataEquippedInArea( slot.GetEquipmentArea(), i ) );
			}
			m_TooltipsManager.ShowTooltip( 0, tooltipsData, m_toolTipOffset );
		}
	}

	private function HideTooltips()
	{
		m_TooltipsManager.HideTooltips();
	}

	private function OnIntro() {}

	private function GetCyberwareSlotControllerFromTarget( evt : inkPointerEvent ) : CyberwareSlot
	{
		var widget : inkWidget;
		var controller : weak< CyberwareSlot >;
		widget = evt.GetCurrentTarget();
		controller = ( ( CyberwareSlot )( widget.GetController() ) );
		return controller;
	}

	private function RequestStat( stat : gamedataStatType ) : StatViewData
	{
		var data : StatViewData;
		var i : Int32;
		for( i = 0; i < m_rawStatsData.Size(); i += 1 )
		{
			if( m_rawStatsData[ i ].type == stat )
			{
				return m_rawStatsData[ i ];
			}
		}
		return data;
	}

}

