# -*- coding:utf-8 -*-
"""
DATE：2024/5/30 11:34

"""
from pot_libs.logger import log
from unify_api.modules.users.procedures.jwt_user import jwt_user
from unify_api.modules.common.dao.common_dao import load_user_lang
from unify_api.constants import (
    E_TYPE_MSG_LANG, PHASE_LINE_LANG, E_TYPE_NAME_LANG, LOG_IN_TIPS,
    PRODUCT_INFOS, PRODUCT_NAME
)


async def parse_user_lang(request):
    lang = "zh_CN"
    try:
        lang = request.args.get("lang")
    except Exception as e:
        user_id = jwt_user(request)
        lang = await load_user_lang(user_id)
    finally:
        return lang


def load_event_msg(e_type, event_param, lang):
    if not event_param:
        return ""

    msg_fmt = E_TYPE_MSG_LANG.get(e_type, {}).get(lang)
    if not msg_fmt:
        return ""

    event_msg = ""
    try:
        event_msg = msg_fmt.format(**event_param)
    except Exception as e:
        log.error(f"load_event_msg e,fmt:{msg_fmt}, param:{event_param}, {e}")
    finally:
        return event_msg


def load_phase_line(mtr_name, lang):
    lst = []
    try:
        lst = mtr_name.split("_")
    except Exception as e:
        log.error(f"load_phase_line error, mtr_name:{mtr_name}, {e}")
    finally:
        if len(lst) > 1:
            return PHASE_LINE_LANG.get(lst[-1], {}).get(lang)

        return ""


def load_e_type_name(e_type, lang="zh_CN"):
    return E_TYPE_NAME_LANG.get(e_type, {}).get(lang, "")


def load_login_tips(err_type, lang="zh_CN"):
    return LOG_IN_TIPS.get(err_type, {}).get(lang, "")


def load_product_info(prod_id, lang="zh_CN"):
    if prod_id not in PRODUCT_INFOS:
        return {}

    prod_info = PRODUCT_INFOS[prod_id]
    prod_name = PRODUCT_NAME.get(prod_id, {}).get(lang)
    if prod_name:
        prod_info["name"] = prod_name
    return prod_info
