dofile_once("data/scripts/lib/utilities.lua")
local action_file = ""
local action_id = ""

local old_add_projectile = add_projectile
function add_projectile( entity_filename )

	local world_state = GameGetWorldStateEntity()
  	if is_valid_entity(world_state) then
		GlobalsSetValue( "projectile_filename", entity_filename )

		extra_modifiers["save_modifiers"] = function() 
			GlobalsSetValue("extra_entities", c.extra_entities)
			--c.extra_entities = c.extra_entities .. "mods/spellbound_bundle/files/entities/misc/store_modifiers.xml,"
		end
		
		table.insert(active_extra_modifiers,"save_modifiers") 

		action_file = entity_filename

	end
    old_add_projectile( entity_filename )
end

local old_add_projectile_trigger_timer = add_projectile_trigger_timer
function add_projectile_trigger_timer( entity_filename, delay_frames, action_draw_count )

	local world_state = GameGetWorldStateEntity()
  	if is_valid_entity(world_state) then
		GlobalsSetValue( "projectile_filename", entity_filename )

		extra_modifiers["save_modifiers"] = function() 
			GlobalsSetValue("extra_entities", c.extra_entities)
			--c.extra_entities = c.extra_entities .. "mods/spellbound_bundle/files/entities/misc/store_modifiers.xml,"
		end
		
		table.insert(active_extra_modifiers,"save_modifiers") 
		action_file = entity_filename
	end
    old_add_projectile_trigger_timer( entity_filename, delay_frames, action_draw_count )
end

local old_add_projectile_trigger_hit_world = add_projectile_trigger_hit_world
function add_projectile_trigger_hit_world( entity_filename, action_draw_count )

	local world_state = GameGetWorldStateEntity()
  	if is_valid_entity(world_state) then
		GlobalsSetValue( "projectile_filename", entity_filename )

		extra_modifiers["save_modifiers"] = function() 
			GlobalsSetValue("extra_entities", c.extra_entities)
			--c.extra_entities = c.extra_entities .. "mods/spellbound_bundle/files/entities/misc/store_modifiers.xml,"
		end
		
		table.insert(active_extra_modifiers,"save_modifiers") 
		action_file = entity_filename
	end
    old_add_projectile_trigger_hit_world( entity_filename, action_draw_count )
end

local old_add_projectile_trigger_death = add_projectile_trigger_death
function add_projectile_trigger_death( entity_filename, action_draw_count )
	local world_state = GameGetWorldStateEntity()
  	if is_valid_entity(world_state) then
		GlobalsSetValue( "projectile_filename", entity_filename )

		extra_modifiers["save_modifiers"] = function() 
			GlobalsSetValue("extra_entities", c.extra_entities)
			--c.extra_entities = c.extra_entities .. "mods/spellbound_bundle/files/entities/misc/store_modifiers.xml,"
		end
		
		table.insert(active_extra_modifiers,"save_modifiers") 
		action_file = entity_filename
	end
    old_add_projectile_trigger_death( entity_filename, action_draw_count )
end

local old_play_action = play_action
function play_action( action )
	old_play_action( action )
	action_id = action.id
	local world_state = GameGetWorldStateEntity()
  	if is_valid_entity(world_state) then
		GlobalsSetValue("ACTION_"..action.id, action_file)
	end	
end

--[[
function add_projectile_trigger_timer_multiple( entity_filename, delay_frames, action_draw_count )
	if reflecting then 
		Reflection_RegisterProjectile( entity_filename )
		return 
	end

	BeginProjectile( entity_filename )
	BeginTriggerTimer( delay_frames )
		draw_shot_multiple( create_shot( action_draw_count ), true )
	EndTrigger()
	EndProjectile()
end

function draw_shot_multiple( shot, instant_reload_if_empty )
	local c_old = c

	c = shot.state

	draw_actions_multiple( shot.num_of_cards_to_draw, instant_reload_if_empty )
	register_action( shot.state )
	SetProjectileConfigs()

	c = c_old
end

function draw_actions_multiple( how_many, instant_reload_if_empty )
	c.action_draw_many_count = how_many

	if playing_permanent_card and (how_many == 1) then
		return -- SPECIAL RULE: modifiers that use draw_actions(1) to draw one more action don't result in two actions being drawn after them if the modifier is permanently attached and wand 'casts 1'
	end
    first_run = false
    for i = 1, how_many do
        if(i == how_many)then
            first_run = true
        end
		local ok = draw_first_action( instant_reload_if_empty, first_run )
		if ok == false then
			-- attempt to draw other actions
			while #deck > 0 do
				if draw_first_action( instant_reload_if_empty, first_run ) then
					break
				end
			end
		end

		if reloading then
			return
		end
	end
	--action = deck[ 1 ]
	--table.remove( deck, 1 )
end

function draw_first_action( instant_reload_if_empty, first_run)
	local action = nil

	state_cards_drawn = state_cards_drawn + 1

	if reflecting then  return  end


	if ( #deck <= 0 ) then
		if instant_reload_if_empty then
			move_discarded_to_deck()
			order_deck()
			start_reload = true
		else
			reloading = true
			return true -- <------------------------------------------ RETURNS
		end
	end
	
	if #deck > 0 then
        -- draw from the start of the deck



		action = deck[ 1 ]

	if(first_run)then
        	table.remove( deck, 1 )
		end

		-- update mana
		local action_mana_required = action.mana
		if action.mana == nil then
			action_mana_required = ACTION_MANA_DRAIN_DEFAULT
		end

		if action_mana_required > mana then
			OnNotEnoughManaForAction()
			if(first_run)then
            table.insert( discarded, action )
			end
			return false -- <------------------------------------------ RETURNS
		end

		if action.uses_remaining == 0 then
			if(first_run)then
			table.insert( discarded, action )
			end
			return false -- <------------------------------------------ RETURNS
		end

		mana = mana - action_mana_required
	end

	--- add the action to hand and execute it ---
	if action ~= nil then
		play_action_spellbound( action, first_run )
	end

	return true
end


function play_action_spellbound( action, first_run )
	if(first_run)then
		table.insert( hand, action )

		set_current_action( action )
	end
	action.action()

	local is_projectile = false

	if action.type == ACTION_TYPE_PROJECTILE then
		is_projectile = true
		got_projectiles = true
	end

	if  action.type == ACTION_TYPE_STATIC_PROJECTILE then
		is_projectile = true
		got_projectiles = true
	end

	if action.type == ACTION_TYPE_MATERIAL then
		is_projectile = true
		got_projectiles = true
	end

	if is_projectile then
		for i,modifier in ipairs(active_extra_modifiers) do
			extra_modifiers[modifier]()
		end
	end

	OnActionPlayed( action.id )
	current_reload_time = current_reload_time + ACTION_DRAW_RELOAD_TIME_INCREASE
end]]