from nonebot import on_command
from nonebot.adapters import MessageTemplate
from urllib import parse
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
"""


items = on_command("item", permission=GROUP | PRIVATE, priority=5, aliases={"道具"})


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


@items.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 items.finish()
        await items.reject("请输入道具ID，搜索请使用命令/搜索，取消请输入“取消”")
    item_info = await get_item_info(item_id)

    await items.finish(item_info)


async def get_item_info(_id):
    # MessageTemplate(MessageSegment.text())
    async with httpx.AsyncClient() as client:
        response = await client.get("{}/item/{}".format(config.kafeapi, _id))
        if not response.status_code == 200:
            return "请求失败！"
        else:
            try:
                _json = response.json()
            except Exception as e:
                return "请求失败！{}".format(e)
            item_name = _json['Name']
            url = "https://ff14.huijiwiki.com/wiki/{}".format(parse.quote("物品:{}".format(item_name)))
            return url

# def make_message():
#
