-- UI Radiation Mod
-- by sokol_jack (sokol_jack@mail.ru)
-- version 0.3
local posx = 0
local posy = 0
local width = 0
local height = 0
local p_dx = 0
local p_dy =0
local p_width = 0
local p_height = 0
local saved_aspect = 0
local saved_width = -1

function update(obj)
	if (obj == nil) then return end
	if (obj.radiation == nil) then return end
	local hud_rad = get_hud()
	local wnd_
	if (hud_rad) then
		local custom_static_rad_back = hud_rad:GetCustomStatic("hud_rad_back")
		local custom_static_rad = hud_rad:GetCustomStatic("hud_rad_level")
		if (device().aspect_ratio ~= saved_aspect) then
			local asp = device().aspect_ratio
			if (math.abs(asp - saved_aspect) > 0.01) then
				mylog("aspect_ratio changed. New is "..device().aspect_ratio..", old was "..saved_aspect..".")
				saved_aspect = device().aspect_ratio
				if(custom_static_rad_back) then hud_rad:RemoveCustomStatic("hud_rad_back") end
				if(custom_static_rad) then hud_rad:RemoveCustomStatic("hud_rad_level") end
				custom_static_rad_back = nil
				custom_static_rad = nil
			end
		end
		local bDetected = false
		if (obj:object("detector_simple") or obj:object("detector_advances") or obj:object("detector_elite")) then
			bDetected = true
		end
		if (bDetected == false) then
			if (custom_static_rad_back) then hud_rad:RemoveCustomStatic("hud_rad_back") end
			if (custom_static_rad) then hud_rad:RemoveCustomStatic("hud_rad_level") end
			return
		end
		if (custom_static_rad_back == nil or custom_static_rad == nil) then
			mylog("creating radiation on HUD")
			mylog("aspect_ratio = "..device().aspect_ratio)
			if (device().aspect_ratio < 0.75) then --widescreen. Use coords from maingame_16.xml		
				read_params(true)
			else --normal -- Use coords from maingame.xml
				read_params()		
			end		

			if custom_static_rad_back == nil then
				hud_rad:AddCustomStatic("hud_rad_back", true)
				custom_static_rad_back = hud_rad:GetCustomStatic("hud_rad_back")
				if (custom_static_rad_back and custom_static_rad_back.wnd) then
					 wnd_ = custom_static_rad_back:wnd()
					 if (wnd_) then
						wnd_:SetWndPos(posx, posy)
						wnd_:SetHeight(height)
						wnd_:SetWidth(width)
						mylog("Setting hud_rad_back. New coords: ["..posx..", "..posy..", w="..width..", h="..height.."]")
					end
				end
			end
			if custom_static_rad == nil then
				hud_rad:AddCustomStatic("hud_rad_level", true)
				custom_static_rad = hud_rad:GetCustomStatic("hud_rad_level")
				if (custom_static_rad and custom_static_rad.wnd) then
					wnd_ = custom_static_rad:wnd()
					if(wnd_) then
						wnd_:SetWndPos(posx + p_dx, posy + p_dy)
						wnd_:SetHeight(p_height)
						wnd_:SetWidth(p_width)
						mylog("Setting hud_rad_level. New coords: ["..posx + p_dx..", "..posy + p_dy..", w="..p_width..", h="..p_height.."]")
					end
				end
			end
		end
		local new_width = obj.radiation * p_width
		if (saved_width ~= new_width) then
			if obj.radiation > 0.0 then
				custom_static_rad:wnd():SetWidth(obj.radiation * p_width)
			else
				custom_static_rad:wnd():SetWidth(obj.radiation * p_width)
				--hud_rad:RemoveCustomStatic("hud_rad_level")
			end
		end
	end
end

function read_params(bWide)
	local section = ""
	local ui_rad_ini = ini_file("misc\\ui_rad.ltx")
	local id, value = "", "", result
	local n = 0	
	section = "ui_rad"
	if bWide == true then section = "ui_rad_wide" end
	if (ui_rad_ini and ui_rad_ini:section_exist(section)) then
		n = ui_rad_ini:line_count(section)
		for i=0, n-1 do
			result, id, value = ui_rad_ini:r_line(section, i, "", "")
			if (id == "x") then
				posx = value
			elseif(id == "y") then
				posy = value
			elseif(id == "width") then
				width = value
			elseif(id == "height") then
				height = value
			end
		end
	end
	section = "ui_rad_progress"
	if bWide == true then section = "ui_rad_progress_wide" end
	if (ui_rad_ini and ui_rad_ini:section_exist(section)) then
		n = ui_rad_ini:line_count(section)		
		for i=0, n-1 do
			result, id, value = ui_rad_ini:r_line(section, i, "", "")
			if (id == "dx") then
				p_dx = value
			elseif(id == "dy") then
				p_dy = value
			elseif(id == "width") then
				p_width = value
			elseif(id == "height") then
				p_height = value
			end
		end
	end	
end
function dbglog(fmt,...)
		local msg = string.format(fmt, ...)
		local msg_no_ws = string.gsub(msg, "%s", "_")
		get_console():execute("dbg:" .. msg_no_ws)
		get_console():execute("flush")		
end

function mylog(text)
	--dbglog("==> "..text)
end