from typing import Optional, Union
from nonebot.adapters import Event as BaseEvent
from nonebot.adapters.console.event import MessageEvent as ConsoleMessageEvent
from nonebot.adapters.discord.event import MessageEvent as DiscordMessageEvent
from nonebot_plugin_alconna import Alconna, Args, UniMessage, on_alconna

from konabot.plugins.roll_dice.roll_dice import generate_dice_image
from konabot.plugins.roll_dice.roll_number import get_random_number, get_random_number_string, roll_number

evt = on_alconna(Alconna(
    "摇数字"
), use_cmd_start=True, use_cmd_sep=False, skip_for_unmatch=True)

@evt.handle()
async def _(event: BaseEvent):
    if isinstance(event, DiscordMessageEvent):
        await evt.send(await UniMessage().text("```\n" + roll_number() + "\n```").export())
    elif isinstance(event, ConsoleMessageEvent):
        await evt.send(await UniMessage().text(roll_number()).export())
    else:
        await evt.send(await UniMessage().text(roll_number(wide=True)).export())

evt = on_alconna(Alconna(
    "摇骰子",
    Args["f1?", str]["f2?", str]
), use_cmd_start=True, use_cmd_sep=False, skip_for_unmatch=True)

@evt.handle()
async def _(event: BaseEvent, f1: Optional[str] = None, f2: Optional[str] = None):
    # if isinstance(event, DiscordMessageEvent):
    #     await evt.send(await UniMessage().text("```\n" + roll_dice() + "\n```").export())
    # elif isinstance(event, ConsoleMessageEvent):
    number = ""
    if(f1 is not None and f2 is not None):
        number = get_random_number_string(f1, f2)
    elif f1 is not None:
        if(float(f1) > 1):
            number = get_random_number_string("1", f1)
        elif (float(f1) > 0):
            number = get_random_number_string("0", f1)
        else:
            number = get_random_number_string(f1, "0")
    else:
        number = get_random_number_string()
    await evt.send(await UniMessage().image(raw=await generate_dice_image(number)).export())
    # else:
    #     await evt.send(await UniMessage().text(roll_dice(wide=True)).export())
