/*
 * Inscoper NBO Example: Inline Elements
 * =====================================
 * This example 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: inscoper-nbo-<version>.jar must be on the classpath.
 */

// --8<-- [start:main_logic]
import com.inscoper.nbo.PlateID_Type;
import com.inscoper.nbo.Screen;
import com.inscoper.nbo.ScreenID_Type;
import com.inscoper.nbo.Screen_InlineDescription;
import com.inscoper.nbo.Screen_InlinePlateRef;

public class InlineElements {
    public static void main(String[] args) {
        // 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 screen = new Screen();

        ScreenID_Type screenId = new ScreenID_Type();
        screenId.set("Screen:1");
        screen.setID(screenId);

        Screen_InlineDescription description = new Screen_InlineDescription();
        description.set("Plate screen, 3 plates");
        screen.setDescription(description);

        // Step 2: Repeated inline elements behave like any other list
        for (String plate : new String[] {"Plate:1", "Plate:2"}) {
            Screen_InlinePlateRef plateRef = new Screen_InlinePlateRef();
            PlateID_Type plateId = new PlateID_Type();
            plateId.set(plate);
            plateRef.setID(plateId);
            screen.addToPlateRef_List(plateRef);
        }

        System.out.println(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>""");

        System.out.println(screen.getDescription().get());
        for (Screen_InlinePlateRef plateRef : screen.getPlateRef_List()) {
            System.out.print(plateRef.getID().get() + " ");
        }
        System.out.println();
    }
}
// --8<-- [end:main_logic]
