import os

# 大模型语义映射表 (Creality/Bambu -> PrusaSlicer)
SEMANTIC_MAP = {
    "outer_wall_line_width": "external_perimeter_extrusion_width",
    "outer_wall_speed": "external_perimeter_speed",
    "line_width": "extrusion_width",
    "infill_direction": "fill_angle",
    "sparse_infill_density": "fill_density",
    "sparse_infill_pattern": "fill_pattern",
    "initial_layer_line_width": "first_layer_extrusion_width",
    "initial_layer_print_height": "first_layer_height",
    "initial_layer_speed": "first_layer_speed",
    "gap_infill_speed": "gap_fill_speed",
    "infill_wall_overlap": "infill_overlap",
    "sparse_infill_speed": "infill_speed",
    "initial_layer_acceleration": "first_layer_acceleration",
    "travel_speed": "travel_speed",
    "bottom_shell_layers": "bottom_solid_layers",
    "top_shell_layers": "top_solid_layers",
    "top_surface_speed": "top_solid_infill_speed",
    "layer_height": "layer_height",
    "wall_loops": "perimeters",
    "inner_wall_speed": "perimeter_speed",
    "raft_layers": "raft_layers",
    "brim_width": "brim_width",
    "print_sequence": "complete_objects",
    "elefant_foot_compensation": "elefant_foot_compensation",
    "nozzle_temperature": "temperature",
    "first_layer_bed_temperature": "first_layer_bed_temperature",
    "bed_temperature": "bed_temperature",
    "filament_diameter": "filament_diameter",
    "support_material": "support_material",
    "support_material_style": "support_material_style",
    "retract_length": "retract_length",
    "retract_speed": "retract_speed",
    "z_hop": "retract_lift"
}

# 从 prusa_new_cli.txt (或 Prusa 官方默认配置) 中允许通过的原生参数白名单
NATIVE_ALLOWED = {
    "bridge_speed", "bridge_flow", "default_acceleration", "brim_object_gap",
    "ironing_type", "filament_cost", "filament_density", "filament_type",
    "gcode_flavor", "nozzle_diameter", "start_gcode", "end_gcode",
    "before_layer_gcode", "printer_model", "z_offset"
}

def process_file(filepath):
    with open(filepath, 'r') as f:
        lines = f.readlines()
    
    new_lines = []
    changed = False
    
    for line in lines:
        stripped = line.strip()
        if not stripped or stripped.startswith('[') or stripped.startswith(';'):
            new_lines.append(line)
            continue
            
        if '=' in line:
            parts = line.split('=', 1)
            key = parts[0].strip()
            val = parts[1].strip()
            
            # 处理特殊语义转换值
            if key == "print_sequence" and val == "by layer":
                val = "0"
            elif key == "print_sequence" and val == "by object":
                val = "1"
            
            if key in SEMANTIC_MAP:
                new_key = SEMANTIC_MAP[key]
                new_lines.append(f"{new_key} = {val}\n")
                changed = True
            elif key in NATIVE_ALLOWED:
                new_lines.append(f"{key} = {val}\n")
            else:
                new_lines.append(f";;;{line}")
                changed = True
        else:
            new_lines.append(line)
            
    if changed:
        with open(filepath, 'w') as f:
            f.writelines(new_lines)
        print(f"Applied semantic map to {filepath}")

for root, dirs, files in os.walk('print_config/prusa_slicer'):
    for file in files:
        if file.endswith('.ini'):
            process_file(os.path.join(root, file))

