package leigh.minecraft.link.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.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.UniversalJob;

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

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

    public static EO generate() {
        final EO schema = SchemaUtil.create(SchemaUtil.API_KEY);

        final EO moveEvent = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(moveEvent);
        ElementElementDAO.setApiName(moveEvent, "leigh.minecraft.link.v1.MoveEvent");
        ElementElementDAO.getFields(moveEvent).add(FieldUtil.createNotNull("x", "X Coordinate"));
        ElementElementDAO.getFields(moveEvent).add(FieldUtil.createNotNull("y", "Y Coordinate"));
        ElementElementDAO.getFields(moveEvent).add(FieldUtil.createNotNull("z", "Z Coordinate"));
        ElementElementDAO.getFields(moveEvent).add(FieldUtil.createNotNull("srcId", "Player ID", EODataType.uuid));
        ElementElementDAO.getFields(moveEvent).add(FieldUtil.createNotNull("srcName", "Player Name"));

        final EO shopEnchant = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(shopEnchant);
        ElementElementDAO.setApiName(shopEnchant, "leigh.minecraft.link.v1.ShopEnchant");
        ElementElementDAO.getFields(shopEnchant).add(FieldUtil.createNotNull("name", "Name"));
        ElementElementDAO.getFields(shopEnchant).add(
                FieldUtil.createNotNull("level", "Level", EODataType.number));

        final EO shop = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(shop);
        ElementElementDAO.setApiName(shop, "leigh.minecraft.link.v1.Shop");
        ElementElementDAO.getFields(shop).add(FieldUtil.createNotNull("x", "X Coordinate"));
        ElementElementDAO.getFields(shop).add(FieldUtil.createNotNull("y", "Y Coordinate"));
        ElementElementDAO.getFields(shop).add(FieldUtil.createNotNull("z", "Z Coordinate"));
        ElementElementDAO.getFields(shop).add(FieldUtil.createNotNull("owner", "Shop Owner", EODataType.uuid));
        ElementElementDAO.getFields(shop).add(FieldUtil.createNotNull("world", "World"));
        ElementElementDAO.getFields(shop).add(FieldUtil.createNotNull("price", "Item Price"));
        ElementElementDAO.getFields(shop).add(FieldUtil.createNotNull("item", "Item"));
        ElementElementDAO.getFields(shop).add(FieldUtil.create("enchants", "Enchantments", EODataType.loop));

        final EO chatMessage = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(chatMessage);
        ElementElementDAO.setApiName(chatMessage, "leigh.minecraft.link.v1.ChatMessage");
        ElementElementDAO.getFields(chatMessage).add(FieldUtil.createNotNull("srcId", "Player ID", EODataType.uuid));
        ElementElementDAO.getFields(chatMessage).add(FieldUtil.createNotNull("srcName", "Player Name"));
        ElementElementDAO.getFields(chatMessage).add(FieldUtil.createNotNull("msg", "Message"));

        final EO simpleResponse = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(simpleResponse);
        ElementElementDAO.setApiName(simpleResponse, "leigh.minecraft.link.v1.SimpleResponse");
        ElementElementDAO.getFields(simpleResponse).add(FieldUtil.createNotNull("msg", "Message"));

        final EO landMemberRemoveRequest = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(landMemberRemoveRequest);
        ElementElementDAO.setApiName(landMemberRemoveRequest, "leigh.minecraft.link.v1.LandMemberRemoveRequest");
        ElementElementDAO.getFields(landMemberRemoveRequest).add(FieldUtil.createNotNull("lot", "Lot Name"));
        ElementElementDAO.getFields(landMemberRemoveRequest).add(FieldUtil.createNotNull("src", "Request Source", EODataType.uuid));
        ElementElementDAO.getFields(landMemberRemoveRequest).add(FieldUtil.createNotNull("player", "Player Name"));

        final EO landMemberAddRequest = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(landMemberAddRequest);
        ElementElementDAO.setApiName(landMemberAddRequest, "leigh.minecraft.link.v1.LandMemberAddRequest");
        ElementElementDAO.getFields(landMemberAddRequest).add(FieldUtil.createNotNull("lot", "Lot Name"));
        ElementElementDAO.getFields(landMemberAddRequest).add(FieldUtil.createNotNull("src", "Request Source", EODataType.uuid));
        ElementElementDAO.getFields(landMemberAddRequest).add(FieldUtil.createNotNull("player", "Player Name"));
        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-minecraft-link-sdk/src/main/java"));
        gen.generateJava("leigh.minecraft.link.sdk.schema.v1", schema);

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