#include "nw_i0_spells"

void main()
{
    object oPC = GetLastUsedBy();

    // Check if the player has the broom in their inventory
    int hasBroom = FALSE;
    object oItem = GetFirstItemInInventory(oPC);

    while (GetIsObjectValid(oItem))
    {
        string itemTag = GetTag(oItem);
       // SendMessageToPC(oPC, "Checking item in inventory: " + itemTag); // Debug message

        if (itemTag == "I_CAS_MOP")
        {
            hasBroom = TRUE;
            //SendMessageToPC(oPC, "Broom found in inventory!"); // Debug message
            break;
        }
        oItem = GetNextItemInInventory(oPC);
    }

    // Check if the broom is equipped in the right hand
    object oEquippedItem = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC);
    if (GetIsObjectValid(oEquippedItem) && GetTag(oEquippedItem) == "I_CAS_MOP")
    {
        hasBroom = TRUE;
        //SendMessageToPC(oPC, "Equipped broom found!"); // Debug message
    }

    // If the player has the broom, delete the placeable
    if (hasBroom)
    {
        object oPlaceable = OBJECT_SELF;
        DestroyObject(oPlaceable);
        SendMessageToPC(oPC, "You mop  up the pixie dust with the ...mop.");
    }
    else
    {
        // Apply one of the three effects randomly
        int nEffect = Random(4);

        switch (nEffect)
        {
            case 0: // Invisibility for 1 turn
                ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectInvisibility(INVISIBILITY_TYPE_NORMAL), oPC, TurnsToSeconds(1));
                break;

            case 1: // Confusion for 5 rounds if failed DC 18 Will save
                if (!MySavingThrow(SAVING_THROW_WILL, oPC, 42, SAVING_THROW_TYPE_SPELL, OBJECT_SELF))
                {
                    ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectDazed(), oPC, RoundsToSeconds(5));
                }
                break;

            case 2: // VFX_DUR_PIXIEDUST for 10 turns
                ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectVisualEffect(VFX_DUR_PIXIEDUST), oPC, TurnsToSeconds(10));
                break;

            case 3: // VFX_FNF_GAS_EXPLOSION_NATURE
                ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_GAS_EXPLOSION_NATURE), oPC);
                break;
        }
    }
}