import hashlib
from typing import Dict


class FingerprintGenerator:
    def __init__(self):
        # platform 只保留 windows
        self.platforms = ['windows']

        # brand 只保留 Chrome 和 Edge
        # Chrome 70%，Edge 30%
        self.brands = (
            ['Chrome'] * 70 +
            ['Edge'] * 30
        )

    def generate(self, username: str) -> Dict:
        """
        根据用户名生成固定的指纹配置
        """
        md5_hash = hashlib.md5(username.encode('utf-8')).hexdigest()

        seed = int(md5_hash[:8], 16) & 0x7FFFFFFF

        # platform 固定 windows
        platform = 'windows'

        # brand 根据 hash 固定生成
        brand_index = int(md5_hash[12:16], 16) % len(self.brands)
        brand = self.brands[brand_index]

        return {
            "username": username,
            "seed": seed,
            "platform": platform,
            "brand": brand
        }

    def get_cli_args(self, username: str) -> list:
        """
        直接生成用于启动浏览器的命令行参数列表
        """
        fp = self.generate(username)

        return [
            f"--fingerprint={fp['seed']}",
            f"--fingerprint-platform={fp['platform']}",
            f"--fingerprint-brand={fp['brand']}"
        ]


# ================= 测试代码 =================
if __name__ == "__main__":
    fp_tool = FingerprintGenerator()

    users = [
        "alice_2024",
        "bob_crypto",
        "jerry_crypto",
        "luna_crypto",
        "jone_crypto",
        "tom_crypto"
    ]

    for username in users:
        fp = fp_tool.generate(username)
        print(
            f"User: {username} -> "
            f"Seed: {fp['seed']}, "
            f"OS: {fp['platform']}, "
            f"Browser: {fp['brand']}"
        )

    # 模拟获取命令行参数
    args = fp_tool.get_cli_args("alice_2024")

    print("\n生成的命令行参数:")
    print(" ".join(args))