require("libs.utils")
local win_msg_hook = nil
local hook = nil
local screen_width = dota.GetScreenWidth()
local screen_height = dota.GetScreenHeight()
local font = dota.DrawUtils.GetInstance():CreateFont("", 12, 500, 0, 0, bit.bor(EFontFlags.FONTFLAG_DROPSHADOW, EFontFlags.FONTFLAG_OUTLINE))

function OnWndProc(uMsg, wParam, lParam)
	if uMsg == WM_KEYDOWN then
		if wParam == VK_F6 then
            --log.Warning(string.format("0x%X\n",log.GetAddress(dota.GlobalInstanceManager:GetEngineClient())))
            --lua_execute("esp.lua")
            --sourcesdk.NetVarManager:DumpNetVar()
            --log.Warning(string.format("LocalPlayer:0x%X\n",log.GetAddress(dota.GetLocalPlayer())))
        elseif wParam == VK_F7 then
            --lua_unload("lasthit_tips.lua")
            --lua_unload("esp.lua")
		end
	end
end

function OnPaint()
    local localHero = dota.GetLocalHero()
    if (localHero == nil) then return end
    local base_entity = nil
    local i = 0
    local n = dota.GetHighestEntityIndex()
    for i = 0, n do
        repeat
            local base_entity = dota.GetClientEntity(i)
            if (base_entity == nil) then break end
            local class_id = base_entity:GetClientClass():GetClassID()
            local class_name = base_entity:GetClientClass():GetName()

            if (class_id ~= ClassIDs.CDOTA_BaseNPC_Tower and class_id ~= ClassIDs.CDOTA_BaseNPC_Fort and class_id ~= ClassIDs.CDOTA_BaseNPC_Barracks and class_id ~= ClassIDs.CDOTA_BaseNPC_Building) then
                if (base_entity:IsVisible() == false) then break end    -- 单位不可视
            end
            if (class_id < ClassIDs.CDOTA_BaseNPC or class_id > ClassIDs.CDOTA_BaseNPC_Warlock_Golem) then break end    -- 过滤CDOTA_BaseNPC

            local healthbar_offset = base_entity:GetHealthbarOffset();
            if (healthbar_offset == -1) then break end
            local vecScreen = dota.WorldToScreen(base_entity:GetAbsOrigin(),dota.Vector(0, 0, healthbar_offset))
            if (vecScreen.x <= 0 or vecScreen.x >= screen_width or vecScreen.y <= 0 or vecScreen.y >= screen_height) then break end
            
            local base_npc = dota.toBaseNPC(base_entity)
            local health    = base_npc:GetHealth()
            local health_max = base_npc:GetMaxHealth()
            
            if (health <= 0) then break end
          
            local height_fix = 22
            if (class_id == ClassIDs.CDOTA_BaseNPC_Tower) then
                height_fix = 70;
            elseif (class_id == ClassIDs.CDOTA_BaseNPC_Fort) then
                height_fix = 70;
            elseif (class_id == ClassIDs.CDOTA_BaseNPC_Barracks) then
                height_fix = 70;
            end
            
            dota.DrawString( font, vecScreen.x, vecScreen.y - height_fix, 255,255,0,255, true, class_id)    -- 一次攻击
            
            break   -- 跳出repeat死循环
        until true
    end -- for循环结束
end

function Initialize()
    win_msg_hook = dota.AddWinMsgHook(OnWndProc)
    --hook = dota.AddPaintHook(OnPaint)
    log.Warning("[lua] test.lua loaded\n")
end
