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

local entity_id    = GetUpdatedEntityID()
local pos_x, pos_y = EntityGetTransform( entity_id )

local projectile_component = EntityGetFirstComponent(entity_id, "ProjectileComponent")

if(projectile_component ~= nil)then
    local action_id = ComponentObjectGetValue2(projectile_component, "config", "action_id")

    local extra_entities = ComponentObjectGetValue2(projectile_component, "config", "extra_entities")

    local game_effect_entities = ComponentObjectGetValue2(projectile_component, "config", "game_effect_entities")

    local trail_material = ComponentObjectGetValue2(projectile_component, "config", "trail_material")

    local trail_material_amount = ComponentObjectGetValue2(projectile_component, "config", "trail_material_amount")

    local material = ComponentObjectGetValue2(projectile_component, "config", "material")

    local material_amount = ComponentObjectGetValue2(projectile_component, "config", "material_amount")

    --EntitySave(entity_id,"test_projectile.xml")

    --print(extra_entities)



    
    local close_enemies = EntityGetInRadiusWithTag(pos_x, pos_y, 100, "mortal")
    if(close_enemies ~= nil)then
        for k, enemy in pairs(close_enemies)do
            local has_action_stored = false    
            --[[

            local components = EntityGetComponent( enemy, "VariableStorageComponent" )

            if( components ~= nil ) then
            
                for key,comp_id in pairs(components) do 
                    local var_name = ComponentGetValue( comp_id, "name" )
            
                    if( var_name == "action_id") then
                        ComponentSetValue2(comp_id, "value_string", action_id)
                 
                        has_action_stored = true
                    end

                    if( var_name == "game_effect_entities")then
                        ComponentSetValue2(comp_id, "value_string", game_effect_entities)
                        has_action_stored = true
                    end

                    if( var_name == "trail_material") then
                        ComponentSetValue2(comp_id, "value_string", trail_material)
                        has_action_stored = true
                    end    
                    
                    if( var_name == "trail_material_amount") then
                        ComponentSetValue2(comp_id, "value_string", trail_material_amount)
                        has_action_stored = true
                    end     
                    
                    if( var_name == "material") then
                        ComponentSetValue2(comp_id, "value_string", material)
                        has_action_stored = true
                    end                              

                    if( var_name == "material_amount") then
                        ComponentSetValue2(comp_id, "value_string", material_amount)
                        has_action_stored = true
                    end          

                    if( var_name == "extra_entities") then
                        ComponentSetValue2(comp_id, "value_string", extra_entities)
                        has_action_stored = true
                    end                   
                end
            end       ]]

            local components = EntityGetComponent( enemy, "VariableStorageComponent" )

            if( components ~= nil ) then
            
                for key,comp_id in pairs(components) do 
                    local var_name = ComponentGetValue( comp_id, "name" )
            
                    if( var_name == "action_id") then
                        ComponentSetValue2(comp_id, "value_string", action_id)
                 
                        has_action_stored = true
                    end

                    if( var_name == "game_effect_entities")then
                        ComponentSetValue2(comp_id, "value_string", game_effect_entities)
                        has_action_stored = true
                    end

                    if( var_name == "trail_material") then
                        ComponentSetValue2(comp_id, "value_string", trail_material)
                        has_action_stored = true
                    end    
                    
                    if( var_name == "trail_material_amount") then
                        ComponentSetValue2(comp_id, "value_string", trail_material_amount)
                        has_action_stored = true
                    end     
                    
                    if( var_name == "material") then
                        ComponentSetValue2(comp_id, "value_string", material)
                        has_action_stored = true
                    end                              

                    if( var_name == "material_amount") then
                        ComponentSetValue2(comp_id, "value_string", material_amount)
                        has_action_stored = true
                    end          

                    if( var_name == "extra_entities") then
                        ComponentSetValue2(comp_id, "value_string", extra_entities)
                        has_action_stored = true
                    end                   
                end
            end 

            if(has_action_stored ~= true)then
              --  print("try add action ID")
                EntityAddComponent(enemy, "VariableStorageComponent", 
                { 
                    name = "action_id",
                    value_string = action_id,
                })                 
                EntityAddComponent(enemy, "VariableStorageComponent", 
                { 
                    name = "game_effect_entities",
                    value_string = game_effect_entities,
                })                 

                EntityAddComponent(enemy, "VariableStorageComponent", 
                { 
                    name = "extra_entities",
                    value_string = extra_entities,
                })

                EntityAddComponent(enemy, "VariableStorageComponent", 
                { 
                    name = "trail_material",
                    value_string = trail_material,
                })
                
                EntityAddComponent(enemy, "VariableStorageComponent", 
                { 
                    name = "trail_material_amount",
                    value_string = trail_material_amount,
                })       
                
                EntityAddComponent(enemy, "VariableStorageComponent", 
                { 
                    name = "material",
                    value_string = material,
                })                             
                
                EntityAddComponent(enemy, "VariableStorageComponent", 
                { 
                    name = "material_amount",
                    value_string = material_amount,
                })                              
            end            
        end
    end
end