from nonebot.adapters.onebot.v11 import Bot, Message
from nonebot import logger


class Reply:
    def __init__(self):
        self.message: str = ''
        self.user_id: int = -1
        self.user_card: str = ''
        self.time: int = -1


async def extract_reply(event, bot: Bot) -> Reply:
    """
    提取回复内容
    """
    logger.info(event['group_id'])
    reply_data = Reply()
    if event['reply'] and event['reply']['message_id']:  # 待优化
        reply = event['reply']
        sender = reply['sender']
        mes: Message = reply['message']
        print(mes)
        msg = mes[0]['data']['text']
        if msg:
            reply_data.message = msg
            meminfo = await get_member_info(bot=bot, user_id=sender['user_id'], group_id=event['group_id'])
            reply_data.user_card = (meminfo["card"] or meminfo["nickname"])
            reply_data.time = reply['time']
            reply_data.user_id = reply['sender']['user_id']
        else:
            reply_data.message = -1
    else:
        mes: Message = event["message"]
        print(mes)
        msg = mes[0]['data']['text'].replace("maq","")
        if msg:
            reply_data.message = msg
            meminfo = await get_member_info(bot=bot, user_id=event['user_id'], group_id=event['group_id'])
            reply_data.user_card = (meminfo["card"] or meminfo["nickname"])
            reply_data.time = event['time']
            reply_data.user_id = event['user_id']
        else:
            reply_data.message = -1
    return reply_data


async def get_member_info(bot: Bot, group_id: int, user_id: int):
    member_info = await bot.get_group_member_info(group_id=group_id, user_id=user_id)
    return member_info
