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-project
        --path "${OUTPUT_PATH}"
        --width 96
        --height 48
        --layer-name Payload
        --layers 2
        --frames 2
        --frame-duration-ms 321
        --include-test-face-payload
        --payload-layer 1
        --payload-frame 1
    RESULT_VARIABLE save_result
    OUTPUT_VARIABLE save_output
    ERROR_VARIABLE save_error)

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

string(FIND "${save_output}" "\"command\":\"save-project\"" save_command_index)
string(FIND "${save_output}" "\"layers\":2" save_layers_index)
string(FIND "${save_output}" "\"frames\":2" save_frames_index)
string(FIND "${save_output}" "\"facePayloads\":1" save_payload_index)
string(FIND "${save_output}" "\"payloadLayer\":1" save_payload_layer_index)
string(FIND "${save_output}" "\"payloadFrame\":1" save_payload_frame_index)
if(save_command_index LESS 0
    OR save_layers_index LESS 0
    OR save_frames_index LESS 0
    OR save_payload_index LESS 0
    OR save_payload_layer_index LESS 0
    OR save_payload_frame_index LESS 0)
    message(FATAL_ERROR "save-project payload output did not contain expected summary: ${save_output}")
endif()

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

string(REGEX MATCH "\"index\":1,\"storedOrder\":1,\"name\":\"Payload 2\".*\"index\":1,\"durationMs\":321,\"dirtyFaces\":\\[\\{\"face\":0" inspect_targeted_payload "${inspect_output}")
if(NOT inspect_targeted_payload)
    message(FATAL_ERROR "inspect-project output did not show the payload on layer 1 frame 1: ${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 payload save-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\":1" load_payload_index)
string(FIND "${load_output}" "\"width\":96" load_width_index)
string(FIND "${load_output}" "\"height\":48" load_height_index)
string(FIND "${load_output}" "\"layers\":2" load_layers_index)
string(FIND "${load_output}" "\"frames\":2" load_frames_index)
string(FIND "${load_output}" "\"animationDurationMs\":642" load_duration_index)
string(FIND "${load_output}" "\"layerNames\":[\"Payload 1\",\"Payload 2\"]" load_layer_index)
string(FIND "${load_output}" "\"layerFrameCounts\":[2,2]" load_layer_frames_index)
if(load_command_index LESS 0
    OR load_pixels_index LESS 0
    OR load_payload_index LESS 0
    OR load_width_index LESS 0
    OR load_height_index LESS 0
    OR load_layers_index LESS 0
    OR load_frames_index LESS 0
    OR load_duration_index LESS 0
    OR load_layer_index LESS 0
    OR load_layer_frames_index LESS 0)
    message(FATAL_ERROR "load-project output did not contain expected payload summary: ${load_output}")
endif()
