#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 IMPACT_DELAY = 1.0;                            // Delay before impact VFX
const float FLIES_DELAY = 1.5;                             // Delay before flies VFX

void main()
{
    // Retrieve the player character using the item
    object oPC = GetItemActivator();
    object oCaster = OBJECT_SELF;

    // Get the parameters from the event
    object oItem = GetEventParamObject(0);    // The item being used
    object oTarget = GetEventParamObject(1);  // The target of the throw (could be invalid)
    object oUser = GetEventParamObject(2);    // The user of the item (oPC should be the same as oUser)

    // Get the area from the player character for creating the target location
    object oArea = GetArea(oPC);

    // Create a target location using the coordinates from the event parameter
    vector vTargetPosition = GetEventParamVector(2);
    location lTarget = Location(oArea, vTargetPosition, 0.0);  // Facing angle is set to 0.0

    // Check if the target object is valid
    if (!GetIsObjectValid(oTarget))
    {
        // Use the created target location for the VFX

        // Apply the impact VFX at the location with delay
        DelayCommand(IMPACT_DELAY, ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMPACT_EFFECT), lTarget));

        // Apply the lingering VFX for the flies with delay
        effect eLinger = EffectVisualEffect(VFX_LINGER_EFFECT);
        DelayCommand(FLIES_DELAY, ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eLinger, lTarget, 10.0));  // Duration is directly 10.0 seconds

        return;
    }

    // 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);

    // Determine the type of target
    int nTarget = GetObjectType(oTarget);

    // Check if the target is a valid type (creature, door, placeable)
    if (nTarget == OBJECT_TYPE_CREATURE || nTarget == OBJECT_TYPE_DOOR || nTarget == OBJECT_TYPE_PLACEABLE)
    {
        // Valid target, apply the impact VFX with delay
        DelayCommand(IMPACT_DELAY, ApplyEffectToObject(DURATION_TYPE_INSTANT, eImpact, oTarget));

        // Apply the lingering VFX for the flies with delay
        DelayCommand(FLIES_DELAY, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLinger, oTarget, 10.0));  // Duration is directly 10.0 seconds
    }
    else
    {
        // No valid target, apply the VFX to the ground using the player's current location

        // Apply the impact VFX at the location with delay
        DelayCommand(IMPACT_DELAY, ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eImpact, lTarget));

        // Apply the lingering VFX for the flies at the location with delay
        DelayCommand(FLIES_DELAY, ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eLinger, lTarget, 10.0));  // Duration is directly 10.0 seconds
    }
}
