//::///////////////////////////////////////////////
//:: Throw Rotten Fruit
//:: throw_rotten_fruit
//:: Script to throw rotten fruit, creating a visual effect without causing damage.
//:: Uses parameters from the event to determine target and item user.
//:: Includes debugging messages for testing and delays for visual effects.
//:://////////////////////////////////////////////

#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
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
    object oUser = GetEventParamObject(2);    // The user of the item (oPC should be the same as oUser)

    // Debugging: check retrieved objects
    SendMessageToPC(oPC, "Throw Rotten Fruit: Item activated by " + GetName(oUser) + ".");
    SendMessageToPC(oPC, "Target: " + (GetIsObjectValid(oTarget) ? GetName(oTarget) : "None"));

    // 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);

    // 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(oPC, sDebugMsg);

    // 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));
        SendMessageToPC(oPC, "Impact VFX will be applied to target after delay.");

        // Apply the lingering VFX for the flies with delay
        DelayCommand(FLIES_DELAY, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLinger, oTarget, LINGER_DURATION));
        SendMessageToPC(oPC, "Lingering flies VFX will be applied to target after delay.");
    }
    else
    {
        // No valid target, apply the VFX to the ground
        location lTargetLocation = GetLocation(oTarget); // Use oTarget's location if possible

        // Apply the impact VFX at the location with delay
        DelayCommand(IMPACT_DELAY, ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eImpact, lTargetLocation));
        SendMessageToPC(oPC, "Impact VFX will be applied at ground location after delay.");

        // Apply the lingering VFX for the flies at the location with delay
        DelayCommand(FLIES_DELAY, ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eLinger, lTargetLocation, LINGER_DURATION));
        SendMessageToPC(oPC, "Lingering flies VFX will be applied at ground location after delay.");
    }
}
