package leigh.teremetori.sdk;

import leigh.eo.EO;
import leigh.eo.EODataType;
import leigh.eo.schema.DAOGenerator;
import leigh.eo.schema.dao.ElementElementDAO;
import leigh.eo.schema.dao.FieldUtil;
import leigh.eo.schema.dao.SchemaElementDAO;
import leigh.eo.schema.dao.SchemaUtil;
import leigh.esp.sdk.SchemaGeneratorApp;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.UniversalJob;

import java.io.File;
import java.io.FileNotFoundException;

public class SchemaGenerator {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(SchemaGeneratorApp.class);

    public static EO generate() {
        final EO schema = SchemaUtil.create(SchemaUtil.API_KEY);
        final EO updateFrame = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(updateFrame);
        ElementElementDAO.setApiName(updateFrame, "leigh.teremetori.sdk.v1.TelemetryFrame");
        ElementElementDAO.getFields(updateFrame).add(FieldUtil.createNotNull(
                "channel", "Events", EODataType.loop));
        ElementElementDAO.getFields(updateFrame).add(FieldUtil.createNotNull(
                "metrics", "Metrics", EODataType.loop));
        ElementElementDAO.getFields(updateFrame).add(FieldUtil.createNotNull(
                "events", "Events", EODataType.loop));
        ElementElementDAO.getFields(updateFrame).add(FieldUtil.createNotNull(
                "time", "Time", EODataType.instant));
        return schema;
    }

    public static void main(final String... args) throws FileNotFoundException {
        UniversalJob.banner(logger, "Apotheosis mk3", "2014 Alex Leigh");
        final EO schema = generate();
        final DAOGenerator gen = new DAOGenerator(new File("leigh-teremetori-sdk/src/main/java"));
        gen.generateJava("leigh.teremetori.sdk.schema.v1", schema);

        logger.info("Generated schema: {}", schema);
    }
}
