# ============================================================================
# CALIBRATION TEST PATTERNS
# Actual printable G-code patterns for calibration tests
# ============================================================================

# ----------------------------------------------------------------------------
# PRESSURE ADVANCE TEST PATTERN
# Prints squares with varying PA values
# ----------------------------------------------------------------------------
[gcode_macro PA_TEST_PATTERN]
description: Print Pressure Advance test pattern
gcode:
    {% set START_PA = params.START|default(0.01)|float %}
    {% set END_PA = params.END|default(0.05)|float %}
    {% set STEPS = params.STEPS|default(10)|int %}
    {% set TEMP = params.TEMP|default(205)|int %}
    
    # Heat up
    M104 S{TEMP}
    M190 S60
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={TEMP-5}
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM=55
    
    # Home
    G28
    G1 Z10 F600
    
    # Settings
    G90 ; Absolute positioning
    M83 ; Relative extrusion
    G92 E0
    
    {% set step_size = (END_PA - START_PA) / STEPS %}
    {% set square_size = 20 %}
    {% set spacing = 5 %}
    
    RESPOND MSG="Printing {STEPS} squares with PA from {START_PA} to {END_PA}"
    
    {% for step in range(STEPS) %}
        {% set current_pa = START_PA + (step * step_size) %}
        {% set x_pos = 50 + (step % 5) * (square_size + spacing) %}
        {% set y_pos = 50 + (step // 5) * (square_size + spacing) %}
        
        RESPOND MSG="Square {step + 1}/{STEPS}: PA={current_pa:.3f}"
        SET_PRESSURE_ADVANCE ADVANCE={current_pa}
        
        # Move to position
        G1 X{x_pos} Y{y_pos} Z0.2 F3000
        
        # Print square
        G1 X{x_pos + square_size} E10 F1800
        G1 Y{y_pos + square_size} E10 F1800
        G1 X{x_pos} E10 F1800
        G1 Y{y_pos} E10 F1800
        
        # Lift Z
        G1 Z5 F600
    {% endfor %}
    
    RESPOND MSG="PA Test Pattern complete!"
    RESPOND MSG="Examine squares and find best corner quality"

# ----------------------------------------------------------------------------
# FLOW RATE TEST CUBE
# Prints cube with varying flow rates
# ----------------------------------------------------------------------------
[gcode_macro FLOW_TEST_CUBE]
description: Print flow rate calibration cube
gcode:
    {% set START_FLOW = params.START|default(0.90)|float %}
    {% set END_FLOW = params.END|default(1.10)|float %}
    {% set STEPS = params.STEPS|default(5)|int %}
    {% set TEMP = params.TEMP|default(205)|int %}
    
    # Heat up
    M104 S{TEMP}
    M190 S60
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={TEMP-5}
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM=55
    
    # Home
    G28
    G1 Z10 F600
    
    # Settings
    G90 ; Absolute positioning
    M83 ; Relative extrusion
    G92 E0
    
    {% set step_size = (END_FLOW - START_FLOW) / STEPS %}
    {% set cube_height = 20 %}
    {% set wall_thickness = 0.4 %}
    
    RESPOND MSG="Printing flow cube with {STEPS} sections"
    RESPOND MSG="Flow range: {START_FLOW} to {END_FLOW}"
    
    {% for step in range(STEPS) %}
        {% set current_flow = START_FLOW + (step * step_size) %}
        {% set z_start = step * cube_height %}
        
        RESPOND MSG="Section {step + 1}/{STEPS}: Flow={current_flow:.2f}"
        
        # Set flow rate (via extrusion multiplier simulation)
        {% set flow_multiplier = current_flow %}
        
        # Print walls for this section
        {% for layer in range(4) %}
            {% set z_height = z_start + (layer * 0.2) %}
            G1 Z{z_height} F600
            
            # Wall 1
            G1 X50 Y50 F3000
            G1 X70 E{10 * flow_multiplier} F1800
            
            # Wall 2
            G1 Y70 E{10 * flow_multiplier} F1800
            
            # Wall 3
            G1 X50 E{10 * flow_multiplier} F1800
            
            # Wall 4
            G1 Y50 E{10 * flow_multiplier} F1800
        {% endfor %}
    {% endfor %}
    
    # Lift Z
    G1 Z50 F600
    
    RESPOND MSG="Flow Test Cube complete!"
    RESPOND MSG="Measure walls with calipers and calculate optimal flow"

# ----------------------------------------------------------------------------
# RETRACTION TEST PATTERN
# Prints towers with varying retraction lengths
# ----------------------------------------------------------------------------
[gcode_macro RETRACT_TEST_PATTERN]
description: Print retraction test pattern
gcode:
    {% set START_RETRACT = params.START|default(1.0)|float %}
    {% set END_RETRACT = params.END|default(4.0)|float %}
    {% set STEPS = params.STEPS|default(8)|int %}
    {% set TEMP = params.TEMP|default(205)|int %}
    
    # Heat up
    M104 S{TEMP}
    M190 S60
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={TEMP-5}
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM=55
    
    # Home
    G28
    G1 Z10 F600
    
    # Settings
    G90 ; Absolute positioning
    M83 ; Relative extrusion
    
    {% set step_size = (END_RETRACT - START_RETRACT) / STEPS %}
    {% set tower_height = 15 %}
    {% set layers_per_tower = 75 %}
    
    RESPOND MSG="Printing retraction test with {STEPS} towers"
    RESPOND MSG="Retraction range: {START_RETRACT}mm to {END_RETRACT}mm"
    
    {% for step in range(STEPS) %}
        {% set current_retract = START_RETRACT + (step * step_size) %}
        {% set x_pos = 30 + (step % 4) * 25 %}
        {% set y_pos = 30 + (step // 4) * 25 %}
        
        RESPOND MSG="Tower {step + 1}/{STEPS}: Retract={current_retract:.1f}mm"
        
        # Print tower
        {% for layer in range(layers_per_tower) %}
            {% set z_height = layer * 0.2 %}
            G1 Z{z_height} F600
            
            # Move to tower position
            G1 X{x_pos} Y{y_pos} F3000
            
            # Retract
            G1 E-{current_retract} F2100
            
            # Travel to next tower position (simulates stringing test)
            {% if step < STEPS - 1 %}
                {% set next_x = 30 + ((step + 1) % 4) * 25 %}
                {% set next_y = 30 + ((step + 1) // 4) * 25 %}
                G1 X{next_x} Y{next_y} F3000
                
                # De-retract
                G1 E{current_retract} F2100
            {% endif %}
            
            # Small square for this layer
            G1 X{x_pos + 10} E2 F1800
            G1 Y{y_pos + 10} E2 F1800
            G1 X{x_pos} E2 F1800
            G1 Y{y_pos} E2 F1800
        {% endfor %}
        
        # Lift Z
        G1 Z20 F600
    {% endfor %}
    
    RESPOND MSG="Retraction Test Pattern complete!"
    RESPOND MSG="Find tower with least stringing"

# ----------------------------------------------------------------------------
# TEMPERATURE TOWER
# Prints tower with varying temperatures
# ----------------------------------------------------------------------------
[gcode_macro TEMP_TOWER]
description: Print temperature tower
gcode:
    {% set START_TEMP = params.START|default(195)|int %}
    {% set END_TEMP = params.END|default(225)|int %}
    {% set STEP = params.STEP|default(5)|int %}
    {% set BED_TEMP = params.BED|default(60)|int %}
    
    # Heat bed
    M190 S{BED_TEMP}
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={BED_TEMP-5}
    
    # Home
    G28
    G1 Z10 F600
    
    # Settings
    G90 ; Absolute positioning
    M83 ; Relative extrusion
    G92 E0
    
    {% set sections = ((END_TEMP - START_TEMP) / STEP)|int + 1 %}
    {% set section_height = 10 %}
    {% set layers_per_section = 50 %}
    
    RESPOND MSG="Printing temperature tower"
    RESPOND MSG="Temp range: {START_TEMP}°C to {END_TEMP}°C"
    RESPOND MSG="Sections: {sections}"
    
    {% for section in range(sections) %}
        {% set current_temp = START_TEMP + (section * STEP) %}
        {% set z_start = section * section_height %}
        
        RESPOND MSG="Section {section + 1}/{sections}: {current_temp}°C"
        
        # Set temperature
        M104 S{current_temp}
        
        # Wait for temp to stabilize
        TEMPERATURE_WAIT SENSOR=extruder MINIMUM={current_temp-2} MAXIMUM={current_temp+2}
        G4 P3000 ; Extra wait for stabilization
        
        # Print section
        {% for layer in range(layers_per_section) %}
            {% set z_height = z_start + (layer * 0.2) %}
            G1 Z{z_height} F600
            
            # Print square
            G1 X100 Y100 F3000
            G1 X120 E10 F1800
            G1 Y120 E10 F1800
            G1 X100 E10 F1800
            G1 Y100 E10 F1800
            
            # Bridging test every 10 layers
            {% if layer % 10 == 0 and layer > 0 %}
                G1 X110 Y110 E5 F2000 ; Bridge
            {% endif %}
        {% endfor %}
    {% endfor %}
    
    # Cool down
    M104 S0
    G1 Z50 F600
    
    RESPOND MSG="Temperature Tower complete!"
    RESPOND MSG="Examine sections for best quality"
