"""
Inscoper NBO Example Script: Choice Elements
===========================================
This script demonstrates how to populate and inspect XSD choice elements, where a
single position in the document may hold one of several alternatives.

Prerequisites:
-------------
1. Installation: The `inscoper_nbo` wheel must be installed in the active Python
   environment.
"""

# --8<-- [start:main_logic]
import inscoper_nbo

# Step 1: A choice of repeated alternatives
# Pixels may carry BinData, TiffData, NGFFData, or MetadataOnly children. The
# choice object is filled first, then assigned to its parent.
pixels_choice = inscoper_nbo.Pixels_Choice()
print(pixels_choice.containsBinData(), pixels_choice.containsTiffData())

bin_data_list = inscoper_nbo.BinDataVector()
for compression in ("zlib", "bzip2"):
    bin_data = inscoper_nbo.BinData()
    bin_data.setCompression(compression)
    bin_data.setBigEndian(True)
    length = inscoper_nbo.NonNegativeLong_Type()
    length.set(12)
    bin_data.setLength(length)
    bin_data_list.append(bin_data)

pixels_choice.addBinData(bin_data_list)
print(pixels_choice.containsBinData(), pixels_choice.containsTiffData())

pixels = inscoper_nbo.Pixels()
pixels.setpixels_choice(pixels_choice)

# Pixels declares required attributes of its own, which a validated export would
# demand. Only the choice matters here, so export without validation; see the
# Describe an Acquired Image example for a complete, valid Pixels element.
print(pixels.toXmlString(False))

# Step 2: A choice of exclusive alternatives
# OME holds either a BinaryOnly reference or a full metadata document. Setting
# one alternative clears the other.
ome_choice = inscoper_nbo.OME_Choice()
print(ome_choice.isBinaryOnly(), ome_choice.isOME_Choice_1())

binary_only = inscoper_nbo.OME_Choice_InlineBinaryOnly()
uuid = inscoper_nbo.UniversallyUniqueIdentifier_Type()
uuid.set("urn:uuid:3e450fae-b8f2-4d35-aa54-702168b2487f")
binary_only.setUUID(uuid)
binary_only.setMetadataFile("metadata.ome.xml")
ome_choice.setBinaryOnly(binary_only)
print(ome_choice.isBinaryOnly(), ome_choice.isOME_Choice_1())

full_document = inscoper_nbo.OME_Choice_InlineOME_Choice_1()
project = inscoper_nbo.Project()
project_id = inscoper_nbo.ProjectID_Type()
project_id.set("Project:1")
project.setID(project_id)
project.setName("Mitosis screen")
full_document.addToProject_List(project)
ome_choice.setOME_Choice_1(full_document)
print(ome_choice.isBinaryOnly(), ome_choice.isOME_Choice_1())

ome = inscoper_nbo.OME()
ome.setome_choice(ome_choice)
print(ome.toXmlString())
# --8<-- [end:main_logic]
