"""
Inscoper NBO Example Script: Inline Elements
===========================================
This script demonstrates how to work with inline element types, which the
generator creates for elements declared directly inside their parent rather than
as a named schema type.

Prerequisites:
-------------
1. Installation: The `inscoper_nbo` wheel must be installed in the active Python
   environment.
"""

# --8<-- [start:main_logic]
import inscoper_nbo

# Step 1: Recognize an inline type by its name
# An element declared inside Screen produces the class Screen_Inline<ElementName>,
# which exists only in the context of that parent.
screen = inscoper_nbo.Screen()

screen_id = inscoper_nbo.ScreenID_Type()
screen_id.set("Screen:1")
screen.setID(screen_id)

description = inscoper_nbo.Screen_InlineDescription()
description.set("Plate screen, 3 plates")
screen.setDescription(description)

# Step 2: Repeated inline elements behave like any other list
for plate in ("Plate:1", "Plate:2"):
    plate_ref = inscoper_nbo.Screen_InlinePlateRef()
    plate_id = inscoper_nbo.PlateID_Type()
    plate_id.set(plate)
    plate_ref.setID(plate_id)
    screen.addToPlateRef_List(plate_ref)

print(screen.toXmlString())

# Step 3: Inline elements are read back from XML into the same classes
screen.fromXmlString(
    """<?xml version="1.0"?>
<Screen ID="Screen:03">
  <Description>Rewritten from XML</Description>
  <PlateRef ID="Plate:01" />
  <PlateRef ID="Plate:02" />
  <PlateRef ID="Plate:03" />
</Screen>"""
)

print(screen.getDescription().get())
print([plate_ref.getID().get() for plate_ref in screen.getPlateRef_List()])
# --8<-- [end:main_logic]
