# qwen use https://api.together.xyz
import time
from os import environ

from expiringdict import ExpiringDict
from telebot import TeleBot
from telebot.types import Message
from telegramify_markdown import markdownify
from together import Together

from ._utils import bot_reply_first, bot_reply_markdown, enrich_text_with_urls, logger


QWEN_API_KEY = environ.get("TOGETHER_API_KEY")
QWEN_MODEL = "Qwen/Qwen2-72B-Instruct"

if QWEN_API_KEY:
    client = Together(api_key=QWEN_API_KEY)

# Global history cache
qwen_player_dict = ExpiringDict(max_len=1000, max_age_seconds=600)
qwen_pro_player_dict = ExpiringDict(max_len=1000, max_age_seconds=600)


def qwen_handler(message: Message, bot: TeleBot) -> None:
    """qwen : /qwen <question>"""
    m = message.text.strip()

    player_message = []
    # restart will lose all TODO
    if str(message.from_user.id) not in qwen_player_dict:
        qwen_player_dict[str(message.from_user.id)] = (
            player_message  # for the imuutable list
        )
    else:
        player_message = qwen_player_dict[str(message.from_user.id)]
    if m.strip() == "clear":
        bot.reply_to(
            message,
            "just clear your qwen messages history",
        )
        player_message.clear()
        return
    if m[:4].lower() == "new ":
        m = m[4:].strip()
        player_message.clear()
    m = enrich_text_with_urls(m)

    who = "qwen"
    # show something, make it more responsible
    reply_id = bot_reply_first(message, who, bot)

    player_message.append({"role": "user", "content": m})
    # keep the last 5, every has two ask and answer.
    if len(player_message) > 10:
        player_message = player_message[2:]

    qwen_reply_text = ""
    try:
        r = client.chat.completions.create(
            messages=player_message, max_tokens=8192, model=QWEN_MODEL
        )
        content = r.choices[0].message.content.encode("utf8").decode()
        if not content:
            qwen_reply_text = f"{who} did not answer."
            player_message.pop()
        else:
            qwen_reply_text = content
            player_message.append(
                {
                    "role": "assistant",
                    "content": qwen_reply_text,
                }
            )

    except Exception:
        logger.exception("Qwen handler error")
        bot.reply_to(message, "answer wrong maybe up to the max token")
        # pop my user
        player_message.pop()
        return

    # reply back as Markdown and fallback to plain text if failed.
    bot_reply_markdown(reply_id, who, qwen_reply_text, bot)


def qwen_pro_handler(message: Message, bot: TeleBot) -> None:
    """qwen_pro : /qwen_pro <question>"""
    m = message.text.strip()

    player_message = []
    # restart will lose all TODO
    if str(message.from_user.id) not in qwen_pro_player_dict:
        qwen_pro_player_dict[str(message.from_user.id)] = (
            player_message  # for the imuutable list
        )
    else:
        player_message = qwen_pro_player_dict[str(message.from_user.id)]
    if m.strip() == "clear":
        bot.reply_to(
            message,
            "just clear your qwen messages history",
        )
        player_message.clear()
        return
    if m[:4].lower() == "new ":
        m = m[4:].strip()
        player_message.clear()
    m = enrich_text_with_urls(m)

    who = "qwen Pro"
    reply_id = bot_reply_first(message, who, bot)

    player_message.append({"role": "user", "content": m})
    # keep the last 5, every has two ask and answer.
    if len(player_message) > 10:
        player_message = player_message[2:]

    try:
        r = client.chat.completions.create(
            messages=player_message,
            max_tokens=8192,
            model=QWEN_MODEL,
            stream=True,
        )
        s = ""
        start = time.time()
        for chunk in r:
            if chunk.choices[0].delta.content is None:
                break
            s += chunk.choices[0].delta.content
            if time.time() - start > 1.7:
                start = time.time()
                bot_reply_markdown(reply_id, who, s, bot, split_text=False)

        if not bot_reply_markdown(reply_id, who, s, bot):
            # maybe not complete
            # maybe the same message
            player_message.clear()
            return

        player_message.append(
            {
                "role": "assistant",
                "content": markdownify(s),
            }
        )

    except Exception:
        logger.exception("Qwen Pro handler error")
        bot.reply_to(message, "answer wrong maybe up to the max token")
        player_message.clear()
        return


if QWEN_API_KEY:

    def register(bot: TeleBot) -> None:
        bot.register_message_handler(qwen_handler, commands=["qwen"], pass_bot=True)
        bot.register_message_handler(qwen_handler, regexp="^qwen:", pass_bot=True)
        bot.register_message_handler(
            qwen_pro_handler, commands=["qwen_pro"], pass_bot=True
        )
        bot.register_message_handler(
            qwen_pro_handler, regexp="^qwen_pro:", pass_bot=True
        )
