import re

from protobuf.douyin import *
from helper import LiveChatConfig, MessageType

live_chat_config = LiveChatConfig()


def parse_chat_msg(payload, queue):
    """聊天消息"""
    # if not random.random() < live_chat_config.chat_prob / 100:
    #     return
    message = ChatMessage().parse(payload)
    user_name = message.user.nick_name
    user_id = message.user.id
    content = message.content
    content = re.sub(r'\[.*?]', '', content)
    if not content.strip():
        return
    prompt = live_chat_config.chat_prompt.format(content=content, product_name='{product_name}', product_specification='{product_specification}', product_description='{product_description}')
    queue.put((MessageType.CHAT.value, prompt, content, user_name))
    # logger.info(f"【聊天msg】[{user_id}]{user_name}: {content}")
    # logger.info(f"队列数量: {queue.qsize()}")


def parse_gif_msg(payload, queue):
    """礼物消息"""
    # if not random.random() < live_chat_config.gift_prob / 100:
    #     return
    message = GiftMessage().parse(payload)
    user_name = message.user.nick_name
    gift_name = message.gift.name
    gift_count = message.combo_count
    prompt = live_chat_config.gift_prompt.format(user_name=user_name, gift_count=gift_count, gift_name=gift_name, product_name='{product_name}', product_specification='{product_specification}', product_description='{product_description}')
    queue.put((MessageType.GIFT.value, prompt, None))
    # logger.info(f"【礼物msg】{user_name} 送出了 {gift_name}x{gift_count}")
    # logger.info(f"队列数量: {queue.qsize()}")


def parse_like_msg(payload, queue):
    '''点赞消息'''
    # if not random.random() < live_chat_config.like_prob / 100:
    #     return
    message = LikeMessage().parse(payload)
    user_name = message.user.nick_name
    count = message.count
    prompt = live_chat_config.like_prompt.format(user_name=user_name, count=count, product_name='{product_name}', product_specification='{product_specification}', product_description='{product_description}')
    queue.put((MessageType.LIKE.value, prompt, None))
    # logger.info(f"【点赞msg】{user_name} 点了{count}个赞")
    # logger.info(f"队列数量: {queue.qsize()}")


def parse_member_msg(payload, queue):
    '''进入直播间消息'''
    # if not random.random() < live_chat_config.enter_live_room_prob / 100:
    #     return
    message = MemberMessage().parse(payload)
    user_name = message.user.nick_name
    user_id = message.user.id
    gender = message.user.gender
    if gender in (0, 1):
        gender = ["女", "男"][gender]
    prompt = live_chat_config.enter_live_room_prompt.format(user_name=user_name, product_name='{product_name}', product_specification='{product_specification}', product_description='{product_description}')
    queue.put((MessageType.ENTER_LIVE_ROOM.value, prompt, None))
    # logger.info(f"【进场msg】[{user_id}][{gender}]{user_name} 进入了直播间")
    # logger.info(f"队列数量: {queue.qsize()}")


def parse_social_msg(payload, queue):
    '''关注消息'''
    # if not random.random() < live_chat_config.follow_prob / 100:
    #     return
    message = SocialMessage().parse(payload)
    user_name = message.user.nick_name
    user_id = message.user.id
    prompt = live_chat_config.follow_prompt.format(user_name=user_name, product_name='{product_name}', product_specification='{product_specification}', product_description='{product_description}')
    queue.put((MessageType.FOLLOW.value, prompt, None))
    # logger.info(f"【关注msg】[{user_id}]{user_name} 关注了主播")
    # logger.info(f"队列数量: {queue.qsize()}")


def parse_room_user_seq_msg(payload):
    '''直播间统计'''
    message = RoomUserSeqMessage().parse(payload)
    current = message.total
    total = message.total_pv_for_anchor
    # print(f"【统计msg】当前观看人数: {current}, 累计观看人数: {total}")


def parse_fansclub_msg(payload):
    '''粉丝团消息'''
    message = FansclubMessage().parse(payload)
    content = message.content
    # print(f"【粉丝团msg】 {content}")


def parse_emoji_chat_msg(payload):
    '''聊天表情包消息'''
    message = EmojiChatMessage().parse(payload)
    emoji_id = message.emoji_id
    user = message.user
    common = message.common
    default_content = message.default_content
    # print(f"【聊天表情包id】 {emoji_id},user：{user},common:{common},default_content:{default_content}")


def parse_room_msg(payload):
    message = RoomMessage().parse(payload)
    common = message.common
    room_id = common.room_id
    # print(f"【直播间msg】直播间id:{room_id}")


def parse_room_stats_msg(payload):
    message = RoomStatsMessage().parse(payload)
    display_long = message.display_long
    # print(f"【直播间统计msg】{display_long}")


def parse_rank_msg(payload):
    message = RoomRankMessage().parse(payload)
    ranks_list = message.ranks_list
    # print(f"【直播间排行榜msg】{ranks_list}")


def parse_room_stream_adaptation_msg(payload):
    message = RoomStreamAdaptationMessage().parse(payload)
    adaptationType = message.adaptation_type
    # print(f'直播间adaptation: {adaptationType}')
