local event = require 'utils.event'

local function create_score_button(player)
	if not player.gui.top.score then
		local button = player.gui.top.add({ type = "sprite-button", name = "score", sprite = "item/rocket-silo" })		
		button.style.minimal_height = 38
		button.style.minimal_width = 38
		button.style.top_padding = 2
		button.style.left_padding = 4
		button.style.right_padding = 4
		button.style.bottom_padding = 2
	end
end

function refresh_score()
	for _, player in (game.connected_players) do
		if player.gui.left["score_panel"] then
			show_score(player)
		end
	end
end

local function show_score(player)	
	if player.gui.left["score_panel"] then player.gui.left["score_panel"].destroy() end
	local score = global.score[player.force.name]
		
	local frame = player.gui.left.add { type = "frame", name = "score_panel" }
		
	local t = frame.add { type = "table", column_count = 4}
	
	local l = t.add { type = "label", caption = "Rockets launched: "}	
	l.style.font = "default-bold"			
	l.style.font_color = { r=0.98, g=0.66, b=0.22}
	l.style.minimal_width = 140
	
	local l = t.add { type = "label", caption = tostring(score.rocket_launches)}

	local l = t.add { type = "label", caption = "Dead biters: "}	
	l.style.font = "default-bold"			
	l.style.font_color = { r=0.98, g=0.66, b=0.22}
	l.style.minimal_width = 140
	
	local l = t.add { type = "label", caption = tostring(score.kills)}
	
	local l = frame.add { type = "label", caption = "--------------------------------------"}
	
	local t = frame.add { type = "table", column_count = 4}
	
	local l = t.add { type = "label", caption = "Player"}	
	l.style.font = "default-bold"			
	l.style.font_color = { r=0.98, g=0.66, b=0.22}
	l.style.minimal_width = 140
	local l = t.add { type = "label", caption = "Killscore"}	
	l.style.font = "default-bold"			
	l.style.font_color = { r=0.98, g=0.66, b=0.22}
	l.style.minimal_width = 140
	local l = t.add { type = "label", caption = "Deaths"}	
	l.style.font = "default-bold"			
	l.style.font_color = { r=0.98, g=0.66, b=0.22}
	l.style.minimal_width = 140
	local l = t.add { type = "label", caption = "Built structures"}	
	l.style.font = "default-bold"			
	l.style.font_color = { r=0.98, g=0.66, b=0.22}
	l.style.minimal_width = 140
	
	for _, player in pairs(game.connected_players) do
		if score.players[player.name] then
			local l = t.add { type = "label", caption = player.name}	
			l.style.font = "default"			
			l.style.font_color = { r=0.98, g=0.66, b=0.22}
			l.style.minimal_width = 140
			local str = "0"
			if score.players[player.name].killscore then str = tostring(score.players[player.name].killscore) end
			local l = t.add { type = "label", caption = str}	
			l.style.font = "default"			
			l.style.font_color = { r=0.98, g=0.66, b=0.22}
			l.style.minimal_width = 140
			local str = "0"
			if score.players[player.name].deaths then str = tostring(score.players[player.name].deaths) end
			local l = t.add { type = "label", caption = str}	
			l.style.font = "default"			
			l.style.font_color = { r=0.98, g=0.66, b=0.22}
			l.style.minimal_width = 140
			local str = "0"
			if score.players[player.name].built_entities then str = tostring(score.players[player.name].built_entities) end
			local l = t.add { type = "label", caption = str}	
			l.style.font = "default"			
			l.style.font_color = { r=0.98, g=0.66, b=0.22}
			l.style.minimal_width = 140
		end
	end
	
end

local function on_player_joined_game(event)
	local player = game.players[event.player_index]
	if not global.score then global.score = {} end
	if not global.score[player.force.name] then global.score[player.force.name] = {} end
	if not global.score[player.force.name].players then global.score[player.force.name].players = {} end	
	
	create_score_button(player)	
end

local function on_gui_click(event)
	if not event then return end
	if not event.element then return end
	if not event.element.valid then return end
	
	local player = game.players[event.element.player_index]
	local name = event.element.name	
	local frame = player.gui.left["score_panel"]	
	
	if (name == "score") and (frame == nil) then
				show_score(player)
	else
		if (name == "score") then
			frame.destroy()
		end
	end
		
end

local function on_rocket_launched(event)
	local force_name = event.rocket_silo.force.name
	if not global.score[force_name] then global.score[force_name] = {} end
	global.score[force_name].rocket_launches = global.score[force_name].rocket_launches + 1
	game.print ("A rocket has been launched!")	
	refresh_score()
end

local score_table = {
	["small-biter"] = 5,
	["medium-biter"] = 15,
	["big-biter"] = 30,
	["behemoth-biter"] = 100,
	["small-spitter"] = 5,
	["medium-spitter"] = 15,
	["big-spitter"] = 30,
	["behemoth-spitter"] = 100,
	["biter-spawner"] = 200
	["spitter-spawner"] = 200
	["small-worm-turret"] = 50
	["medium-worm-turret"] = 150
	["big-worm-turret"] = 300		
}

local function on_entity_died(event)	
	if not event.cause then return end
	if not event.cause.associated_player then return end
	local player = event.cause.associated_player
	if player.force.name == event.entity.force.name then return end
	if not global.score[event.cause.force.name] then global.score[event.cause.force.name] = {} end
	if not global.score[event.cause.force.name].players then global.score[event.cause.force.name].players = {} end
	
	if score_table[event.entity.name] then
		global.score[event.cause.force.name].kills = global.score[event.cause.force.name].kills + 1
		global.score[event.cause.force.name].players[player.name].killscore = global.score[event.cause.force.name].players[player.name].killscore + score_table[event.entity.name]
	end
	
	if event.entity.name == "player" then
		global.score[event.cause.force.name].players[player.name].deaths = global.score[event.cause.force.name].players[player.name].deaths + 1
	end

	refresh_score()
end

event.add(defines.events.on_entity_died, on_entity_died)
event.add(defines.events.on_gui_click, on_gui_click)
event.add(defines.events.on_player_joined_game, on_player_joined_game)
event.add(defines.events.on_rocket_launched, on_rocket_launched)