import sys
sys.path.insert(0, ".")
import ctypes
import src.common.win32_window as w

w.attach_to_default_desktop()
u = ctypes.windll.user32
k = ctypes.windll.kernel32

poe = w.find_poe2_window()
cur = k.GetCurrentThreadId()
fg = u.GetForegroundWindow()
fg_t = u.GetWindowThreadProcessId(fg, None) if fg else 0
poe_t = u.GetWindowThreadProcessId(poe, None)

print(f"fg: {fg}, fg_t: {fg_t}, poe_t: {poe_t}")
att_fg = u.AttachThreadInput(cur, fg_t, True) if fg_t else 0
print(f"att_fg: {att_fg}")
att_poe = u.AttachThreadInput(cur, poe_t, True) if poe_t else 0
print(f"att_poe: {att_poe}")

# Alt down/up bypass
u.keybd_event(0x12, 0, 0, 0)
u.BringWindowToTop(poe)
sf = u.SetForegroundWindow(poe)
u.SetActiveWindow(poe)
u.SetFocus(poe)
u.keybd_event(0x12, 0, 2, 0)
print(f"SetForegroundWindow: {sf}")
print(f"new fg: {u.GetForegroundWindow()}")

if att_poe:
    u.AttachThreadInput(cur, poe_t, False)
if att_fg:
    u.AttachThreadInput(cur, fg_t, False)
