import threading
import re

from graia.application import GraiaMiraiApplication
from graia.application.exceptions import AccountMuted
from graia.saya import Saya, Channel
from graia.saya.builtins.broadcast.schema import ListenerSchema
from graia.application.event.messages import *
from graia.application.event.mirai import *


# æ�’ä»¶ä¿¡æ�¯
__name__ = "Repeater"
__description__ = "å¤�è¯»ğŸ�“ï¼ˆx"
__author__ = "SAGIRI-kawaii"
__usage__ = "ä¸¤ä¸ªç›¸å�Œmessageå�³å�¯è§¦å�‘å¤�è¯»"

saya = Saya.current()
channel = Channel.current()
group_repeat = {}
lock = threading.Lock()

channel.name(__name__)
channel.description(f"{__description__}\nä½¿ç”¨æ–¹æ³•ï¼š{__usage__}")
channel.author(__author__)


@channel.use(ListenerSchema(listening_events=[GroupMessage]))
async def repeater(app: GraiaMiraiApplication, message: MessageChain, group: Group):
    group_id = group.id
    message_serialization = message.asSerializationString()
    message_serialization = message_serialization.replace(
        "[mirai:source:" + re.findall(r'\[mirai:source:(.*?)]', message_serialization, re.S)[0] + "]",
        ""
    )

    # lock.acquire()
    if group_id in group_repeat.keys():
        group_repeat[group.id]["lastMsg"] = group_repeat[group.id]["thisMsg"]
        group_repeat[group.id]["thisMsg"] = message_serialization
        if group_repeat[group.id]["lastMsg"] != group_repeat[group.id]["thisMsg"]:
            group_repeat[group.id]["stopMsg"] = ""
        else:
            if group_repeat[group.id]["thisMsg"] != group_repeat[group.id]["stopMsg"]:
                group_repeat[group.id]["stopMsg"] = group_repeat[group.id]["thisMsg"]
                try:
                    await app.sendGroupMessage(group, message.asSendable())
                except AccountMuted:
                    pass
    else:
        group_repeat[group_id] = {"lastMsg": "", "thisMsg": "", "stopMsg": ""}
    # lock.release()
