class UISoldierQuickIconPanel extends UIList config(SoldierSelectIcons) implements(X2VisualizationMgrObserverInterface) config(SoldiersQuickInfo);

var config int MAX_SIZE;
var config int OFFSET_X;
var config int OFFSET_Y;
var config int ICON_SIZE;
var config int ICON_PADDING;
var config bool ORIENT_HORIZONTAL;
var config bool ANCHOR_TOP;
var config bool ANCHOR_RIGHT;
var config bool TEMP_ICONS;
var config int FONT_SIZE;

var config int ConfigVersion;

var array<SoldierQuickIconObject> SoldierIcons;

var SoldierQuickIconObject pendingIconToClick;

simulated function UISoldierQuickIconPanel InitSoldierQuickInfo()
{
	local Object ThisObj;

	if (!ANCHOR_TOP)
	{
		OFFSET_Y = -default.OFFSET_Y;
		if (ORIENT_HORIZONTAL)
			OFFSET_Y -= ICON_SIZE * 1.15 + 38;
	}

	if (ANCHOR_RIGHT)
	{
		OFFSET_X = -default.OFFSET_X;
		if (!ORIENT_HORIZONTAL)
			OFFSET_X -= ICON_SIZE * 1.16;
	}

	InitList(, OFFSET_X, OFFSET_Y, ICON_SIZE * 1.16, ICON_SIZE * 1.15 + 38, ORIENT_HORIZONTAL);
	if (ANCHOR_TOP && ANCHOR_RIGHT)
		AnchorTopRight();
	else if (!ANCHOR_TOP && ANCHOR_RIGHT)
		AnchorBottomRight();
	else if (ANCHOR_TOP && !ANCHOR_RIGHT)
		AnchorTopLeft();
	else if (!ANCHOR_TOP && !ANCHOR_RIGHT)
		AnchorBottomLeft();
	ItemPadding = ICON_PADDING;

	PopulateData();	

	`XCOMVISUALIZATIONMGR.RegisterObserver(self);
	ThisObj = self;
	`XEVENTMGR.RegisterForEvent(ThisObj, 'UnitDied', OnRefreshRequested, ELD_OnVisualizationBlockCompleted);
	`XEVENTMGR.RegisterForEvent(ThisObj, 'UnitRemovedFromPlay', OnRefreshRequested, ELD_OnVisualizationBlockCompleted);

	return self;
}

simulated function PopulateData(optional XComGameState AssociatedGameState)
{
	local array<XComGameState_Unit> Units;
	local XComGameState_Unit Unit;
	local SoldierQuickIconObject SoldierIcon;

	local array<SoldierQuickIconObject> IconsToDelete;
	local array<int> soldierIDs, iconSoldierIDs, squadIDs;
	local StateObjectReference UnitRef;

	local int PosAdjust;

	foreach SoldierIcons(SoldierIcon)
	{
		iconSoldierIDs.AddItem(SoldierIcon.UnitID);
	}

	XComTacticalController(PC).m_XGPlayer.GetPlayableUnits(Units);

	foreach `XCOMHQ.Squad(UnitRef)
	{
		squadIDs.AddItem(UnitRef.ObjectID);
	}

	foreach Units(Unit)
	{
		if (iconSoldierIDs.Find(Unit.GetReference().ObjectID) == INDEX_NONE)
		{
			SoldierIcon = SoldierQuickIconObject(CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit);
			SoldierIcon.ProcessMouseEvents(OnItemMouseEvent);

			if (squadIDs.Find(Unit.GetReference().ObjectID) != INDEX_NONE)
				SoldierIcon.IsOriginalSquad = true;

			SoldierIcons.AddItem(SoldierIcon);

		}
		soldierIDs.AddItem(Unit.GetReference().ObjectID);
	}


	foreach SoldierIcons(SoldierIcon)
	{
		if (soldierIDs.Find(SoldierIcon.UnitID) == INDEX_NONE)
		{
			if (!SoldierIcon.IsOriginalSquad)
				IconsToDelete.AddItem(SoldierIcon);
		}
	}

	foreach IconsToDelete(SoldierIcon)
	{
		SoldierIcon.Cleanup();
		SoldierIcons.RemoveItem(SoldierIcon);
	}

	foreach Units(Unit)
	{
		foreach SoldierIcons(SoldierIcon)
		{
			if (SoldierIcon.UnitID == Unit.GetReference().ObjectID && !SoldierIcon.IsOriginalSquad)
			{
				MoveItemToBottom(SoldierIcon);
			}
		}
	}

	RealizeItems();

	if (ORIENT_HORIZONTAL)
	{
		SetWidth(MAX_SIZE);
		PosAdjust = ShrinkToFit();
		if (!ANCHOR_RIGHT)
			PosAdjust = 0;
		SetPosition(OFFSET_X - PosAdjust, OFFSET_Y);
	}
	else
	{
		SetHeight(MAX_SIZE);
		PosAdjust = ShrinkToFit();
		if (ANCHOR_TOP)
			PosAdjust = 0;
		SetPosition(OFFSET_X, OFFSET_Y - PosAdjust);
	}


	UpdateData(AssociatedGameState);
}

simulated function ResizeAllIcons(int NewSize)
{
	local SoldierQuickIconObject SoldierIcon;
	local int PosAdjust;

	ICON_SIZE = NewSize;

	foreach SoldierIcons(SoldierIcon)
	{
		SoldierIcon.ResizeAll(NewSize);
	}
	RealizeItems();

	if (!ANCHOR_TOP)
	{
		OFFSET_Y = -default.OFFSET_Y;
		if (ORIENT_HORIZONTAL)
			OFFSET_Y -= ICON_SIZE * 1.15 + 38;
	}
	else
	{
		OFFSET_Y = default.OFFSET_Y;
	}

	if (ANCHOR_RIGHT)
	{
		OFFSET_X = -default.OFFSET_X;
		if (!ORIENT_HORIZONTAL)
			OFFSET_X -= ICON_SIZE * 1.16;
	}
	else
	{
		OFFSET_X = default.OFFSET_X;
	}

	if (ORIENT_HORIZONTAL)
	{
		SetWidth(MAX_SIZE);
		SetHeight(NewSize * 1.15 + 38);
		PosAdjust = ShrinkToFit();
		if (!ANCHOR_RIGHT)
			PosAdjust = 0;
		SetPosition(OFFSET_X - PosAdjust, OFFSET_Y);
	}
	else
	{
		SetHeight(MAX_SIZE);
		SetWidth(NewSize * 1.16);
		PosAdjust = ShrinkToFit();
		if (ANCHOR_TOP)
			PosAdjust = 0;
		SetPosition(OFFSET_X, OFFSET_Y - PosAdjust);
	}
}

function EventListenerReturn OnRefreshRequested(Object EventData, Object EventSource, XComGameState GameState, Name EventID, Object CallbackObject)
{
	PopulateData();
	return ELR_NoInterrupt;
}

event OnVisualizationIdle()
{
	ProcessQueuedData();
}

event OnVisualizationBlockComplete(XComGameState AssociatedGameState)
{
	local array<XComGameState_Unit> Units;
	XComTacticalController(PC).m_XGPlayer.GetPlayableUnits(Units);

	if (Units.Length != SoldierIcons.Length)
	{
		//`log("New unit has joined!",,'QuickInfo');
		PopulateData(AssociatedGameState);
	}
	else
	{
		//`log("GameStateUpdated",,'QuickInfo');
		UpdateData(AssociatedGameState);
	}
}
		  
event OnActiveUnitChanged(XComGameState_Unit NewActiveUnit)
{
	//`log("Active unit changed",,'QuickInfo');
	UpdateData();
}

simulated function ProcessQueuedData()
{
	if (pendingIconToClick != none)
	{
		pendingIconToClick = none;
		pendingIconToClick.OnMouseUp();
	}
}

simulated function UpdateData(optional XComGameState relevantGameState)
{
	local SoldierQuickIconObject SIcon;

	foreach SoldierIcons(SIcon)
	{
		SIcon.UpdateData(relevantGameState);
	}
}

simulated function OnItemMouseEvent(UIPanel SoldierIcon, int cmd)
{
	local int i;
	local SoldierQuickIconObject icon;

	for (i = 0; i < SoldierIcons.Length; i++)
	{
		icon = SoldierIcons[i];
		if (SoldierIcon == icon)  
		{
			switch (cmd)
			{
				case class'UIUtilities_Input'.const.FXS_L_MOUSE_UP:
					if (`XCOMVISUALIZATIONMGR.VisualizerBusy())
					{
						pendingIconToClick = icon;
						if (!`XCOMVISUALIZATIONMGR.VisualizerBlockingAbilityActivation(true))
						{
							XComTacticalController(PC).bManuallySwitchedUnitsWhileVisualizerBusy = true;
							XComTacticalController(PC).Visualizer_SelectUnit(icon.UnitState);
						}
					}
					else
					{
						pendingIconToClick = none;
						icon.OnMouseUp();
					}
					break;
				case class'UIUtilities_Input'.const.FXS_L_MOUSE_IN:
					icon.SetFocus(true);
					break;
				case class'UIUtilities_Input'.const.FXS_L_MOUSE_OUT:
				case class'UIUtilities_Input'.const.FXS_L_MOUSE_DRAG_OUT:
					icon.SetFocus(false);
					break;

			}
		}
	}
}

defaultproperties
{
	bAnimateOnInit=false;
}