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 Roundtrip
        --layer-opacity 0.625
        --blend-mode 4
        --alpha-locked
        --hidden
        --layers 2
        --frames 3
        --frame-duration-ms 321
    RESULT_VARIABLE save_result
    OUTPUT_VARIABLE save_output
    ERROR_VARIABLE save_error)

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

string(FIND "${save_output}" "\"command\":\"save-project\"" save_command_index)
string(FIND "${save_output}" "\"bytes\":65830" save_bytes_index)
string(FIND "${save_output}" "\"layers\":2" save_layers_index)
string(FIND "${save_output}" "\"frames\":3" save_frames_index)
string(FIND "${save_output}" "\"layerOpacity\":0.625" save_opacity_index)
string(FIND "${save_output}" "\"blendMode\":4" save_blend_index)
string(FIND "${save_output}" "\"alphaLocked\":true" save_alpha_index)
string(FIND "${save_output}" "\"visible\":false" save_visible_index)
if(save_command_index LESS 0
    OR save_bytes_index LESS 0
    OR save_layers_index LESS 0
    OR save_frames_index LESS 0
    OR save_opacity_index LESS 0
    OR save_blend_index LESS 0
    OR save_alpha_index LESS 0
    OR save_visible_index LESS 0)
    message(FATAL_ERROR "save-project 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}" 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-project: ${load_output}${load_error}")
endif()

string(FIND "${load_output}" "\"command\":\"load-project\"" load_command_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\":3" load_frames_index)
string(FIND "${load_output}" "\"animationDurationMs\":963" load_duration_index)
string(FIND "${load_output}" "\"layerNames\":[\"Roundtrip 1\",\"Roundtrip 2\"]" load_layer_index)
string(FIND "${load_output}" "\"layerFrameCounts\":[3,3]" load_layer_frames_index)
string(FIND "${load_output}" "\"layerDurationsMs\":[963,963]" load_layer_durations_index)
string(FIND "${load_output}" "\"layerOpacities\":[0.625,0.625]" load_layer_opacity_index)
string(FIND "${load_output}" "\"layerBlendModes\":[\"overlay\",\"overlay\"]" load_layer_blend_index)
string(FIND "${load_output}" "\"layerAlphaLocked\":[true,true]" load_layer_alpha_index)
string(FIND "${load_output}" "\"layerVisible\":[false,false]" load_layer_visible_index)
if(load_command_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
    OR load_layer_durations_index LESS 0
    OR load_layer_opacity_index LESS 0
    OR load_layer_blend_index LESS 0
    OR load_layer_alpha_index LESS 0
    OR load_layer_visible_index LESS 0)
    message(FATAL_ERROR "load-project output did not contain expected round-trip summary: ${load_output}")
endif()
