/*
 * Inscoper NBO Example: Choice Elements
 * =====================================
 * This example 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: inscoper-nbo-<version>.jar must be on the classpath.
 */

// --8<-- [start:main_logic]
import com.inscoper.nbo.BinData;
import com.inscoper.nbo.BinDataVector;
import com.inscoper.nbo.NonNegativeLong_Type;
import com.inscoper.nbo.OME;
import com.inscoper.nbo.OME_Choice;
import com.inscoper.nbo.OME_Choice_InlineBinaryOnly;
import com.inscoper.nbo.OME_Choice_InlineOME_Choice_1;
import com.inscoper.nbo.Pixels;
import com.inscoper.nbo.Pixels_Choice;
import com.inscoper.nbo.Project;
import com.inscoper.nbo.ProjectID_Type;
import com.inscoper.nbo.UniversallyUniqueIdentifier_Type;

public class ChoiceElements {
    public static void main(String[] args) {
        // 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 pixelsChoice = new Pixels_Choice();
        System.out.println(pixelsChoice.containsBinData() + " " + pixelsChoice.containsTiffData());

        BinDataVector binDataList = new BinDataVector();
        for (String compression : new String[] {"zlib", "bzip2"}) {
            BinData binData = new BinData();
            binData.setCompression(compression);
            binData.setBigEndian(true);
            NonNegativeLong_Type length = new NonNegativeLong_Type();
            length.set(12);
            binData.setLength(length);
            binDataList.add(binData);
        }

        pixelsChoice.addBinData(binDataList);
        System.out.println(pixelsChoice.containsBinData() + " " + pixelsChoice.containsTiffData());

        Pixels pixels = new Pixels();
        pixels.setpixels_choice(pixelsChoice);

        // 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.
        System.out.println(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 omeChoice = new OME_Choice();
        System.out.println(omeChoice.isBinaryOnly() + " " + omeChoice.isOME_Choice_1());

        OME_Choice_InlineBinaryOnly binaryOnly = new OME_Choice_InlineBinaryOnly();
        UniversallyUniqueIdentifier_Type uuid = new UniversallyUniqueIdentifier_Type();
        uuid.set("urn:uuid:3e450fae-b8f2-4d35-aa54-702168b2487f");
        binaryOnly.setUUID(uuid);
        binaryOnly.setMetadataFile("metadata.ome.xml");
        omeChoice.setBinaryOnly(binaryOnly);
        System.out.println(omeChoice.isBinaryOnly() + " " + omeChoice.isOME_Choice_1());

        OME_Choice_InlineOME_Choice_1 fullDocument = new OME_Choice_InlineOME_Choice_1();
        Project project = new Project();
        ProjectID_Type projectId = new ProjectID_Type();
        projectId.set("Project:1");
        project.setID(projectId);
        project.setName("Mitosis screen");
        fullDocument.addToProject_List(project);
        omeChoice.setOME_Choice_1(fullDocument);
        System.out.println(omeChoice.isBinaryOnly() + " " + omeChoice.isOME_Choice_1());

        OME ome = new OME();
        ome.setome_choice(omeChoice);
        System.out.println(ome.toXmlString());
    }
}
// --8<-- [end:main_logic]
