from nonebot import on_command, logger
from nonebot.adapters.cqhttp import Message
from io import BytesIO
from nonebot.typing import T_State
from nonebot.adapters.cqhttp.permission import GROUP, PRIVATE
from nonebot.adapters.cqhttp.event import Event
from nonebot.adapters.cqhttp.bot import Bot
from nonebot.adapters.cqhttp.message import MessageSegment
import httpx
from ....nonebot_plugin_ff14 import config

PLUGIN_COMMAND = "/称号"

PLUGIN_INFO = "查询称号"

HELP_INFO = """/称号 ID
"""


title = on_command("title", permission=GROUP | PRIVATE, priority=5, aliases={"称号"})


@title.handle()
async def _(bot: Bot, event: Event, state: T_State):
    args = str(event.get_message()).strip()
    if args:
        state["id"] = args


@title.got("id", prompt="想要查询的道具ID是多少？")
async def _get(bot: Bot, event: Event, state: T_State):
    item_id: str = state['id']
    if not item_id.isdecimal():
        if item_id == '取消':
            await title.finish()
        await title.reject("请输入称号ID，搜索请使用命令/搜索，取消请输入“取消”")
    item_info = await get_item_info(item_id)
    await title.finish(item_info)


async def get_item_info(_id):
    content_link_type = {'Achievement': '成就'}
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get("{}/title/{}".format(config.kafeapi, _id))
        except httpx.ConnectTimeout:
            return "请求超时，请检查服务器网络！"
        if not response.status_code == 200:
            return "查询失败，请检查ID是否正确！"
        else:
            try:
                _json = response.json()
            except Exception as e:
                return "请求失败！{}".format(e)
            for key in _json:
                print("{}: {}".format(key, _json[key]))
            name = _json['Name']
            content_link = _json['GameContentLinks']
            content_link_str = ""
            for _type in content_link:
                content_link_str += "{}: {}\n".format(content_link_type[_type], content_link[_type]['Title'])
            # description = _json['Description']
            version = _json['GamePatch']['Version']
            icon_url = _json['Icon']
            text = "名称：{}\n获取：\n{}版本：{}".format(name, content_link_str, version)
            try:
                res: httpx.Response = await client.get("{}{}".format(config.kafeapi, icon_url))
            except Exception as e:
                logger.error(e)
                message = Message.template(MessageSegment.text(text))
            else:
                message = Message.template(MessageSegment.image(BytesIO(res.content)) + MessageSegment.text(text))
            return message
