//::///////////////////////////////////////////////
//:: Throw Rotten Fruit
//:: throw_rotten_fruit
//:: Script to throw rotten fruit, creating a visual effect without causing damage.
//:: Includes debugging messages for testing.
//:://////////////////////////////////////////////

#include "X2_I0_SPELLS"

const int VFX_IMPACT_EFFECT = VFX_COM_CHUNK_GREEN_MEDIUM;  // The impact VFX
const int VFX_LINGER_EFFECT = VFX_DUR_FLIES;               // The lingering flies VFX
const float LINGER_DURATION = 10.0;                         // Duration in seconds

void main()
{
    // Get the player who is throwing the fruit
    object oPlayer = OBJECT_SELF;

    // Create the VFX for the impact
    effect eImpact = EffectVisualEffect(VFX_IMPACT_EFFECT);
    
    // Create the VFX for the lingering flies
    effect eLinger = EffectVisualEffect(VFX_LINGER_EFFECT);

    // Get the target of the throw action
    object oTarget = GetSpellTargetObject();
    int nTarget = GetObjectType(oTarget);

    // Debug message: Show target type
    string sDebugMsg = "Throw Rotten Fruit: ";
    if (nTarget == OBJECT_TYPE_CREATURE) sDebugMsg += "Target is a creature.";
    else if (nTarget == OBJECT_TYPE_DOOR) sDebugMsg += "Target is a door.";
    else if (nTarget == OBJECT_TYPE_PLACEABLE) sDebugMsg += "Target is a placeable.";
    else sDebugMsg += "No valid target. Using ground location.";

    SendMessageToPC(oPlayer, sDebugMsg);

    // Check if the target is a creature, door, or placeable
    if (nTarget == OBJECT_TYPE_CREATURE || nTarget == OBJECT_TYPE_DOOR || nTarget == OBJECT_TYPE_PLACEABLE)
    {
        // Valid target, apply the impact VFX
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eImpact, oTarget);
        SendMessageToPC(oPlayer, "Impact VFX applied to target.");

        // Apply the lingering VFX for the flies
        ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLinger, oTarget, LINGER_DURATION);
        SendMessageToPC(oPlayer, "Lingering flies VFX applied to target.");
    }
    else
    {
        // No valid target, apply the VFX to the ground
        location lTargetLocation = GetSpellTargetLocation();

        // Apply the impact VFX at the location
        ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eImpact, lTargetLocation);
        SendMessageToPC(oPlayer, "Impact VFX applied at ground location.");

        // Apply the lingering VFX for the flies at the location
        ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eLinger, lTargetLocation, LINGER_DURATION);
        SendMessageToPC(oPlayer, "Lingering flies VFX applied at ground location.");
    }
}
