/*
 * Inscoper NBO Example: Import a MicroMetaApp Configuration
 * ========================================================
 * This example demonstrates how to populate an NBO Instrument from a MicroMetaApp
 * JSON configuration and export it as NBO-XML.
 *
 * Prerequisites:
 * -------------
 * 1. Installation: inscoper-nbo-<version>.jar must be on the classpath.
 * 2. Sample Data: this example reads micrometaapp_nikon_ti2.json. Change
 *    MMA_JSON_PATH below to the location where you saved it, or to your own
 *    MicroMetaApp configuration.
 */

import com.inscoper.nbo.Instrument;
import com.inscoper.nbo.Objective;

import java.nio.file.Path;
import java.nio.file.Paths;

public class ImportMicroMetaApp {

    // --8<-- [start:main_logic]
    static final String MMA_JSON_PATH = "micrometaapp_nikon_ti2.json";

    public static void main(String[] args) {
        // Step 1: Import the configuration
        // fromMicroMetaAppJsonFile() parses the JSON produced by MicroMetaApp, maps
        // every component to its NBO counterpart, and fills the instrument in one call.
        Instrument instrument = new Instrument();
        instrument.fromMicroMetaAppJsonFile(MMA_JSON_PATH);

        // A missing or unreadable file throws InscoperNBOException. The generated
        // method carries no throws clause, so catch Exception and read its message.
        try {
            new Instrument().fromMicroMetaAppJsonFile("does_not_exist.json");
        } catch (Exception error) {
            System.out.println("Import failed: " + error.getMessage());
        }

        // Step 2: Inspect what the import produced
        System.out.println("Instrument: " + instrument.getID().get());

        for (var stand : instrument.getMicroscopeStandGroup_List()) {
            System.out.println("Stand: " + stand.getXMLName() + " " + stand.getManufacturer() + " "
                    + stand.getModel());
        }

        for (Objective objective : instrument.getObjective_List()) {
            System.out.println(
                    "Objective: " + objective.getModel() + " - " + objective.getMagnification() + "x");
        }

        for (var detector : instrument.getDetectorGroup_List()) {
            System.out.println("Detector: " + detector.getXMLName() + " " + detector.getModel());
        }

        for (var lightSource : instrument.getLightSourceGroup_List()) {
            System.out.println(
                    "Light source: " + lightSource.getXMLName() + " " + lightSource.getModel());
        }

        // Step 3: Export the imported instrument as NBO-XML
        // Pass false to write exactly what the import produced, without validating the
        // document and without filling in schema default values.
        Path outputPath = Paths.get(System.getProperty("java.io.tmpdir"), "instrument_from_mma.xml");
        instrument.toXmlFile(outputPath.toString(), false);
        System.out.println("NBO-XML written to " + outputPath);
    }
    // --8<-- [end:main_logic]
}
