package io.lumify.core.cmdline;

import com.google.inject.Inject;
import io.lumify.core.exception.LumifyException;
import io.lumify.core.model.ontology.OntologyRepository;
import io.lumify.core.model.ontology.OntologyRepositoryBase;
import io.lumify.core.model.properties.types.DoubleLumifyProperty;
import io.lumify.core.model.properties.types.GeoPointLumifyProperty;
import io.lumify.core.model.properties.types.StringLumifyProperty;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.semanticweb.owlapi.model.*;

import static org.securegraph.util.IterableUtils.toList;

public class OwlToJava extends CommandLineBase {
    private OntologyRepository ontologyRepository;
    private String inFileName;

    public static void main(String[] args) throws Exception {
        int res = new OwlToJava().run(args);
        if (res != 0) {
            System.exit(res);
        }
    }

    @Override
    protected Options getOptions() {
        Options options = super.getOptions();

        options.addOption(
                OptionBuilder
                        .withLongOpt("iri")
                        .withDescription("The IRI of the ontology you would like exported")
                        .isRequired()
                        .hasArg(true)
                        .withArgName("iri")
                        .create("i")
        );

        return options;
    }

    @Override
    protected void processOptions(CommandLine cmd) throws Exception {
        super.processOptions(cmd);
        this.inFileName = cmd.getOptionValue("in");
    }

    @Override
    protected int run(CommandLine cmd) throws Exception {
        IRI iri = IRI.create(cmd.getOptionValue("iri"));

        OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration();
        OWLOntologyManager m = ontologyRepository.createOwlOntologyManager(config, null);

        OWLOntology o = m.getOntology(iri);
        if (o == null) {
            System.err.println("Could not find ontology " + iri);
            return 1;
        }

        System.out.println("public class Ontology {");

        for (OWLObjectProperty objectProperty : o.getObjectPropertiesInSignature()) {
            if (!o.isDeclared(objectProperty, false)) {
                continue;
            }
            exportObjectProperty(o, objectProperty);
        }
        System.out.println();

        for (OWLClass owlClass : o.getClassesInSignature()) {
            if (!o.isDeclared(owlClass, false)) {
                continue;
            }
            exportClass(o, owlClass);
        }
        System.out.println();

        for (OWLDataProperty dataProperty : o.getDataPropertiesInSignature()) {
            if (!o.isDeclared(dataProperty, false)) {
                continue;
            }
            exportDataProperty(o, dataProperty);
        }
        System.out.println();

        System.out.println("}");

        return 0;
    }

    private void exportObjectProperty(OWLOntology o, OWLObjectProperty objectProperty) {
        String iri = objectProperty.getIRI().toString();
        String label = OntologyRepositoryBase.getLabel(o, objectProperty);

        System.out.println(String.format("    public static final String EDGE_LABEL_%s = \"%s\";", toJavaConst(label), iri));
    }

    private void exportClass(OWLOntology o, OWLClass owlClass) {
        String iri = owlClass.getIRI().toString();
        String label = OntologyRepositoryBase.getLabel(o, owlClass);

        System.out.println(String.format("    public static final String CONCEPT_TYPE_%s = \"%s\";", toJavaConst(label), iri));
    }

    private void exportDataProperty(OWLOntology o, OWLDataProperty dataProperty) {
        String iri = dataProperty.getIRI().toString();
        String label = OntologyRepositoryBase.getLabel(o, dataProperty);
        OWLDatatype range = (OWLDatatype) toList(dataProperty.getRanges(o)).get(0);
        String rangeIri = range.getIRI().toString();

        String type;
        if ("http://www.w3.org/2001/XMLSchema#double".equals(rangeIri)) {
            type = DoubleLumifyProperty.class.getSimpleName();
        } else if ("http://lumify.io#geolocation".equals(rangeIri)) {
            type = GeoPointLumifyProperty.class.getSimpleName();
        } else if ("http://www.w3.org/2001/XMLSchema#string".equals(rangeIri)) {
            type = StringLumifyProperty.class.getSimpleName();
        } else {
            throw new LumifyException("Could not map range type " + rangeIri);
        }

        System.out.println(String.format("    public static final %s %s = new %s(\"%s\");", type, toJavaConst(label), type, iri));
    }

    private String toJavaConst(String label) {
        return label.toUpperCase().replaceAll(" ", "_");
    }

    @Inject
    public void setOntologyRepository(OntologyRepository ontologyRepository) {
        this.ontologyRepository = ontologyRepository;
    }
}

@@ -757,39 +757,8 @@
 ory;
-%0A    private String inFileName;
 %0A%0A  
@@ -1428,186 +1428,8 @@
  %7D%0A%0A
-    @Override%0A    protected void processOptions(CommandLine cmd) throws Exception %7B%0A        super.processOptions(cmd);%0A        this.inFileName = cmd.getOptionValue(%22in%22);%0A    %7D%0A%0A
     

remove unused parameter