import time

import pythoncom
import win32com
import win32com.client
from win10toast import ToastNotifier

toaster = ToastNotifier()


def get_active_wps_text():
    pythoncom.CoInitialize()  # 初始化 COM
    try:
        wps = win32com.client.Dispatch("Kwps.Application")  # WPS Word
        doc = wps.ActiveDocument
        content = doc.Content.Text
        print("[WPS] 当前文档内容：", content[:100])  # 打印前100个字符
        return content
    except Exception as e:
        print("[!] 无法访问 WPS 文档：", e)
        return None


def check_for_changes():
    global current_text
    while True:
        time.sleep(2)  # 每2秒检查一次文档内容
        new_text = get_active_wps_text()
        if new_text and new_text != current_text:
            current_text = new_text
            update_recommendation(new_text)
            toaster.show_toast("新推荐内容", recommendation, duration=10)


def update_recommendation(content):
    global recommendation
    recommendation = f"基于你输入的内容，推荐：{content[:50]}..."
