from nonebot import on_command, get_plugin_config,logger
from nonebot.rule import Rule, fullmatch
from .config import Config
import os
from nonebot_plugin_saa import Text, Image, MessageFactory
from nonebot.adapters.onebot.v11.event import GroupMessageEvent
import random
import asyncio

ALLOWED_GROUPS = [621016172]

async def _group_check(e: GroupMessageEvent) -> bool:
    """Check if message is from an allowed group."""
    return e.group_id in ALLOWED_GROUPS

_group_rule = Rule(_group_check)
plugin_config = get_plugin_config(Config)

help = on_command("帮助", rule=_group_rule & fullmatch(('帮助','help')), aliases={"help"}, priority=1, block=True)
@help.handle()
async def _handle_help():
    current_dir = os.path.dirname(__file__)
    image_path = os.path.join(current_dir, "img", "帮助菜单.jpg")
    try:
        with open(image_path, "rb") as f:
            image_bytes = f.read()
        await asyncio.sleep(random.uniform(2, 3))
        await MessageFactory([Image(image_bytes)]).send(at_sender=True, reply=True)
    except FileNotFoundError:
        logger.warning(f"[Help] 帮助菜单图片不存在: {image_path}")
        await help.finish("帮助菜单图片暂时不可用，请联系管理员")

downdload = on_command("下载", rule=_group_rule & fullmatch(('下载',"dl","downdload")), aliases={"dl","downdload"}, priority=1, block=True)
@downdload.handle()
async def _handle_download():
    await asyncio.sleep(random.uniform(2, 3))
    await downdload.finish(plugin_config.DowndLoadStr)

wd = on_command("帮助文档", rule=_group_rule & fullmatch(('帮助文档',"wd")), aliases={"wd"}, priority=1, block=True)
@wd.handle()
async def _handle_wd():
    await asyncio.sleep(random.uniform(2, 3))
    await wd.finish("https://www.danding.vip")


free = on_command("公益版", rule=_group_rule & fullmatch(('公益版',"free","gyb")), aliases={"free","gyb"}, priority=1, block=True)
@free.handle()
async def _handle_free():
    await asyncio.sleep(random.uniform(2, 3))
    await help.finish(plugin_config.FreeStr)

pro = on_command("正式版", rule=_group_rule & fullmatch(('正式版',"pro","zsb")), aliases={"pro","zsb"}, priority=1, block=True)
@pro.handle()
async def _handle_pro():
    await asyncio.sleep(random.uniform(2, 3))
    await help.finish(plugin_config.ProStr)

dyh = on_command("正式版御魂双开", rule=_group_rule & fullmatch(('正式版御魂双开',"dyh")), aliases={"dyh"}, priority=1, block=True)
@dyh.handle()
async def _handle_dyh():
    current_dir = os.path.dirname(__file__)
    image_path = os.path.join(current_dir, "img", "御魂双开方法.jpg")
    try:
        with open(image_path, "rb") as f:
            image_bytes = f.read()
        await asyncio.sleep(random.uniform(2, 3))
        await MessageFactory([Text("御魂双开方法见下图"), Image(image_bytes)]).send(at_sender=True, reply=True)
    except FileNotFoundError:
        logger.warning(f"[Help] 御魂双开图片不存在: {image_path}")
        await dyh.finish("教程图片暂时不可用，请联系管理员")
    

htr = on_command("正式版如何运行", rule=_group_rule & fullmatch(('正式版如何运行',"htr")), aliases={"htr"}, priority=1, block=True)
@htr.handle()
async def _handle_htr():
    current_dir = os.path.dirname(__file__)
    image_path = os.path.join(current_dir, "img", "开软件教程.jpg")
    try:
        with open(image_path, "rb") as f:
            image_bytes = f.read()
        await asyncio.sleep(random.uniform(2, 3))
        await MessageFactory([Text("How To Run? Look!"), Image(image_bytes)]).send(at_sender=True, reply=True)
    except FileNotFoundError:
        logger.warning(f"[Help] 运行教程图片不存在: {image_path}")
        await htr.finish("教程图片暂时不可用，请联系管理员")
    
order = on_command("下单", rule=_group_rule & fullmatch(('下单',"xd")), aliases={"xd"}, priority=1, block=True)
@order.handle()
async def _handle_order():
    await asyncio.sleep(random.uniform(2, 3))
    await order.finish(plugin_config.OrderStr)

daily_trial = on_command("每日试用", rule=_group_rule & fullmatch(('每日试用',"mrss")), aliases={"mrss"}, priority=1, block=True)
@daily_trial.handle()
async def _handle_daily_trial():
    await asyncio.sleep(random.uniform(2, 3))
    await daily_trial.finish(plugin_config.DailyTrialStr)
