ScriptName VCA_Affinity_List extends ActiveMagicEffect

;-- Properties --------------------------------------

GlobalVariable Property DisplayMode Auto
GlobalVariable Property SupportItemSorting Auto

FormList Property CompanionReferenceList Auto
FormList Property CompanionAvailabilityList Auto
FormList Property ThresholdMessageList Auto

Message Property Affinity_List_Menu Auto
Message Property Affinity_List_Empty Auto

Form Property TriggerItem Auto
Form Property TriggerItemSortable Auto

Float Property IsCompanion = 0 Auto conditional

Faction Property CurrentCompanionFaction Auto
Faction Property HasBeenCompanionFaction Auto

;-- Variables ---------------------------------------

int ExitButton = 13

FormList DisplayMessageList

;-- Functions ---------------------------------------

Function OnEffectStart(Actor akTarget, Actor akCaster)
	If SupportItemSorting.GetValue() == 0
		Game.GetPlayer().AddItem(TriggerItem, 1, True)
	Else
		Game.GetPlayer().AddItem(TriggerItemSortable, 1, True)
	EndIf
	
	If ( DisplayMode.GetValue() == 0 )
		DisplayMessageList = ThresholdMessageList.GetAt(0) as FormList
	ElseIf( DisplayMode.GetValue() == 1 )
		DisplayMessageList = ThresholdMessageList.GetAt(1) as FormList
	ElseIf( DisplayMode.GetValue() == 2 )
		DisplayMessageList = ThresholdMessageList.GetAt(2) as FormList
	ElseIf( DisplayMode.GetValue() == 3 )
		DisplayMessageList = ThresholdMessageList.GetAt(3) as FormList
	EndIf
	
	ProgramStart()
	If ( IsCompanion )
		MenuEntry()
	Else 
		Affinity_List_Empty.Show(0, 0, 0, 0, 0, 0, 0, 0, 0)
	EndIf
EndFunction

Function MenuEntry()
	int buttonpressed = Affinity_List_Menu.Show(0, 0, 0, 0, 0, 0, 0, 0, 0)
	If ( buttonpressed < ExitButton )
		ShowAffinity(CompanionReferenceList.GetAt(buttonpressed) as Actor, DisplayMessageList.GetAt(buttonpressed) as FormList)
		MenuEntry()
	EndIf
EndFunction

Function ProgramStart()
	IsCompanion = 0
	Int i = 0
	While i < CompanionReferenceList.GetSize()
		Actor akCompanion = CompanionReferenceList.GetAt(i) as Actor
		If (akCompanion.IsInFaction(CurrentCompanionFaction) || akCompanion.IsInFaction(HasBeenCompanionFaction))
			GlobalVariable IsCompanionActor = CompanionAvailabilityList.GetAt(i) as GlobalVariable
			IsCompanionActor.SetValue(1)
			IsCompanion = 1
		EndIf
		i += 1
	EndWhile
EndFunction

Function ShowAffinity(ObjectReference akCompRef, FormList MessageList)
	Actor akComp = akCompRef as Actor
	Float IsRomantic = akComp.getValue(followersscript.GetScript().CA_IsRomantic)
	Float CurrentThreshold = akComp.getValue(followersscript.GetScript().CA_CurrentThreshold)
	Bool IsDaed = akComp.IsDead()
	float CurrentAffinity = akComp.getValue(followersscript.GetScript().CA_Affinity)
	If (DisplayMode.getValue() < 3)
		CurrentAffinity = CurrentAffinity / 10
	EndIf
	Message ThresholdMessage = MessageList.GetAt(getThresholdIndex(CurrentThreshold, IsRomantic, IsDaed)) as Message
	ThresholdMessage.show(CurrentAffinity, 0, 0, 0, 0, 0, 0, 0, 0)
EndFunction

Int Function getThresholdIndex(Float CurrentTreshold, Float IsRomantic, Bool IsDead)
	If (IsDead)
		Return 0
	ElseIf (IsRomantic > 0)
		Return 6
	ElseIf (CurrentTreshold == 1000)
		Return 5
	ElseIf (CurrentTreshold == 500)
		Return 4
	ElseIf (CurrentTreshold == -500)
		Return 2
	ElseIf (CurrentTreshold == -1000)
		Return 1
	Else
		Return 3
	EndIf
EndFunction
