if(NOT DEFINED PANO_CLI)
    message(FATAL_ERROR "PANO_CLI 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}" save-document-project
        --path "${OUTPUT_PATH}"
        --width 64
        --height 32
    RESULT_VARIABLE save_result
    OUTPUT_VARIABLE save_output
    ERROR_VARIABLE save_error)

if(NOT save_result EQUAL 0)
    message(FATAL_ERROR "save-document-project failed: ${save_output}${save_error}")
endif()

string(FIND "${save_output}" "\"command\":\"save-document-project\"" save_command_index)
string(FIND "${save_output}" "\"document\":{\"width\":64,\"height\":32,\"layers\":2,\"frames\":2,\"facePayloads\":2}" save_document_index)
string(FIND "${save_output}" "\"export\":{\"dirtyFaces\":2,\"rgbaFacePayloads\":2" save_export_index)
if(save_command_index LESS 0 OR save_document_index LESS 0 OR save_export_index LESS 0)
    message(FATAL_ERROR "save-document-project output did not contain expected summary: ${save_output}")
endif()

if(NOT EXISTS "${OUTPUT_PATH}")
    message(FATAL_ERROR "save-document-project 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 save-document-project: ${inspect_output}${inspect_error}")
endif()

string(FIND "${inspect_output}" "\"dirtyFaces\":2" inspect_dirty_index)
string(FIND "${inspect_output}" "\"name\":\"Base\"" inspect_base_index)
string(FIND "${inspect_output}" "\"name\":\"Paint\"" inspect_paint_index)
string(REGEX MATCH "\"index\":0,\"durationMs\":100,\"dirtyFaces\":\\[\\{\"face\":0,\"box\":\\[2,3,3,4\\]" inspect_base_payload "${inspect_output}")
string(REGEX MATCH "\"index\":1,\"storedOrder\":1,\"name\":\"Paint\".*\"dirtyFaces\":\\[\\{\"face\":5,\"box\":\\[4,5,5,6\\]" inspect_paint_payload "${inspect_output}")
if(inspect_dirty_index LESS 0
    OR inspect_base_index LESS 0
    OR inspect_paint_index LESS 0
    OR NOT inspect_base_payload
    OR NOT inspect_paint_payload)
    message(FATAL_ERROR "inspect-project output did not contain expected document export 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 save-document-project: ${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\":2" load_payload_index)
string(FIND "${load_output}" "\"layers\":2" load_layers_index)
string(FIND "${load_output}" "\"frames\":2" load_frames_index)
string(FIND "${load_output}" "\"layerNames\":[\"Base\",\"Paint\"]" load_names_index)
string(FIND "${load_output}" "\"layerFrameCounts\":[2,1]" load_frame_counts_index)
string(FIND "${load_output}" "\"layerDurationsMs\":[350,333]" 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 document export summary: ${load_output}")
endif()
