Scriptname FCP_ControllerScript extends Quest

Actor Property PlayerRef Auto
slaFrameworkScr Property SLA Auto

Faction Property NudeFaction Auto
Faction Property ToplessFaction Auto
Faction Property BottomlessFaction Auto
Faction Property ShowingChestFaction Auto
Faction Property ShowingGenitalsFaction Auto
Faction Property ShowingAssFaction Auto

Keyword Property NearlyNakedKeyword Auto
Keyword Property EffectivelyNakedKeyword Auto
Keyword Property MiniskirtKeyword Auto
Keyword Property ThongKeyword Auto
Keyword Property HotpantsKeyword Auto
Keyword Property TransparentTopKeyword Auto

MagicEffect Property Trimester1Effect Auto
MagicEffect Property Trimester2Effect Auto
MagicEffect Property Trimester3Effect Auto

GlobalVariable Property CommentEligible Auto
GlobalVariable Property Revealing Auto
GlobalVariable Property SluttyKeyword Auto
GlobalVariable Property PlayerPregnant Auto
GlobalVariable Property CommentWindow Auto
GlobalVariable Property PlayerAssExposed Auto
GlobalVariable Property PlayerChestExposed Auto
GlobalVariable Property PlayerWearsSkirt Auto
GlobalVariable Property PlayerWearsPanty Auto
ReferenceAlias Property LastCommenterAlias Auto
MiscObject Property Gold001 Auto

Float Property PollSeconds = 50.0 Auto
Int Property ExposureGain = 1 Auto

Event OnInit()
    ScanPlayer()
    RegisterForSingleUpdate(PollSeconds)
EndEvent

Event OnUpdate()
    ScanPlayer()
    RegisterForSingleUpdate(PollSeconds)
EndEvent

Function ScanPlayer()
    ; The activation-only player branches are available until the next poll
    ; after one of this mod's comments has actually played.
    If CommentWindow.GetValue() > 0
        CommentWindow.SetValue(0)
    EndIf
    Bool isRevealing = PlayerRef.GetFactionRank(NudeFaction) >= 0 || \
        PlayerRef.GetFactionRank(ToplessFaction) >= 0 || \
        PlayerRef.GetFactionRank(BottomlessFaction) >= 0 || \
        PlayerRef.GetFactionRank(ShowingChestFaction) >= 0 || \
        PlayerRef.GetFactionRank(ShowingGenitalsFaction) >= 0 || \
        PlayerRef.GetFactionRank(ShowingAssFaction) >= 0

    Bool hasKeyword = PlayerRef.WornHasKeyword(NearlyNakedKeyword) || \
        PlayerRef.WornHasKeyword(EffectivelyNakedKeyword) || \
        PlayerRef.WornHasKeyword(MiniskirtKeyword) || \
        PlayerRef.WornHasKeyword(ThongKeyword) || \
        PlayerRef.WornHasKeyword(HotpantsKeyword) || \
        PlayerRef.WornHasKeyword(TransparentTopKeyword)

    Bool isPregnant = PlayerRef.HasMagicEffect(Trimester1Effect) || \
        PlayerRef.HasMagicEffect(Trimester2Effect) || \
        PlayerRef.HasMagicEffect(Trimester3Effect)

    If isRevealing || hasKeyword
        SLA.UpdateActorExposure(PlayerRef, ExposureGain, "Fame Comment revealing outfit")
    EndIf

    Revealing.SetValue(isRevealing as Int)
    SluttyKeyword.SetValue(hasKeyword as Int)
    PlayerPregnant.SetValue(isPregnant as Int)
    PlayerAssExposed.SetValue((PlayerRef.GetFactionRank(ShowingAssFaction) >= 0) as Int)
    PlayerChestExposed.SetValue((PlayerRef.GetFactionRank(ShowingChestFaction) >= 0 || PlayerRef.GetFactionRank(ToplessFaction) >= 0) as Int)
    PlayerWearsSkirt.SetValue(PlayerRef.WornHasKeyword(MiniskirtKeyword) as Int)
    PlayerWearsPanty.SetValue(PlayerRef.WornHasKeyword(ThongKeyword) as Int)
    CommentEligible.SetValue((isRevealing || hasKeyword || isPregnant) as Int)

    ; String revealingStatus = "OFF"
    ; String keywordStatus = "OFF"
    ; String pregnancyStatus = "OFF"
    ; If isRevealing
    ;     revealingStatus = "ON"
    ; EndIf
    ; If hasKeyword
    ;     keywordStatus = "ON"
    ; EndIf
    ; If isPregnant
    ;     pregnancyStatus = "ON"
    ; EndIf
    ; Debug.Notification("FCP scan: revealing " + revealingStatus + ", keyword " + keywordStatus + ", pregnant " + pregnancyStatus)
EndFunction

Function RecordCommenter(ObjectReference akSpeakerRef)
    If akSpeakerRef
        LastCommenterAlias.ForceRefTo(akSpeakerRef)
        CommentWindow.SetValue(1)
    EndIf
EndFunction

Function ApplyResponseEffects(Actor speaker)
    If speaker
        SLA.UpdateActorExposure(speaker, 10, "FCP sexual response")
    EndIf
    PlayerRef.AddItem(Gold001, Utility.RandomInt(100, 1200), True)
    Int fame = ModEvent.Create("SLSF_Request_ModFameValueByCurrent")
    If fame
        ModEvent.PushInt(fame, 1)
        ModEvent.PushInt(fame, 17)
        ModEvent.PushInt(fame, 5)
        ModEvent.PushBool(fame, True)
        ModEvent.Send(fame)
    EndIf
EndFunction

Function StartSexWithSpeaker(ObjectReference akSpeakerRef)
    Actor speaker = akSpeakerRef as Actor
    If !speaker || speaker.IsDead() || speaker.IsChild()
        Return
    EndIf

    If !SexLabUtil.SexLabIsReady()
        Debug.Notification("Fame Comment: SexLab is not ready.")
        Return
    EndIf

    If SexLabUtil.IsActorActive(PlayerRef) || SexLabUtil.IsActorActive(speaker)
        Return
    EndIf

    ; Match TDF Aroused Rape's direct two-actor aggressive startup. The player
    ; explicitly selected the dialogue response before this fragment runs.
    SexLabUtil.QuickStart(PlayerRef, speaker, victim = PlayerRef, animationTags = "Aggressive, MF")
EndFunction
