dofile( "data/scripts/lib/utilities.lua" )
dofile( "data/scripts/gun/gun_enums.lua" )
dofile( "data/scripts/gun/gun_actions.lua" )
dofile( "mods/spellbound_bundle/files/scripts/utils.lua" )
local entity_id    = GetUpdatedEntityID()
local pos_x, pos_y = EntityGetTransform( entity_id )

local cost_self = EntityGetFirstComponent(entity_id, "ItemCostComponent")

if(cost_self == nil)then
    local entities = EntityGetInRadiusWithTag( pos_x, pos_y, 50, "card_action" )

    modifiers_found = {}

    timer = timer or 60

    seed = StatsGetValue("world_seed")
    
    SetRandomSeed(seed + pos_x, seed + pos_y)

    for k, v in pairs(entities)do
        if(v ~= entity_id)then
            local action_comp = EntityGetFirstComponent(v, "ItemActionComponent")
            if(action_comp ~= nil)then
                local action_id = ComponentGetValue2(action_comp, "action_id")
                if(action_id)then
                    for _, action in pairs(actions)do
                        if(action.id == action_id)then
                            local cost = EntityGetFirstComponent(v, "ItemCostComponent")
                            if(cost == nil)then
                                table.insert(modifiers_found, v)

                                local x, y = EntityGetTransform(v)

                                local effect = EntityLoad("mods/spellbound_bundle/files/entities/projectiles/spell_marker.xml", x, y)
                                EntityAddChild(v, effect)
                            end
                        end
                    end
                end
            end
        end
    end



    if(#modifiers_found > 0)then
        timer = timer - 1
        EntitySetComponentsWithTagEnabled( entity_id, "transform_module", true )
    else
        EntitySetComponentsWithTagEnabled( entity_id, "transform_module", false )
    end



    if(#modifiers_found > 0 and timer < 0)then

		for k, v in pairs(modifiers_found)do
			local action_comp = EntityGetFirstComponent(v, "ItemActionComponent")
            if(action_comp ~= nil)then
                local action_id = ComponentGetValue2(action_comp, "action_id")
                if(action_id)then
					local id = action_id

					if(action_id == "SPELL_RUNE" or action_id == "PROJECTILE_RUNE" or action_id == "MODIFIER_RUNE")then
						local stored_spells = EntityGetVariable(entity_id, "storedspells", "string")
						local stored_spells2 = EntityGetVariable(v, "storedspells", "string")
						if(stored_spells2 ~= "")then
							EntitySetVariable(entity_id, "storedspells", "string", stored_spells..stored_spells2..",")
						end
					else
						local stored_spells = EntityGetVariable(entity_id, "storedspells", "string")
						EntitySetVariable(entity_id, "storedspells", "string", stored_spells..id..",")
						
					end

					EntityKill(v)
				end
			end
		end

		local all_spells = EntityGetVariable(entity_id, "storedspells", "string")
		local spell_count = 0

		for spell_id in string.gmatch(all_spells, '([^,]+)') do
			spell_count = spell_count + 1
		end

		local item_comp_self = EntityGetFirstComponent(entity_id, "ItemComponent")
		ComponentSetValue2(item_comp_self, "item_name", EntityGetVariable(entity_id, "module_name", "string").." with "..tostring(spell_count).." spells.")
		ComponentSetValue2(item_comp_self, "ui_sprite", EntityGetVariable(entity_id, "module_image", "string"))

		local sprite_comp = EntityGetFirstComponentIncludingDisabled(entity_id, "SpriteComponent", "item_identified")
						
		ComponentSetValue2(sprite_comp, "image_file", EntityGetVariable(entity_id, "module_image", "string"))




        EntityLoad(EntityGetVariable(entity_id, "transform_effect", "string"), pos_x, pos_y)

        GamePlaySound( "data/audio/Desktop/event_cues.bank", "event_cues/rune/destroy", pos_x, pos_y )

        timer = 60
    end
end