package it.unibz.inf.ontop.model.term.functionsymbol.impl.geof;

import com.google.common.collect.ImmutableList;
import it.unibz.inf.ontop.iq.node.VariableNullability;
import it.unibz.inf.ontop.model.term.*;
import it.unibz.inf.ontop.model.term.functionsymbol.FunctionSymbol;
import it.unibz.inf.ontop.model.term.functionsymbol.db.DBConcatFunctionSymbol;
import it.unibz.inf.ontop.model.term.functionsymbol.impl.SPARQLFunctionSymbolImpl;
import it.unibz.inf.ontop.model.type.DBTypeFactory;
import it.unibz.inf.ontop.model.type.RDFDatatype;
import it.unibz.inf.ontop.model.type.RDFTermType;
import it.unibz.inf.ontop.model.type.TermTypeInference;
import it.unibz.inf.ontop.model.vocabulary.GEOF;
import it.unibz.inf.ontop.model.vocabulary.SPARQL;
import it.unibz.inf.ontop.utils.ImmutableCollectors;
import org.apache.commons.rdf.api.IRI;

import javax.annotation.Nonnull;
import java.util.Optional;

public class GeofGetSRIDFunctionSymbolImpl extends AbstractGeofIRIFunctionSymbolImpl {//AbstractGeofIRIFunctionSymbolImpl {

    public static final String defSRID = "http://www.opengis.net/def/crs/OGC/1.3/CRS84";

    public GeofGetSRIDFunctionSymbolImpl(@Nonnull IRI functionIRI, RDFDatatype wktLiteralType, RDFDatatype xsdAnyUri) {
        super("GEOF_GETSRID", functionIRI, ImmutableList.of(wktLiteralType), xsdAnyUri);
    }

    @Override
    protected ImmutableTerm computeDBTerm(ImmutableList<ImmutableTerm> subLexicalTerms, ImmutableList<ImmutableTerm> typeTerms, TermFactory termFactory) {

        ImmutableTerm term = subLexicalTerms.get(0);

        if (term instanceof NonGroundFunctionalTerm) {
            NonGroundFunctionalTerm f = (NonGroundFunctionalTerm) term;
            FunctionSymbol fs = f.getFunctionSymbol();
            if (fs instanceof DBConcatFunctionSymbol) {
                // TEMPLATE FOUND!
                // DBConcatFunctionSymbol concat = (DBConcatFunctionSymbol) fs;
                if (f.getTerm(0) instanceof DBConstant) {
                    DBConstant t = (DBConstant) f.getTerm(0);
                    String tt = t.getValue();
                    if (tt.startsWith("<") && tt.indexOf(">") > 0) {
                        String srid = tt.substring(1, tt.indexOf(">"));
                        return termFactory.getDBStringConstant(srid);
                    }
                }
            }
        }

        return termFactory.getDBStringConstant(defSRID);
    }
}

@@ -1560,27 +1560,98 @@
     
-if (term instanceof
+Optional%3CImmutableTerm%3E immutableTerm = Optional.of(term)%0A                // template is a
  Non
@@ -1670,20 +1670,21 @@
 onalTerm
-) %7B%0A
+%0A    
         
@@ -1687,16 +1687,42 @@
         
+.filter(t -%3E t instanceof 
 NonGroun
@@ -1740,12 +1740,18 @@
 Term
- f =
+).map(t -%3E
  (No
@@ -1778,12 +1778,9 @@
 m) t
-erm;
+)
 %0A   
@@ -1788,16 +1788,45 @@
         
+    // template uses DBConcat
 Function
@@ -1836,36 +1836,36 @@
 bol 
-fs = f.getF
+as the f
 unction
-S
+al s
 ymbol
-();%0A
+%0A   
     
@@ -1876,14 +1876,43 @@
     
-if (fs
+ .filter(t -%3E t.getFunctionSymbol()
  ins
@@ -1942,18 +1942,16 @@
 nSymbol)
- %7B
 %0A       
@@ -1966,161 +1966,389 @@
  // 
-TEMPLATE FOUND!%0A                // DBConcatFunctionSymbol concat = (DBConcatFunctionSymbol) fs;%0A                if (f.getTerm(0) instanceof DBC
+the first argument is the string starting with the IRI of the SRID%0A                .map(t -%3E t.getTerm(0));%0A%0A        return getSRIDFromDbConstant(termFactory, immutableTerm);%0A%0A    %7D%0A%0A    private DBConstant getSRIDFromDbConstant(TermFactory termFactory, Optional%3CImmutableTerm%3E immutableTerm) %7B%0A        return immutableTerm%0A                // the first argument has to be a c
 onstant
-) %7B
 %0A   
@@ -2360,19 +2360,41 @@
         
-   
+.filter(t -%3E t instanceof
  DBConst
@@ -2400,12 +2400,18 @@
 tant
- t =
+).map(t -%3E
  (DB
@@ -2424,21 +2424,10 @@
 nt) 
-f.getTerm(0);
+t)
 %0A   
@@ -2443,34 +2443,31 @@
     
-    String tt = t.
+.map(Constant::
 getValue
 ();%0A
@@ -2466,11 +2466,9 @@
 alue
-();
+)
 %0A   
@@ -2484,18 +2484,76 @@
     
-    if (tt
+// the SRID is enclosed by %22%3C%22 and %22%3E%0A                .filter(v -%3E v
 .sta
@@ -2568,18 +2568,17 @@
 %22%3C%22) && 
-tt
+v
 .indexOf
@@ -2587,21 +2587,17 @@
 %3E%22) %3E 0)
- %7B%0A  
+%0A
         
@@ -2608,64 +2608,46 @@
     
-      String srid = tt.substring(1, tt.indexOf(%22%3E%22));%0A  
+// extract the SRID out of the string%0A
     
@@ -2658,28 +2658,25 @@
         
-      return
+.map(v -%3E
  termFac
@@ -2704,68 +2704,95 @@
 ant(
-srid);%0A                    %7D%0A                %7D%0A            %7D
+v.substring(1, v.indexOf(%22%3E%22))))%0A                // otherwise, returns the default SRID
 %0A   
@@ -2800,19 +2800,16 @@
     
-%7D%0A%0A
         
 retu
@@ -2804,23 +2804,24 @@
         
-return 
+.orElse(
 termFact
@@ -2852,16 +2852,17 @@
 defSRID)
+)
 ;%0A    %7D%0A

refactor GetSRID