import json
import math
import random
import execjs
import os
from pathlib import Path
from .cookie_util import trans_cookies

# 获取当前文件的目录
current_dir = Path(__file__).parent
static_dir = current_dir.parent / 'static'

# 尝试多个可能的路径
js_paths = [
    static_dir / 'xhs_xs_xsc_56.js',
    current_dir.parent.parent.parent / 'core' / 'xhs_spider' / 'static' / 'xhs_xs_xsc_56.js',
    Path('core/xhs_spider/static/xhs_xs_xsc_56.js'),
    Path('static/xhs_xs_xsc_56.js'),
    Path('../static/xhs_xs_xsc_56.js')
]

js = None
for js_path in js_paths:
    try:
        if js_path.exists():
            js = execjs.compile(js_path.read_text(encoding='utf-8'))
            break
    except Exception as e:
        print(f"Error loading {js_path}: {e}")
        continue

if js is None:
    raise FileNotFoundError("无法找到 xhs_xs_xsc_56.js 文件")

# 对xray.js做同样的处理
xray_paths = [
    static_dir / 'xhs_xray.js',
    current_dir.parent.parent.parent / 'core' / 'xhs_spider' / 'static' / 'xhs_xray.js',
    Path('core/xhs_spider/static/xhs_xray.js'),
    Path('static/xhs_xray.js'),
    Path('../static/xhs_xray.js')
]

xray_js = None
for xray_path in xray_paths:
    try:
        if xray_path.exists():
            xray_js = execjs.compile(xray_path.read_text(encoding='utf-8'))
            break
    except Exception as e:
        print(f"Error loading {xray_path}: {e}")
        continue

if xray_js is None:
    raise FileNotFoundError("无法找到 xhs_xray.js 文件")

def generate_x_b3_traceid(len=16):
    x_b3_traceid = ""
    for t in range(len):
        x_b3_traceid += "abcdef0123456789"[math.floor(16 * random.random())]
    return x_b3_traceid

def generate_xs_xs_common(a1, api, data=''):
    if js is None:
        raise RuntimeError("JavaScript引擎未初始化")
    ret = js.call('get_request_headers_params', api, data, a1)
    xs, xt, xs_common = ret['xs'], ret['xt'], ret['xs_common']
    return xs, xt, xs_common

def generate_xs(a1, api, data=''):
    if js is None:
        raise RuntimeError("JavaScript引擎未初始化")
    ret = js.call('get_xs', api, data, a1)
    xs, xt = ret['X-s'], ret['X-t']
    return xs, xt

def generate_xray_traceid():
    if xray_js is None:
        raise RuntimeError("XRay JavaScript引擎未初始化")
    return xray_js.call('traceId')
def get_common_headers():
    return {
        "authority": "www.xiaohongshu.com",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "referer": "https://www.xiaohongshu.com/",
        "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "same-origin",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
    }
def get_request_headers_template():
    return {
        "authority": "edith.xiaohongshu.com",
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "cache-control": "no-cache",
        "content-type": "application/json;charset=UTF-8",
        "origin": "https://www.xiaohongshu.com",
        "pragma": "no-cache",
        "referer": "https://www.xiaohongshu.com/",
        "sec-ch-ua": "\"Not A(Brand\";v=\"99\", \"Microsoft Edge\";v=\"121\", \"Chromium\";v=\"121\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0",
        "x-b3-traceid": "",
        "x-mns": "unload",
        "x-s": "",
        "x-s-common": "",
        "x-t": "",
        "x-xray-traceid": generate_xray_traceid()
    }

def generate_headers(a1, api, data=''):
    xs, xt, xs_common = generate_xs_xs_common(a1, api, data)
    x_b3_traceid = generate_x_b3_traceid()
    headers = get_request_headers_template()
    headers['x-s'] = xs
    headers['x-t'] = str(xt)
    headers['x-s-common'] = xs_common
    headers['x-b3-traceid'] = x_b3_traceid
    if data:
        data = json.dumps(data, separators=(',', ':'), ensure_ascii=False)
    return headers, data

def generate_request_params(cookies_str, api, data=''):
    cookies = trans_cookies(cookies_str)
    a1 = cookies['a1']
    headers, data = generate_headers(a1, api, data)
    return headers, cookies, data

def splice_str(api, params):
    url = api + '?'
    for key, value in params.items():
        if value is None:
            value = ''
        url += key + '=' + value + '&'
    return url[:-1]

