dofile( "data/scripts/lib/utilities.lua" )

local entity_id = GetUpdatedEntityID()
local pos_x, pos_y, current_rotation = EntityGetTransform( entity_id )
local projectile_component = EntityGetFirstComponent( entity_id, "ProjectileComponent" )
local velocity_component = EntityGetFirstComponent( entity_id, "VelocityComponent" )

local speed = 10


if(projectile_component ~= nil)then
    EntityLoad( "mods/spellbound_bundle/files/entities/misc/bubble_projectiles_explosion.xml", pos_x, pos_y )

    local components = EntityGetComponent( entity_id, "VariableStorageComponent" )

    local storage_index = 0

    local last_comp_id = 0

    for key,comp_id in pairs(components) do 
        local var_name = ComponentGetValue( comp_id, "name" )

        if(string.match(var_name, "original_ProjectileComponent_speed_max"))then
            storage_index = storage_index + 1
            local value =  ComponentGetValue( comp_id, "value_string" ) 
            speed = tostring(value)
            
            --print(var_name)

            last_comp_id = comp_id
        end
    end
 
  --  print(tostring(storage_index))

    if(storage_index == 1)then

      -- print("this ran")

        local sprite_components = EntityGetComponent(entity_id, "SpriteComponent")
        for k, sprite_component in ipairs(sprite_components) do
            local sprite = ComponentGetValue(sprite_component, "image_file")
            if(sprite == "mods/spellbound_bundle/files/gfx/projectile_gfx/bubble_projectiles.xml" )then
                EntityRemoveComponent( entity_id, sprite_component )
            end
        end

        for key,comp_id in pairs(components) do 
            local var_name = ComponentGetValue( comp_id, "name" )

        
            if(var_name == "original_ProjectileComponent_bounce_always")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(projectile_component, "bounce_always", value)
                
            elseif(var_name == "original_ProjectileComponent_bounces_left")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(projectile_component, "bounces_left", value)

            elseif(var_name == "original_ProjectileComponent_bounce_energy")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(projectile_component, "bounce_energy", value)
    
            elseif(var_name == "original_ProjectileComponent_speed_min")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(projectile_component, "speed_min", value)
    
            elseif(var_name == "original_ProjectileComponent_speed_min")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(projectile_component, "speed_min", value)
    
            elseif(var_name == "original_ProjectileComponent_speed_max")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(projectile_component, "speed_max", value)
                speed = tostring(value)
    
            elseif(var_name == "original_ProjectileComponent_friction")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(projectile_component, "friction", value)
        
            elseif(var_name == "original_VelocityComponent_friction")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(velocity_component, "friction", value)
        
            elseif(var_name == "original_VelocityComponent_air_friction")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(velocity_component, "air_friction", value)
            elseif(var_name == "original_VelocityComponent_gravity_x")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(velocity_component, "gravity_x", value)
        
            elseif(var_name == "original_VelocityComponent_gravity_y")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(velocity_component, "gravity_y", value)
        
            elseif(var_name == "original_ProjectileComponent_lifetime")then
                local value =  ComponentGetValue( comp_id, "value_string" ) 
                ComponentSetValue(projectile_component, "lifetime", value)
                                                                                                                        
            end
            
        end
    end
    if(storage_index > 1)then    
        EntityRemoveComponent(entity_id, last_comp_id)

        EntityAddComponent( entity_id, "LuaComponent", 
        { 
            script_source_file="mods/spellbound_bundle/files/scripts/projectiles/bubble_projectiles_delayed_cast.lua",
            execute_every_n_frame="60",
            remove_after_executed="1",
        } )

    end

    local closest_enemy = EntityGetClosestWithTag( pos_x, pos_y, "homing_target" )
    if(closest_enemy ~= nil)then
        
        local enemy_x, enemy_y = EntityGetTransform( closest_enemy )

        local direction_x = enemy_x - pos_x
        local direction_y = enemy_y - pos_y

        edit_component( entity_id, "VelocityComponent", function(comp,vars)

            local vel_x = direction_x
    
            local vel_y = direction_y
            
            local len = math.sqrt((vel_x*vel_x) + (vel_y*vel_y))
    
            local dir_x = vel_x / len
            local dir_y = vel_y / len
    
            local force_x = dir_x * speed
            local force_y = dir_y * speed
            
            ComponentSetValueVector2( comp, "mVelocity", force_x, force_y )
    
        end)	
    end
    
end