# python3
# -*- coding: utf-8 -*-
# @Time    : 2022/12/19 0:23
# @Author  : yzyyz
# @Email   :  youzyyz1384@qq.com
# @File    : particular_e_notice.py
# @Software: PyCharm
import asyncio
from datetime import datetime

from nonebot.adapters.onebot.v11 import (
    Bot, Event, PokeNotifyEvent, HonorNotifyEvent, GroupUploadNoticeEvent, GroupDecreaseNoticeEvent,
    GroupIncreaseNoticeEvent, GroupAdminNoticeEvent, LuckyKingNotifyEvent, MessageSegment
)
from nonebot.matcher import Matcher
from nonebot.plugin import on_notice
from nonebot.typing import T_State

from .utils import fi

# è�·å�–æˆ³ä¸€æˆ³çŠ¶æ€�
async def _is_poke(bot: Bot, event: Event, state: T_State) -> bool:
    return isinstance(event, PokeNotifyEvent) and event.is_tome()


# è�·å�–ç¾¤è�£èª‰å�˜æ›´
async def _is_honor(bot: Bot, event: Event, state: T_State) -> bool:
    return isinstance(event, HonorNotifyEvent)


# è�·å�–æ–‡ä»¶ä¸Šä¼ 
async def _is_checker(bot: Bot, event: Event, state: T_State) -> bool:
    return isinstance(event, GroupUploadNoticeEvent)


# ç¾¤æˆ�å‘˜å‡�å°‘
async def _is_user_decrease(bot: Bot, event: Event, state: T_State) -> bool:
    return isinstance(event, GroupDecreaseNoticeEvent)


# ç¾¤æˆ�å‘˜å¢�åŠ 
async def _is_user_increase(bot: Bot, event: Event, state: T_State) -> bool:
    return isinstance(event, GroupIncreaseNoticeEvent)


# ç®¡ç�†å‘˜å�˜åŠ¨
async def _is_admin_change(bot: Bot, event: Event, state: T_State) -> bool:
    return isinstance(event, GroupAdminNoticeEvent)


# çº¢åŒ…è¿�æ°”ç�‹
async def _is_red_packet(bot: Bot, event: Event, state: T_State) -> bool:
    return isinstance(event, LuckyKingNotifyEvent)


poke = on_notice(_is_poke, priority=50)
honor = on_notice(_is_honor, priority=50)
upload_files = on_notice(_is_checker, priority=50)
user_decrease = on_notice(_is_user_decrease, priority=50)
user_increase = on_notice(_is_user_increase, priority=50)
admin_change = on_notice(_is_admin_change, priority=50)
red_packet = on_notice(_is_red_packet, priority=50)


@poke.handle()
async def _(bot: Bot, event: Event, state: T_State):
    # TODO åœ¨æœ¬åœ°å�šè®°å½• ä¸�å¤ªæƒ³å†™æ–‡æœ¬ï¼Œå› ä¸ºdevåˆ†æ”¯å·²ç»�ç”¨äº†æ•°æ�®åº“ï¼Œå��å†�åœ¨å†™
    ...


@honor.handle()
async def _(bot: Bot, event: HonorNotifyEvent, state: T_State, matcher: Matcher):
    honor_type = event.honor_type
    uid = event.user_id
    reply = ""
    honor_map = {"performer": ["ğŸ”¥", "ç¾¤è�Šä¹‹ç�«"], "emotion": ["ğŸ¤£", "å¿«ä¹�æº�æ³‰"]}
    # é¾™ç�‹
    u_info = await bot.get_group_member_info(user_id=event.user_id)
    u_name = u_info["card"] if u_info["card"] else u_info["nickname"]
    if honor_type == "talkative":
        if uid == bot.self_id:
            reply = "ğŸ’¦ æ–°é¾™ç�‹è¯�ç”Ÿï¼Œå�Ÿæ�¥æ˜¯æˆ‘è‡ªå·±~"
        else:
            reply = f"ğŸ’¦ æ�­å–œ {u_name} è�£è�·é¾™ç�‹æ ‡è¯†~"
    for key, value in honor_map.items():
        if honor_type == key:
            reply = f"{value[0]} æ�­å–œ{u_name}è�£è�·ã€�{value[1]}ã€‘æ ‡è¯†~"
    await fi(matcher, reply)


@upload_files.handle()
async def _(bot: Bot, event: GroupUploadNoticeEvent, state: T_State, matcher: Matcher):
    # TODO åœ¨æœ¬åœ°å�šè®°å½•
    ...


@user_decrease.handle()
async def _(bot: Bot, event: GroupDecreaseNoticeEvent, state: T_State, matcher: Matcher):
    op = await bot.get_group_member_info(group_id=event.group_id, user_id=event.operator_id)
    casualty_name = (await bot.get_stranger_info(user_id=event.user_id)).get("nickname")
    op_name = op['card'] if op.get('card') else op['nickname']
    e_time = datetime.fromtimestamp(event.time).strftime("%Y-%m-%d %H:%M:%S")
    avatar = get_avatar(event.user_id)
    farewell_words = "æ„Ÿè°¢/oç»™/né€�ä¸Šçš„é£�æœºï¼Œè°¢è°¢/o"
    farewell_self_words = "/nç¦»ç¾¤å‡ºèµ°/n"
    # TODO ä¸ºä»¥å��è‡ªå®šä¹‰æ¬¢é€�è¯�å�šå‡†å¤‡
    if event.operator_id != event.user_id:
        reply = f"ğŸ›« æˆ�å‘˜å�˜åŠ¨\n {farewell_words.replace('/o', f' {op_name} ').replace('/n', f' {casualty_name} ')}"
        reply += MessageSegment.image(avatar) + f" \n {e_time}\n"
    else:
        reply = f"ğŸ›« æˆ�å‘˜å�˜åŠ¨\n {farewell_self_words.replace('/n', f' {casualty_name} ')}"
    await fi(matcher, reply)


@user_increase.handle()
async def _(bot: Bot, event: GroupIncreaseNoticeEvent, state: T_State, matcher: Matcher):
    await asyncio.sleep(1)
    avatar = get_avatar(event.user_id)
    new_be = (await bot.get_group_member_info(group_id=event.group_id, user_id=event.user_id))['nickname']
    wel_words = "æ¬¢è¿�/nåŠ å…¥"
    # TODO ä¸ºä»¥å��è‡ªå®šä¹‰æ¬¢è¿�è¯�å�šå‡†å¤‡
    reply = "âœ¨ æˆ�å‘˜å�˜åŠ¨\n"+MessageSegment.image(avatar) + MessageSegment.at(event.user_id) + f"\n {wel_words.replace('/n', f' {new_be} ')}\n "
    await fi(matcher, reply)


@admin_change.handle()
async def _(bot: Bot, event: GroupAdminNoticeEvent, state: T_State, matcher: Matcher):
    reply = ""
    sub_type = event.sub_type
    uid = event.user_id
    user = await bot.get_group_member_info(group_id=event.group_id, user_id=uid)
    u_name = user['card'] if user.get('card') else user['nickname']
    cong_words = "æ�­å–œ/næˆ�ä¸ºç®¡ç�†"
    re_words = "Ops! /nä¸�å†�æ˜¯æœ¬ç¾¤ç®¡ç�†"
    if uid == bot.self_id:
        if sub_type == "set":
            reply = f"ğŸš” ç®¡ç�†å‘˜å�˜åŠ¨\n{cong_words.replace('/n', 'æˆ‘')}"
        if sub_type == "unset":
            reply = f"ğŸš” ç®¡ç�†å‘˜å�˜åŠ¨\n{re_words.replace('/n', 'æˆ‘')}"
    else:
        if sub_type == "set":
            reply = f"ğŸš” ç®¡ç�†å‘˜å�˜åŠ¨\n{cong_words.replace('/n', f' {u_name} ')}"
        if sub_type == "unset":
            reply = f"ğŸš”  ç®¡ç�†å‘˜å�˜åŠ¨\n{re_words.replace('/n', f' {u_name} ')}"
    await fi(matcher, reply)


@red_packet.handle()
async def _(bot: Bot, event: LuckyKingNotifyEvent, state: T_State, matcher: Matcher):
    # TODO ä¹Ÿè®¸å�šç‚¹æœ¬è®°å½•ï¼ˆè¿�æ°”ç�‹ï¼‰
    ...


def get_avatar(uid):
    return f"https://q4.qlogo.cn/headimg_dl?dst_uin={uid}&spec=640"
