from ...utils import get_user, text_response, User, server_id_2_server_name, server_name_2_server_id, get_user_async
import tsugu_api
from arclet.alconna import Alconna, Option, Subcommand, Args, CommandMeta, Empty, Namespace, namespace, command_manager
import tsugu_api_async


difficulty_text_tuple = ('easy', 'ez', 'normal', 'nm', 'hard', 'hd', 'expert', 'ex', 'special', 'sp')
difficulty_text_2_difficulty_id = {
    "easy": 0,
    "ez": 0,
    "normal": 1,
    "nm": 1,
    "hard": 2,
    "hd": 2,
    "expert": 3,
    "ex": 3,
    "special": 4,
    "sp": 4,
}


alc = Alconna(
        ["查谱面", "查铺面"],
        Args["songId", int]['difficultyText', difficulty_text_tuple, 'ex'],
        meta=CommandMeta(
            compact=True,
            description="查谱面",
            usage='根据曲目ID与难度查询铺面信息。',
            example='''查谱面 1 :返回1号曲的ex难度谱面
查谱面 128 special :返回128号曲的special难度谱面'''
        )
    )
def handler(message: str, user_id: str, platform: str):
    res = alc.parse(message)

    if res.matched:
        user = get_user(user_id, platform)
        try:
            return tsugu_api.song_chart(user.displayed_server_list, res.songId, difficulty_text_2_difficulty_id[res.difficultyText])
        except Exception as e:
            return text_response(e)

    return res


async def handler_async(message: str, user_id: str, platform: str):
    res = alc.parse(message)

    if res.matched:
        user = await get_user_async(user_id, platform)
        try:
            return await tsugu_api_async.song_chart(user.displayed_server_list, res.songId, difficulty_text_2_difficulty_id[res.difficultyText])
        except Exception as e:
            return text_response(e)

    return res





