dofile("../defs/hud_info_defs.lua")

HudInfo = class("HudInfo")

local STATS = {
	range = {
		name = "RNG",
		color = COLORS.range,
	},
	damage = {
		name = "DMG",
		color = COLORS.damage,
	},
	rate = {
		name = "ROF",
		color = COLORS.rate,
	},
	muzzle = {
		name = "MUZ",
		color = COLORS.muzzle,
	},
	pierce = {
		name = "PEN",
		color = COLORS.pierce,
	},
	shots = {
		name = "SHT",
		color = COLORS.shots,
	},
	speed = {
		name = "SPE",
		color = COLORS.speed,
	},
	armor = {
		name = "DEF",
		color = COLORS.armor,
	},
	resistance  = {
		name = "RES",
		color = COLORS.resistance,
	},
	size = {
		name = "SIZ",
		color = COLORS.size, 
	},
	heal = {
		name = "HEL",
		color = COLORS.heal,
	},
}

local INFO_TYPES = {
	text = {
		new = function(self, def, value)
			assert(value)
			local w,h = video.getTextSpritesSize(value, self.font or def.font, def.scale)
			local info = {text = value, w = w, h = h}
			return info
		end,
		render = function(self, def, info, x, y, w, h, scale, color, a)
			local rx = x + (def.align-1)*w*0.5
			local color = info.color or def.color or color
			video.renderTextSprites(info.text, rx, y, def.align, self.font or def.font, a*def.alpha, color.r, color.g, color.b,nil,def.scale*scale)
		end,
	},
	stats = {
		new = function(self, def, value, comp)
			local lw,lh = video.getTextSpritesSize("999", self.font or def.font, def.scale)
			local w,h = video.getTextSpritesSize("999 999", self.font or def.font, def.scale)
			local tot = 0
			local copy = {}
			local min = 0 
			local max = 5
			local max_val = 1
			for stat, amt in pairs(value) do
				tot = tot + h
				local val = amt
				if comp then
					val = val-(comp[stat] or 0)
				end
				copy[stat] = val
				min = math.min(val, min)
				max = math.max(val, max)
				max_val = math.max(val, max_val)
			end
			local fake_max = max
			local total_range = fake_max-min
			local info = {stats = copy, value = value, w = w*3,label_w = lw, text_w = w, h = tot, row = h, range = total_range, min = min, max = fake_max, max_val = max_val}
			return info
		end,
		render = function(self, def, info, x, y, w, h, scale, color, a)
			local bar_w = (w-info.text_w)			
			local value_scale = bar_w/info.range

			local value_offset = math.abs(info.min)*value_scale
			local row = info.row
			local font = self.font or def.font
			local rx = x + value_offset - w*0.5
			for i=math.ceil(info.min), math.floor(info.max_val) do
				local xx = rx+i*value_scale
				local ry = y + info.h
				video.renderSpriteLine(xx, y, xx, ry, (i==0) and 32 or 16, 255,255,255,nil,nil,2)
			end
			for stat, amt in pairs(info.stats) do
				local amt_scaled = amt*value_scale
				y = y + row*0.2
				local sdef = STATS[stat]
				
				--if amt > 0 then
				local bar_col = COLORS.good
				local bx = rx
				if amt_scaled < 0 then
					bx = bx + amt_scaled
					amt_scaled = math.abs(amt_scaled)
					bar_col = COLORS.bad
				elseif amt_scaled == 0 then
					amt_scaled = 2
					bx = bx-1
					bar_col = COLORS.identical
				end
				video.renderSpriteRectangle(bx,y, amt_scaled, row*0.6, 255, bar_col.r, bar_col.g,bar_col.b)
				--else
				--	video.renderRectangle(rx,y, amt_scaled, row*0.6, 255, cr,cg,cb)
				--end
				local col = sdef.color
				local cr,cg,cb = col.r, col.g, col.b
				local nx = x+w*0.5
				local nscale = def.scale*scale
				local alp = a*def.alpha

				video.renderTextSprites(sdef.name, nx, y + row*0.3 + fonts.alignY(font, 1, def.scale), 2, font, a*def.alpha*0.5, cr,cg,cb,nil,nscale)
				video.renderTextSprites(info.value[stat], nx-5-info.label_w, y + row*0.3 + fonts.alignY(font, 1, def.scale), 2, font, a*def.alpha, cr,cg,cb,nil,nscale)
				
				y = y + row*0.8
			end			
		end,
	},
}
function HudInfo:new()
	local h = instance(self)
	return h
end

function HudInfo:set_key(key, v, comp)
	local def = HUD_INFO_DEFS[key]
	local idef = INFO_TYPES[def.info_type]
	local holder = idef.new(self, def, v, comp)
	self[key] = holder
	self:refresh_size()
	return holder
end

function HudInfo:set_color(color)
	self.hud_color = color
end

function HudInfo:refresh_size()
	local tot_w,tot_h = 0,0
	for index, def in ipairs(HUD_INFO_DEFS) do
		local info = self[def.key]
		if info then
			local w,h = info.w, info.h
			tot_w = math.max(tot_w, w)
			tot_h = tot_h + h
		end
	end
	self.hud_info_w = tot_w
	self.hud_info_h = tot_h
end

function HudInfo:get_dimensions()
	return self.hud_info_w, self.hud_info_h
end

function HudInfo:render(x, y, scale, perspective, angle, a, r, g, b)

	local margin = 5*scale
	local w,h = self.hud_info_w*scale, self.hud_info_h*scale

	video.renderRectangle(x-w*0.5-margin, y-h*0.5-margin, w+margin*2, h+margin*2, a*0.8, 0,0,0)

	local color = self.hud_color or COLORS.white
	local y = y -h*0.5	
	for index, def in ipairs(HUD_INFO_DEFS) do

		local info = self[def.key]
		if info then
			--print(def.key)
			local idef = INFO_TYPES[def.info_type]
			idef.render(self, def, info, x, y, w, h, scale, color, a)
			y = y + info.h*scale
		end
	end
end
			