local util = require("util")

local created_items = function()
  return
  {
    ["copper-plate"] = 200,
    ["iron-gear-wheel"] = 200,
    ["wood"] = 200,
    ["pistol"] = 1,
    ["firearm-magazine"] = 100,
    ["electric-mining-drill"] = 400,
    ["transport-belt"] = 4000,
    ["stone-furnace"] = 450,
    ["assembling-machine-1"] = 200,
    ["pipe"] = 100,
    ["pipe-to-ground"] = 20,
    ["lab"] = 20,
    ["fast-inserter"] = 1000,
    ["long-handed-inserter"] = 200,
    ["offshore-pump"] = 10,
    ["small-electric-pole"] = 600,
    ["steam-engine"] = 120,
    ["underground-belt"] = 400,
    ["splitter"] = 400,
    ["medium-electric-pole"] = 400,
    ["boiler"] = 60
  }
end

local respawn_items = function()
  return
  {
    ["pistol"] = 1,
    ["firearm-magazine"] = 10
  }
end

local on_player_created = function(event)
  local player = game.players[event.player_index]
  player.game_view_settings.show_rail_block_visualisation = true
  player.character_inventory_slots_bonus=100
  player.character_running_speed_modifier=1
  player.character_build_distance_bonus=500
  player.character_reach_distance_bonus=500
  player.character_item_drop_distance_bonus=500
  player.character_resource_reach_distance_bonus=500
  player.character_item_pickup_distance_bonus=2
  player.character_logistic_slot_count_bonus=30
  player.character_trash_slot_count_bonus=20
  player.character_health_bonus=10000
--  player.surface.always_day=true
  util.insert_safe(player, global.created_items)
  
  player.insert{name="power-armor-mk2", count = 1}
  local p_armor = player.get_inventory(5)[1].grid
  p_armor.put({name = "fusion-reactor-equipment"})
  p_armor.put({name = "fusion-reactor-equipment"})
  p_armor.put({name = "fusion-reactor-equipment"})
  p_armor.put({name = "fusion-reactor-equipment"})
  p_armor.put({name = "personal-roboport-mk2-equipment"})
  p_armor.put({name = "personal-roboport-mk2-equipment"})
  p_armor.put({name = "personal-roboport-mk2-equipment"})
  p_armor.put({name = "personal-roboport-mk2-equipment"})
  p_armor.put({name = "personal-roboport-mk2-equipment"})
  p_armor.put({name = "battery-mk2-equipment"})
  p_armor.put({name = "battery-mk2-equipment"})
  p_armor.put({name = "battery-mk2-equipment"})
  p_armor.put({name = "battery-mk2-equipment"})
  p_armor.put({name = "battery-mk2-equipment"})
  p_armor.put({name = "battery-mk2-equipment"})
  player.insert{name="construction-robot", count = 100}  
  
  
  
  
  local r = global.chart_distance or 200
  player.force.chart(player.surface, {{player.position.x - r, player.position.y - r}, {player.position.x + r, player.position.y + r}})

  if not global.skip_intro then
    if game.is_multiplayer() then
      player.print({"msg-intro"})
    else
      game.show_message_dialog{text = {"msg-intro"}}
    end
  end
end

local on_player_respawned = function(event)
  local player = game.players[event.player_index]
  util.insert_safe(player, global.respawn_items)
end

local freeplay = {}

freeplay.events =
{
  [defines.events.on_player_created] = on_player_created,
  [defines.events.on_player_respawned] = on_player_respawned,
}

freeplay.on_configuration_changed = function(event)
  global.created_items = global.created_items or created_items()
  global.respawn_items = global.respawn_items or respawn_items()
end

freeplay.on_init = function()
  global.created_items = created_items()
  global.respawn_items = respawn_items()
end

freeplay.add_remote_interface = function()
  remote.add_interface("freeplay",
  {
    get_created_items = function()
      return global.created_items
    end,
    set_created_items = function(map)
      global.created_items = map
    end,
    get_respawn_items = function()
      return global.respawn_items
    end,
    set_respawn_items = function(map)
      global.respawn_items = map
    end,
    set_skip_intro = function(bool)
      global.skip_intro = bool
    end,
    set_chart_distance = function(value)
      global.chart_distance = tonumber(value)
    end
  })
end

return freeplay
