class SoldierQuickIconObject extends UIPanel;

var XComGameState_Unit UnitState;
var int UnitID;

var UIIcon SoldierHeadshot;
var UIPanel HPContainer;
var UIProgressBar HPBar;
var UIProgressBar WillBar;
var array<UIIcon> ArmorPips;
var UIText ActionPoints;
var UIBGBox NameBG;
var UIBGBox APBG;
var UIScrollingText UnitName;
var UIIcon ClassIcon;
var UIIcon RankIcon;
var UIIcon DebuffIcon;
var UIIcon BuffIcon;

var UIBGBox SurroundBox;
var UIBGBox ImageMask;

var UIIcon StatusIconLarge;
var UIText StatusTextLarge;

var bool IsSelected;

var bool IsOriginalSquad;

var XComPhotographer_Strategy Photo;

var int IconSize;
var int FontSize;

simulated function SoldierQuickIconObject InitQuickIcon(XComGameState_Unit Soldier, optional int Size = 0, optional int NewFontSize = -1)
{
	local X2SoldierClassTemplate soldierClass;
	local string NameCheck;

	InitPanel();

	if (Size > 20)
		IconSize = Size;
	else
		IconSize = class'UISoldierQuickIconPanel'.default.ICON_SIZE;

	if (NewFontSize <= 0)
		FontSize = class'UISoldierQuickIconPanel'.default.FONT_SIZE;
	else
		FontSize = NewFontSize;
	SetSize(IconSize * 1.16, (IconSize * 1.15) + 8);

	UnitState = Soldier;
	UnitID = UnitState.GetReference().ObjectID;

	//`log("Unit Icon generated:"@ UnitID@"at size"@IconSize,,'QuickInfo');

	SoldierHeadshot = Spawn(class'UIIcon', self).InitIcon(,"img:///" $ X2VisualizerInterface(UnitState.GetVisualizer()).GetMyHUDIcon(), false);
	SoldierHeadshot.SetHeight(IconSize - 5);
	SoldierHeadshot.SetWidth(IconSize - 5);
	SoldierHeadshot.SetY(IconSize * 0.15);
	SoldierHeadshot.bDisableSelectionBrackets = true;

	MakeHeadshot();

	ImageMask = Spawn(class'UIBGBox', self).InitBG();
	ImageMask.SetSize(IconSize, IconSize);
	ImageMask.SetBGColor("0x000000");
	ImageMask.SetY(IconSize * 0.15);
	ImageMask.Hide();

	StatusIconLarge = Spawn(class'UIIcon', self).InitIcon(,, false, false);
	StatusIconLarge.SetSize(IconSize * 0.64, IconSize * 0.64);
	StatusIconLarge.SetX(IconSize * 0.18);
	StatusIconLarge.SetY(IconSize * 0.33);
	StatusIconLarge.bDisableSelectionBrackets = true;

	StatusTextLarge = Spawn(class'UIText', self).InitText(, "");
	StatusTextLarge.SetX(IconSize * 0.64);
	StatusTextLarge.SetY(IconSize * 0.4);
	StatusTextLarge.SetColor("0xff0000");
	StatusTextLarge.SetWidth(IconSize * 0.32);
	StatusTextLarge.SetHeight(IconSize * 0.50);

	MakeHeadshot();

	HPContainer = Spawn(class'UIPanel', self).InitPanel();
	HPContainer.SetSize(IconSize, 10);
	HPBar = HPContainer.Spawn(class'UIProgressBar', HPContainer).InitProgressbar();
	HPBar.SetX(2);
	HPBar.SetY(1);
	HPBar.SetSize(IconSize * 0.96, 4);
	HPBar.SetBGColor("0x990000");

	WillBar = HPContainer.Spawn(class'UIProgressBar', HPContainer).InitProgressbar();
	WillBar.SetX(2);
	WillBar.SetY(5);
	WillBar.SetSize(IconSize * 0.96, 4);
	WillBar.SetColor("0x009900");
	WillBar.SetBGColor("0xaaaaaa");

	HPContainer.SetY(IconSize * 1.15);

	SurroundBox = Spawn(class'UIBGBox', self).InitBG();
	SurroundBox.SetSize(IconSize, IconSize + 12);
	SurroundBox.SetY(IconSize * 0.15);
	SurroundBox.SetOutline(true, "0xffffff");
	SurroundBox.Hide();

	ClassIcon = Spawn(class'UIIcon', self).InitIcon(,, false, false);
	ClassIcon.SetSize(IconSize * 0.4, IconSize * 0.4);
	ClassIcon.SetX(IconSize * 0.7 - 5);
	ClassIcon.SetY(IconSize * 0.85 - 5);
	ClassIcon.bDisableSelectionBrackets = true;
	soldierClass = UnitState.GetSoldierClassTemplate();
	if (soldierClass != none)
	{
		ClassIcon.LoadIcon(soldierClass.IconImage);
	}

	RankIcon = Spawn(class'UIIcon', self).InitIcon(,, false, false);
	RankIcon.SetSize(IconSize * 0.33, IconSize * 0.33);
	RankIcon.SetX(IconSize * 0.82 - 5);
	RankIcon.bDisableSelectionBrackets = true;
	if (soldierClass != none)
	{
		RankIcon.LoadIcon(class'UIUtilities_Image'.static.GetRankIcon(UnitState.GetRank(), UnitState.GetSoldierClassTemplateName()));
	}

	APBG = Spawn(class'UIBGBox', self).InitBG();
	APBG.SetSize(15, 20);
	APBG.SetPosition(IconSize * 0.05, IconSize * 1.15 - 25);
	APBG.SetBGColor("0x000000");
	APBG.SetAlpha(66);

	ActionPoints = Spawn(class'UIText', self).InitText(, "");
	ActionPoints.SetX(IconSize * 0.08);
	ActionPoints.SetY(IconSize * 1.15 - 30);
	ActionPoints.SetSize(IconSize, 30);
	ActionPoints.SetColor("0xffffff");

	NameBG = Spawn(class'UIBGBox', self).InitBG();
	NameBG.SetSize(IconSize * 0.9, 10 + FontSize);
	NameBG.SetPosition(IconSize * 0.05, (IconSize * 1.15) + 11);
	NameBG.SetBGColor("0x000000");
	NameBG.SetAlpha(50);

	NameCheck = UnitState.GetName(eNameType_Nick);
	//`log(UnitID@"has name length of"@Len(NameCheck),,'QuickInfo');
	if(Len(NameCheck) <= 1)
	{
		NameCheck = Left(UnitState.GetName(eNameType_First), 1) $ "." @ UnitState.GetName(eNameType_Last);
	}

	NameCheck = class'UIUtilities_Text'.static.GetSizedText(NameCheck, FontSize);

	UnitName = Spawn(class'UIScrollingText', self).InitScrollingText(, NameCheck, IconSize * 0.8, IconSize * 0.1, IconSize * 1.15 + 11);

	BuffIcon = Spawn(class'UIIcon', self).InitIcon(,, false, false);
	BuffIcon.SetSize(16, 16);
	BuffIcon.SetX(4);
	BuffIcon.SetY(IconSize * 1.07);
	BuffIcon.bDisableSelectionBrackets = true;
	BuffIcon.Hide();

	DebuffIcon = Spawn(class'UIIcon', self).InitIcon(,, false, false);
	DebuffIcon.SetSize(16, 16);
	DebuffIcon.SetX(24);
	DebuffIcon.SetY(IconSize * 1.07);
	DebuffIcon.bDisableSelectionBrackets = true;
	DebuffIcon.Hide();

	UpdateData();

	return self;
}

simulated function ResizeAll(int NewIconSize)
{
	IconSize = NewIconSize;
	SetSize(IconSize * 1.16, IconSize * 1.15 + 38);
	SoldierHeadshot.SetHeight(IconSize - 5);
	SoldierHeadshot.SetWidth(IconSize - 5);
	SoldierHeadshot.SetY(IconSize * 0.15);
	ImageMask.SetSize(IconSize, IconSize);
	ImageMask.SetY(IconSize * 0.15);
	StatusIconLarge.SetSize(IconSize * 0.64, IconSize * 0.64);
	StatusIconLarge.SetX(IconSize * 0.18);
	StatusIconLarge.SetY(IconSize * 0.33);
	StatusTextLarge.SetX(IconSize * 0.64);
	StatusTextLarge.SetY(IconSize * 0.4);
	StatusTextLarge.SetWidth(IconSize * 0.32);
	StatusTextLarge.SetHeight(IconSize * 0.50);
	HPContainer.SetSize(IconSize, 6);
	HPBar.SetSize(IconSize * 0.96, 4);
	WillBar.SetSize(IconSize * 0.96, 4);
	HPContainer.SetY(IconSize * 1.15);
	SurroundBox.SetSize(IconSize, IconSize + 12);
	SurroundBox.SetY(IconSize * 0.15);
	ClassIcon.SetSize(IconSize * 0.4, IconSize * 0.4);
	ClassIcon.SetX(IconSize * 0.7 - 5);
	ClassIcon.SetY(IconSize * 0.85 - 5);
	RankIcon.SetSize(IconSize * 0.33, IconSize * 0.33);
	RankIcon.SetX(IconSize * 0.82 - 5);
	APBG.SetSize(15, 20);
	APBG.SetPosition(IconSize * 0.05, IconSize * 1.15 - 25);
	ActionPoints.SetX(IconSize * 0.05 + 3);
	ActionPoints.SetY(IconSize * 1.15 - 30);
	ActionPoints.SetSize(IconSize, 30);
	NameBG.SetSize(IconSize * 0.9, 10 + FontSize);
	NameBG.SetPosition(IconSize * 0.05, IconSize * 1.15 + 11);
	UnitName.SetPosition(IconSize * 0.1, IconSize * 1.15 + 11);
	UnitName.SetWidth(IconSize * 0.8);
	BuffIcon.SetSize(16, 16);
	BuffIcon.SetX(4);
	BuffIcon.SetY(IconSize * 1.07);
	DebuffIcon.SetSize(16, 16);
	DebuffIcon.SetX(24);
	DebuffIcon.SetY(IconSize * 1.07);
}

simulated function OnMouseUp()
{
	XComTacticalController(PC).Visualizer_SelectUnit(UnitState); 
}

simulated function SetFocus(bool isFocus)
{
	if (isFocus)
	{
		SurroundBox.SetOutline(true, "0x00ffff");
		SurroundBox.Show();
	}
	else
	{
		if (IsSelected)
		{
			SurroundBox.SetOutline(true, "0xffffff");
			SurroundBox.Show();
		}
		else
		{
			SurroundBox.Hide();
		}
	}
}

simulated function UpdateWill(int Will, int MaxWill)
{
	local float willPercent;

	willPercent = float(will) / float(MaxWill);
	WillBar.SetPercent(willPercent);
}

simulated function UpdateHP(int HP, int MaxHP, int Armor)
{
	local UIIcon armorPip;
	local int i;
	local float healthPercent;

	Armor = Clamp(Armor, 0, 7); // Cap to 7 armor

	if (Armor != ArmorPips.Length)
	{
		for (i = 0; i < ArmorPips.Length; i++)
		{
			if (i >= Armor)
			{
				ArmorPips[i].Hide();
				ArmorPips[i].Remove();
			}
		}
		if (ArmorPips.Length > Armor)
		{
			ArmorPips.Length = Armor;
		}
		else
		{
			for (i = ArmorPips.Length; i < Armor; i++)
			{
				armorPip = HPContainer.Spawn(class'UIIcon', HPContainer).InitIcon(,, false, true);
				armorPip.SetSize(1, 1);
				armorPip.SetY(2);
				armorPip.SetX(IconSize - 6 - (6 * i));
				armorPip.SetBGColor("0xffff00");
				armorPip.bDisableSelectionBrackets = true;
				ArmorPips.AddItem(armorPip);
			}
		}

		HPBar.SetSize(IconSize - 6 - (6 * Armor), 4);
	}
	healthPercent = float(HP) / float(MaxHP);
	HPBar.SetPercent(healthPercent);
}

simulated function CheckUnitStatus()
{
	StatusTextLarge.Hide();
	ImageMask.Hide();
	if (UnitState.IsBleedingOut())
	{
		//`log(UnitID@"is bleeding out",,'QuickInfoStatus');
		StatusIconLarge.LoadIcon("img:///UILibrary_Common.status_poison");
		StatusIconLarge.Show();
		StatusTextLarge.SetText(string(UnitState.GetBleedingOutTurnsRemaining()));
		StatusTextLarge.Show();
		ImageMask.SetAlpha(50);
		ImageMask.Show();
		UpdateHP(1, 10 , 0);
	}
	else if (UnitState.IsDead())
	{
		//`log(UnitID@"is dead",,'QuickInfoStatus');
		StatusIconLarge.LoadIcon("img:///UILibrary_Common.status_poison");
		StatusIconLarge.Show();
		ImageMask.SetAlpha(50);
		ImageMask.Show();
		UpdateHP(0, 10 , 0);
	}
	else if (UnitState.IsMindControlled() && !UnitState.IsPlayerControlled())
	{
		StatusIconLarge.LoadIcon("img:///UILibrary_Common.status_mindcontrolled");
		StatusIconLarge.Show();
		ImageMask.SetAlpha(50);
		ImageMask.Show();
	}
	else if (UnitState.IsUnconscious())
	{
		StatusIconLarge.LoadIcon("img:///UILibrary_Common.status_unconscious");
		StatusIconLarge.Show();
		ImageMask.SetAlpha(50);
		ImageMask.Show();
	}
	else if (UnitState.IsStunned())
	{
		StatusIconLarge.LoadIcon("img:///UILibrary_Common.status_stunned");
		StatusIconLarge.Show();
		ImageMask.SetAlpha(50);
		ImageMask.Show();
	}
	else if (UnitState.IsDisoriented())
	{
		StatusIconLarge.LoadIcon("img:///UILibrary_Common.status_disoriented");
		StatusIconLarge.Show();
	}
	else if (UnitState.IsBurning() || UnitState.IsAcidBurning())
	{
		StatusIconLarge.LoadIcon("img:///UILibrary_Common.status_burning");
		StatusIconLarge.Show();
	}
	else if (UnitState.ReserveActionPoints.Find('Overwatch') > -1)
	{
		StatusIconLarge.LoadIcon("img:///UILibrary_Common.status_overwatch");
		StatusIconLarge.Show();
	}
	else
	{
		StatusIconLarge.Hide();
	}
}

simulated function UpdateData(optional XComGameState relevantGameState)
{
	local string MoveStr;
	local array<UISummary_UnitEffect> UnitEffects;
	local XComGameState_Unit unit;
	
	if( relevantGameState != None )
	{
		unit = XComGameState_Unit(relevantGameState.GetGameStateForObjectID(UnitID));
		if (unit != none)
			UnitState = unit;
			//`log(UnitID@"GameState is updated",,'QuickInfoGameStateUpdate');
	}

	UpdateHP(UnitState.GetCurrentStat(eStat_HP), UnitState.GetMaxStat(eStat_HP), UnitState.GetArmorMitigationForUnitFlag());
	UpdateWill(UnitState.GetCurrentStat(eStat_Will), UnitState.GetMaxStat(eStat_Will));

	// Action Points
	MoveStr = string(UnitState.NumAllActionPoints());
	if (UnitState.bRemovedFromPlay || UnitState.IsDead() || UnitState.IsUnconscious())
	{
		// Units removed from play has no action points
		MoveStr = "0";
	}

	if (MoveStr == "0")
	{
		SetAlpha(50);
	}
	else
	{
		SetAlpha(100);
	}
	ActionPoints.SetText(MoveStr);

	// Buffs and debuffs
	UnitEffects = UnitState.GetUISummary_UnitEffectsByCategory(ePerkBuff_Penalty);

	if (UnitEffects.Length > 0)
	{
		DebuffIcon.Show();
	}
	else
	{
		DebuffIcon.Hide();
	}

	UnitEffects = UnitState.GetUISummary_UnitEffectsByCategory(ePerkBuff_Bonus);

	if (UnitEffects.Length > 0)
	{
		BuffIcon.Show();
	}
	else
	{
		BuffIcon.Hide();
	}

	// Status effects
	CheckUnitStatus();

	// Is Selected
	IsSelected = XGUnit(UnitState.GetVisualizer()).IsActiveUnit();

	SetFocus(false);
}

simulated function SetIconImage(string path)
{
	SoldierHeadshot.LoadIcon(path);
	SoldierHeadshot.SetSize(IconSize - 5, IconSize - 5);
}

simulated function bool GetAlienPortraits()
{
	if (!class'UISoldierQuickIconPanel'.default.TEMP_ICONS)
		return false;
	switch (UnitState.GetMyTemplateName())
	{
		case 'Sectoid':
			SetIconImage("img:///QSIcon.ICON_Sectoid");
			return true;
			break;
		case 'Viper':
			SetIconImage("img:///QSIcon.ICON_Viper");
			return true;
			break;
		case 'PsiZombie':
		case 'PsiZombieHuman':
			SetIconImage("img:///QSIcon.Zombie");
			return true;
			break;
		case 'XComTurretM1':
		case 'XComTurretM2':
		case 'AdvTurretM1':
		case 'AdvShortTurretM1':
		case 'AdvTurretM2':
		case 'AdvShortTurretM2':
		case 'AdvTurretM3':
		case 'AdvShortTurretM3':
		case 'AdvShortTurret':
			SetIconImage("img:///QSIcon.ICON_Turret");
			return true;
			break;
		case 'Sectopod':
		case 'PrototypeSectopod':
			SetIconImage("img:///QSIcon.ICON_Sectopod");
			return true;
			break;
		case 'Muton':
			SetIconImage("img:///QSIcon.ICON_Muton");
			return true;
			break;
		case 'Gatekeeper':
			SetIconImage("img:///QSIcon.ICON_Gatekeeper");
			return true;
			break;
		case 'Faceless':
			SetIconImage("img:///QSIcon.ICON_Faceless");
			return true;
			break;
		case 'Cyberus':
			SetIconImage("img:///QSIcon.ICON_Cyberus");
			return true;
			break;
		case 'Chryssalid':
			SetIconImage("img:///QSIcon.ICON_Chryssalid");
			return true;
			break;
		case 'Berserker':
			SetIconImage("img:///QSIcon.ICON_Berserker");
			return true;
			break;
		case 'Archon':
			SetIconImage("img:///QSIcon.ICON_Archon");
			return true;
			break;
		case 'Andromedon':
		case 'AndromedonRobot':
			SetIconImage("img:///QSIcon.ICON_Andromedon");
			return true;
			break;
		case 'AdvMEC_M1':
		case 'AdvMEC_M2':
			SetIconImage("img:///QSIcon.ICON_AdventMEC");
			return true;
			break;
		case 'AdvTrooperM1':
		case 'AdvCaptainM1':
		case 'AdvStunLancerM1':
		case 'AdvTrooperM2':
		case 'AdvCaptainM2':
		case 'AdvShieldBearerM2':
		case 'AdvStunLancerM2':
		case 'AdvTrooperM3':
		case 'AdvCaptainM3':
		case 'AdvShieldBearerM3':
		case 'AdvStunLancerM3':
			SetIconImage("img:///QSIcon.ICON_Advent");
			return true;
			break;
		case 'AdvPsiWitchM2':
		case 'AdvPsiWitchM3':
			SetIconImage("img:///uilibrary_strategyimages.ResearchTech.GOLDTECH_Avatar");
			return true;
			break;
		default:
			break;
	}
	return false;
}

simulated function MakeHeadshot()
{
	local Texture2D StaffPicture;
	local XComGameState_CampaignSettings SettingsState;

	local StateObjectReference UnitRef;

	
	UnitRef = UnitState.GetReference();

	SettingsState = XComGameState_CampaignSettings(`XCOMHISTORY.GetSingleGameStateObjectForClass(class'XComGameState_CampaignSettings'));
	StaffPicture = `XENGINE.m_kPhotoManager.GetHeadshotTexture(SettingsState.GameIndex, UnitRef.ObjectID, 128, 128);

	if(StaffPicture == none)
	{
		GetAlienPortraits();
	}	
	else
	{
		SetIconImage(class'UIUtilities_Image'.static.ValidateImagePath(PathName(StaffPicture)));
	}
}

simulated function Cleanup()
{
	RemoveChildren();
	Remove();
}