from typing import Optional
from nonebot_plugin_alconna import Alconna, Args, UniMessage, UniMsg, on_alconna
from konabot.common.web_render import WebRenderer
from nonebot.adapters import Event as BaseEvent
from playwright.async_api import Page

async def continue_handle(page: Page, content: str) -> None:
    # 这里可以添加一些预处理逻辑
    # 找到 id 为 input 的 textarea 元素
    textarea = await page.query_selector("#input")
    if textarea:
        # 在 textarea 中输入内容
        await textarea.fill(content)
    # 找到 id 为 submit-btn 的按钮元素
    submit_button = await page.query_selector("#submit-btn")
    if submit_button:
        # 点击按钮提交
        await submit_button.click()

evt = on_alconna(
    Alconna(
        f"生成喜报",
        Args["content?", str]
    ),
    use_cmd_start=True,
    use_cmd_sep=False,
    skip_for_unmatch=True,
)
@evt.handle()
async def _(msg: UniMsg, event: BaseEvent, content: Optional[str] = ""):

    screenshot = await WebRenderer.render(
        url="https://witnessbot.mxowl.com/services/congratulations/",
        target="#main-canvas",
        other_function=lambda page: continue_handle(page, content),
        timeout=30
    )
    await evt.send(
        await UniMessage().image(raw=screenshot).export()
    )

async def beibao_continue_handle(page: Page, content: str) -> None:
    # 这里可以添加一些预处理逻辑
    # 找到 id 为 input 的 textarea 元素
    textarea = await page.query_selector("#input")
    if textarea:
        # 在 textarea 中输入内容
        await textarea.fill(content)
    # 找到 class 为 btn btn-outline-primary，for属性为 mode-2 的标签元素
    mode_radio = await page.query_selector("label.btn.btn-outline-primary[for='mode-2']")
    if mode_radio:
        # 点击选择悲报模式
        await mode_radio.click()
    # 找到 id 为 submit-btn 的按钮元素
    submit_button = await page.query_selector("#submit-btn")
    if submit_button:
        # 点击按钮提交
        await submit_button.click()

evt = on_alconna(
    Alconna(
        f"生成悲报",
        Args["content?", str]
    ),
    use_cmd_start=True,
    use_cmd_sep=False,
    skip_for_unmatch=True,
)
@evt.handle()
async def _(msg: UniMsg, event: BaseEvent, content: Optional[str] = ""):

    screenshot = await WebRenderer.render(
        url="https://witnessbot.mxowl.com/services/congratulations/",
        target="#main-canvas",
        other_function=lambda page: beibao_continue_handle(page, content),
        timeout=30
    )
    await evt.send(
        await UniMessage().image(raw=screenshot).export()
    )

async def continue_handle_3(page: Page, arg1: str, arg2: str) -> None:
    # 这里可以添加一些预处理逻辑
    # 找到 id 为 textL 的 input，id 为 textR 的 input
    input1 = await page.query_selector("#textL")
    input2 = await page.query_selector("#textR")
    if input1:
        await input1.fill(arg1)
    if input2:
        await input2.fill(arg2)
    # 等待 0.3 秒钟
    await page.wait_for_timeout(300)
    # 等待 id 为 loading 的元素不可见
    loading = await page.query_selector("#loading")
    if loading:
        await loading.wait_for_element_state("hidden")

evt = on_alconna(
    Alconna(
        f"BA生成",
        Args["arg1", str],
        Args["arg2", str]
    ),
    use_cmd_start=True,
    use_cmd_sep=False,
    skip_for_unmatch=True,
)
@evt.handle()
async def _(msg: UniMsg, event: BaseEvent, arg1: str, arg2: str):
    screenshot = await WebRenderer.render(
        url="https://tmp.nulla.top/ba-logo/",
        target="#canvas",
        other_function=lambda page: continue_handle_3(page, arg1, arg2),
        timeout=30
    )
    await evt.send(
        await UniMessage().image(raw=screenshot).export()
    )