import os
import json
import shutil

# 加载标准球队名称
with open('team_name_standardization.json', 'r', encoding='utf-8') as f:
    data = json.load(f)
    
teams = data['teams']
base_dir = r'C:\Users\jerry\PyCharmProjects\footviz\footviz-vue'

# 定义需要处理的路径
directories_to_process = [
    os.path.join(base_dir, 'public', 'team_player_images'),
    os.path.join(base_dir, 'public', 'team-visuals'),
    os.path.join(base_dir, 'public', 'team_images'),
    os.path.join(base_dir, 'public', 'static', 'team_data_html'),
    os.path.join(base_dir, 'src', 'assets', 'team_data_html')
]

# 创建一个从别名到标准名称的映射
alias_to_standard = {}
for team in teams:
    standard_name = team['standard_name']
    alias_to_standard[standard_name] = standard_name  # 标准名称映射到自身
    for alias in team['aliases']:
        alias_to_standard[alias] = standard_name

print("标准名称映射:")
for alias, standard in alias_to_standard.items():
    print(f"  '{alias}' -> '{standard}'")

# 处理每个目录
for directory in directories_to_process:
    if not os.path.exists(directory):
        print(f"目录不存在: {directory}")
        continue
        
    print(f"\n处理目录: {directory}")
    
    # 获取目录中的所有项目
    items = os.listdir(directory)
    
    # 首先处理文件夹/文件的重命名
    for item in items:
        item_path = os.path.join(directory, item)
        
        # 处理目录
        if os.path.isdir(item_path):
            # 移除可能的后缀（如 "Vis"）
            base_name = item
            if base_name.endswith(' Vis'):
                base_name = base_name[:-4]
            elif base_name.endswith(' Vis/'):
                base_name = base_name[:-5]
                
            # 检查是否需要重命名
            if base_name in alias_to_standard:
                standard_name = alias_to_standard[base_name]
                if base_name != standard_name:
                    new_path = os.path.join(directory, standard_name)
                    if os.path.exists(new_path):
                        print(f"  警告: 目标路径已存在 {new_path}，跳过 {item_path}")
                    else:
                        os.rename(item_path, new_path)
                        print(f"  重命名目录: '{item}' -> '{standard_name}'")
                else:
                    print(f"  目录名称正确: '{item}'")
            else:
                print(f"  未找到映射关系: '{base_name}'")
                
        # 处理文件
        elif os.path.isfile(item_path):
            # 处理HTML文件
            if item.endswith('_goals_vs_assists.html'):
                # 提取球队名称部分
                base_name = item.replace('_goals_vs_assists.html', '')
                
                # 检查是否需要重命名
                if base_name in alias_to_standard:
                    standard_name = alias_to_standard[base_name]
                    if base_name != standard_name:
                        new_filename = f"{standard_name}_goals_vs_assists.html"
                        new_path = os.path.join(directory, new_filename)
                        if os.path.exists(new_path):
                            print(f"  警告: 目标文件已存在 {new_path}，跳过 {item_path}")
                        else:
                            os.rename(item_path, new_path)
                            print(f"  重命名文件: '{item}' -> '{new_filename}'")
                    else:
                        print(f"  文件名称正确: '{item}'")
                else:
                    print(f"  未找到映射关系: '{base_name}'")
            
            # 处理图片文件
            elif item.endswith('.png'):
                # 提取球队名称部分（移除.png扩展名）
                base_name = item.replace('.png', '')
                
                # 检查是否需要重命名
                if base_name in alias_to_standard:
                    standard_name = alias_to_standard[base_name]
                    if base_name != standard_name:
                        new_filename = f"{standard_name}.png"
                        new_path = os.path.join(directory, new_filename)
                        if os.path.exists(new_path):
                            print(f"  警告: 目标文件已存在 {new_path}，跳过 {item_path}")
                        else:
                            os.rename(item_path, new_path)
                            print(f"  重命名图片: '{item}' -> '{new_filename}'")
                    else:
                        print(f"  图片名称正确: '{item}'")
                else:
                    print(f"  未找到映射关系: '{base_name}'")

print("\n处理完成!")