if(NOT DEFINED PANO_CLI)
    message(FATAL_ERROR "PANO_CLI must be set")
endif()

if(NOT DEFINED STROKE_SCRIPT)
    message(FATAL_ERROR "STROKE_SCRIPT must be set")
endif()

if(NOT DEFINED OUTPUT_PATH)
    message(FATAL_ERROR "OUTPUT_PATH must be set")
endif()

get_filename_component(output_dir "${OUTPUT_PATH}" DIRECTORY)
file(MAKE_DIRECTORY "${output_dir}")
file(REMOVE "${OUTPUT_PATH}")

execute_process(
    COMMAND "${PANO_CLI}" apply-stroke-script
        --path "${STROKE_SCRIPT}"
        --output "${OUTPUT_PATH}"
        --width 64
        --height 32
    RESULT_VARIABLE apply_result
    OUTPUT_VARIABLE apply_output
    ERROR_VARIABLE apply_error)

if(NOT apply_result EQUAL 0)
    message(FATAL_ERROR "apply-stroke-script failed: ${apply_output}${apply_error}")
endif()

string(FIND "${apply_output}" "\"command\":\"apply-stroke-script\"" apply_command_index)
string(FIND "${apply_output}" "\"strokes\":2" apply_strokes_index)
string(FIND "${apply_output}" "\"samples\":9" apply_samples_index)
string(FIND "${apply_output}" "\"samplesPerStroke\":[6,3]" apply_samples_per_stroke_index)
string(FIND "${apply_output}" "\"document\":{\"width\":64,\"height\":32,\"layers\":1,\"frames\":1,\"facePayloads\":1}" apply_document_index)
string(FIND "${apply_output}" "\"payload\":{\"face\":0,\"box\":[0,0,11,11],\"width\":11,\"height\":11,\"bytes\":484}" apply_payload_index)
string(FIND "${apply_output}" "\"export\":{\"bytes\":" apply_export_index)
if(apply_command_index LESS 0
    OR apply_strokes_index LESS 0
    OR apply_samples_index LESS 0
    OR apply_samples_per_stroke_index LESS 0
    OR apply_document_index LESS 0
    OR apply_payload_index LESS 0
    OR apply_export_index LESS 0)
    message(FATAL_ERROR "apply-stroke-script output did not contain expected summary: ${apply_output}")
endif()

if(NOT EXISTS "${OUTPUT_PATH}")
    message(FATAL_ERROR "apply-stroke-script did not create ${OUTPUT_PATH}")
endif()

execute_process(
    COMMAND "${PANO_CLI}" inspect-project --path "${OUTPUT_PATH}"
    RESULT_VARIABLE inspect_result
    OUTPUT_VARIABLE inspect_output
    ERROR_VARIABLE inspect_error)

if(NOT inspect_result EQUAL 0)
    message(FATAL_ERROR "inspect-project failed after apply-stroke-script: ${inspect_output}${inspect_error}")
endif()

string(FIND "${inspect_output}" "\"dirtyFaces\":1" inspect_dirty_index)
string(FIND "${inspect_output}" "\"rgbaFacePayloads\":1" inspect_payload_index)
string(FIND "${inspect_output}" "\"name\":\"Stroke Script\"" inspect_layer_index)
string(REGEX MATCH "\"dirtyFaces\":\\[\\{\"face\":0,\"box\":\\[0,0,11,11\\]" inspect_face_box "${inspect_output}")
if(inspect_dirty_index LESS 0
    OR inspect_payload_index LESS 0
    OR inspect_layer_index LESS 0
    OR NOT inspect_face_box)
    message(FATAL_ERROR "inspect-project output did not contain expected applied stroke layout: ${inspect_output}")
endif()

execute_process(
    COMMAND "${PANO_CLI}" load-project --path "${OUTPUT_PATH}"
    RESULT_VARIABLE load_result
    OUTPUT_VARIABLE load_output
    ERROR_VARIABLE load_error)

if(NOT load_result EQUAL 0)
    message(FATAL_ERROR "load-project failed after apply-stroke-script: ${load_output}${load_error}")
endif()

string(FIND "${load_output}" "\"command\":\"load-project\"" load_command_index)
string(FIND "${load_output}" "\"pixelDataLoaded\":true" load_pixels_index)
string(FIND "${load_output}" "\"facePayloads\":1" load_payload_index)
string(FIND "${load_output}" "\"layers\":1" load_layers_index)
string(FIND "${load_output}" "\"frames\":1" load_frames_index)
string(FIND "${load_output}" "\"layerNames\":[\"Stroke Script\"]" load_names_index)
string(FIND "${load_output}" "\"layerFrameCounts\":[1]" load_frame_counts_index)
string(FIND "${load_output}" "\"layerDurationsMs\":[100]" load_durations_index)
if(load_command_index LESS 0
    OR load_pixels_index LESS 0
    OR load_payload_index LESS 0
    OR load_layers_index LESS 0
    OR load_frames_index LESS 0
    OR load_names_index LESS 0
    OR load_frame_counts_index LESS 0
    OR load_durations_index LESS 0)
    message(FATAL_ERROR "load-project output did not contain expected applied stroke summary: ${load_output}")
endif()
