import json

import requests

from .get_bf6_data import *
from .tracker_data import *


async def get_data_bf3(player_id, user_id, platform):
    url = f"https://api.gametools.network/bf3/all/?format_values=true&playerid={player_id}&oid={user_id}&platform={platform}&skip_battlelog=false&lang=en-us"

    payload = {}
    headers = {
        'accept': 'application/json'
    }
    logger.info(f"请求URL：{url}")
    response = requests.request("GET", url, headers=headers, data=payload)
    data_json = json.loads(response.text)
    return data_json


async def get_data_bf4(player_id, user_id, platform):
    url = f"https://api.gametools.network/bf4/all/?format_values=true&playerid={player_id}&oid={user_id}&platform={platform}&skip_battlelog=false&lang=en-us"
    payload = {}
    headers = {
        'accept': 'application/json'
    }
    logger.info(f"请求URL：{url}")
    response = requests.request("GET", url, headers=headers, data=payload)
    data_json = json.loads(response.text)
    return data_json


async def get_data_bf1(player_id, user_id, platform):
    url = f"https://api.gametools.network/bf1/all/?format_values=true&playerid={player_id}&oid={user_id}&platform={platform}&skip_battlelog=false&lang=en-us"
    payload = {}
    headers = {
        'accept': 'application/json'
    }
    logger.info(f"请求URL：{url}")
    response = requests.request("GET", url, headers=headers, data=payload)
    data_json = json.loads(response.text)
    return data_json


async def get_data_bfv(player_id, user_id, platform):
    url = f"https://api.gametools.network/bfv/all/?format_values=true&playerid={player_id}&oid={user_id}&platform={platform}&skip_battlelog=false&lang=en-us"

    payload = {}
    headers = {
        'accept': 'application/json'
    }
    logger.info(f"请求URL：{url}")
    response = requests.request("GET", url, headers=headers, data=payload)
    data_json = json.loads(response.text)
    return data_json


async def get_data_bf6(player_id, user_id, platform):
    url = f"https://api.gametools.network/bf6/all/?format_values=true&playerid={player_id}&oid={user_id}&platform={platform}&skip_battlelog=false&lang=en-us"

    payload = {}
    headers = {
        'accept': 'application/json'
    }
    logger.info(f"请求URL：{url}")
    response = requests.request("GET", url, headers=headers, data=payload)
    data_json = json.loads(response.text)
    return data_json


async def get_player_info_by_name(user_name, platform):
    user_info_url = f"https://api.gametools.network/bfglobal/player/?name={user_name}&platform={platform}&skip_battlelog=false"

    payload = {}
    headers = {
        'accept': 'application/json'
    }
    logger.info(f"查询用户请求url:{user_info_url}")
    response = requests.request("GET", user_info_url, headers=headers, data=payload)

    user_info_json = json.loads(response.text)
    return user_info_json


async def get_player_info_by_ea_id(player_id, user_id, platform):
    url = f"https://api.gametools.network/bfglobal/player/?playerid={player_id}&oid={user_id}&platform={platform}&skip_battlelog=false"

    payload = {}
    headers = {
        'accept': 'application/json'
    }

    response = requests.request("GET", url, headers=headers, data=payload)

    user_info_json = json.loads(response.text)
    return user_info_json


async def get_bf6_rank(player):
    url = f"https://api.tracker.gg/api/v2/bf6/standard/search?platform=origin&query={player}&autocomplete=true"
    result = await fetch_tracker_search(url, exported_cookie_path)
    # 无结果再使用steam搜索
    if "errors" in result:
        return 0
    user_info = {}
    if "status" in result or "errors" in result:
        return 0
    for res in result['data']:
        if res['status'] is not None and res['platformSlug'] == 'origin':
            name = res['platformUserHandle']
            uid = res['titleUserId']
            status = res['status'].strip().split('•', 1)[0].replace("Rank", "")
            user = {
                'name': name,
                'rank': status,
                'uid': uid,
            }
            user_info = user

    logger.info(f"单用户{user_info}")
    logger.info(f"查询结果: {json.dumps(user_info, ensure_ascii=False, indent=2)}")
    if 'rank' not in user_info:
        return 0
    return user_info['rank']
