###########################################
# Smart Park Macros
###########################################

[gcode_macro _SMART_PARK]
description: Parks your printhead near the print area for pre-print hotend heating.
gcode:
    {% set kamp_settings = printer["gcode_macro _KAMP_Settings"] %}
    {% set bed_x_max = printer["gcode_macro PRINTER_PARAM"].max_x_position | float %}
    {% set bed_y_max = printer["gcode_macro PRINTER_PARAM"].max_y_position | float %}                                                                           
    {% set z_height = kamp_settings.smart_park_height | float %}
    {% set purge_margin = kamp_settings.purge_margin | float %}
    {% set purge_amount = kamp_settings.purge_amount | float %}
    {% set verbose_enable = kamp_settings.verbose_enable | abs %}
    {% set center_x = bed_x_max / 2 %}
    {% set center_y = bed_y_max / 2 %}
    {% set axis_minimum_x = printer.toolhead.axis_minimum.x | float %}
    {% set axis_minimum_y = printer.toolhead.axis_minimum.y | float %}
    {% set all_points = printer.exclude_object.objects | map(attribute='polygon') | sum(start=[]) %}
    {% set x_min = (all_points | map(attribute=0) | min | default(0)) %}
    {% set x_max = (all_points | map(attribute=0) | max | default(0)) %}
    {% set y_min = (all_points | map(attribute=1) | min | default(0)) %}
    {% set y_max = (all_points | map(attribute=1) | max | default(0)) %}
    {% set travel_speed = (printer.toolhead.max_velocity) * 60 | float %}
    {% set rapid_move = 10 %}
    {% set park_x_center = ([((x_max + x_min) / 2) - (purge_amount / 2), 0] | max) %}
    {% set park_y_center = ([((y_max + y_min) / 2) - (purge_amount / 2), 0] | max) %}
    {% if (park_x_center + purge_amount + rapid_move) > bed_x_max %}
        {% set park_x_center = (bed_x_max - (purge_amount + rapid_move)) %}
    {% endif %}
    {% if (park_y_center + purge_amount + rapid_move) > bed_y_max %}
        {% set park_y_center = (bed_y_max - (purge_amount + rapid_move)) %}
    {% endif %}
    {% set park_x_origin_low = (x_min - purge_margin) %}                                         
    {% set park_x_origin_high = (x_max + purge_margin) %}                                        
    {% set park_y_origin_low = (y_min - purge_margin) %}                                         
    {% set park_y_origin_high = (y_max + purge_margin) %}
    {% set detect_object = (x_min + x_max + y_min + y_max) %}
    {% if detect_object == 0 %}
        {% set x_min = 10 %}
        {% set y_min = 10 %}
        {% set z_height = 2 %}
    {% elif park_y_origin_low > 0 %}
        {% set x_min = park_x_center %}
        {% set y_min = park_y_origin_low %}
    {% elif park_x_origin_low > 0 %}
        {% set x_min = park_x_origin_low %}
        {% set y_min = park_y_center %}
    {% elif park_y_origin_high < bed_y_max %}
        {% set x_min = park_x_center %}
        {% set y_min = park_y_origin_high %}
    {% elif park_x_origin_high < bed_x_max %}
        {% set x_min = park_x_origin_high %}
        {% set y_min = park_y_center %}
    {% else %}
        {% set x_min = 10 %}
        {% set y_min = 10 %}
        {% set z_height = 2 %}
    {% endif %}

    {% if verbose_enable == True %}
        RESPOND TYPE=command MSG="Smart Park location: {x_min},{y_min}"
    {% endif %}
    SAVE_GCODE_STATE NAME=Presmartpark_State 
    G90
    {% if printer.toolhead.position.z < z_height %}
        G0 Z{z_height}
    {% endif %}
    G0 X{x_min} Y{y_min} F{travel_speed}
    G0 Z{z_height}
    RESTORE_GCODE_STATE NAME=Presmartpark_State 
