// Path: HandsOn/Group05/PlanesMadrid/app/src/main/java/com/linkeddata/vktrsm/planesmadrid/ModelProvider.java
// public class ModelProvider {
//   private static Model model;
// 
//   public static Model getInstance() {  return model; }
// 
//   ModelProvider(Context context) {
//       Log.d("debug", "leyendo modelo");
//       InputStream instream = context.getResources().openRawResource(R.raw.rdf_completo);
//       model = ModelFactory.createDefaultModel();
//       model.read(instream, null);
//       Log.d("debug", "modelo leido");
// 
//   }
// }
// 
// Path: HandsOn/Group05/PlanesMadrid/app/src/main/java/com/linkeddata/vktrsm/planesmadrid/dtos/PlaceDTO.java
// public class PlaceDTO {
//   private String name;
//   private double[] location;
// 
//   public PlaceDTO(String name, double latitude, double longitude){
//     this.name = name;
//     this.location = new double[2];
//     this.location[0] = latitude;
//     this.location[1] = longitude;
//   }
// 
//   public String getName(){
//     return this.name;
//   }
// 
//   public double[] getLocation(){
//     return this.location;
//   }
// 
// }


import java.util.ArrayList;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import com.linkeddata.vktrsm.planesmadrid.ModelProvider;
import com.linkeddata.vktrsm.planesmadrid.dtos.PlaceDTO;
  private final String admingeo = "http://data.ordnancesurvey.co.uk/ontology/admingeo/";
  private final String mibarrio = "http://www.semanticweb.org/root/ontologies/2014/11/mibarrio#";

  @Override
  public ArrayList<PlaceDTO> getListByName(String nombre){
    ArrayList<PlaceDTO> result = new ArrayList<>();
    Resource mibarrio_SportFacility = model.createResource(mibarrio + "SportFacility");
    ResIterator rIter = this.model.listSubjectsWithProperty(RDF.type,mibarrio_SportFacility);
    while (rIter.hasNext() && result.size() < 10){
      Resource r = rIter.nextResource();
      Property schema_name = this.model.createProperty(schema+"name");
      Property schema_description = this.model.createProperty(schema+"description");
      if (r.hasProperty(schema_name)){
        String placeName = r.getRequiredProperty( schema_name ).getString();
        if (placeName.toLowerCase().contains(nombre.toLowerCase())){
          double[] location = this.getPlaceCoordinates(placeName.replace(" ",""));
          String placeDescription = "";
          if(r.hasProperty(schema_description)){
            placeDescription = r.getRequiredProperty( schema_description).getString();
          }
          PlaceDTO place = new PlaceDTO(placeName,location[0],location[1]);
          result.add(place);
        }
      }
    }
    return result;
  }
  public double[] getPlaceCoordinates(String place){
    double[] location = new double[2];
    Resource spatial = this.model.getResource(ns + "Spatial/" + place);
// Path: HandsOn/Group05/PlanesMadrid/app/src/main/java/com/linkeddata/vktrsm/planesmadrid/ModelProvider.java
// public class ModelProvider {
//   private static Model model;
// 
//   public static Model getInstance() {  return model; }
// 
//   ModelProvider(Context context) {
//       Log.d("debug", "leyendo modelo");
//       InputStream instream = context.getResources().openRawResource(R.raw.rdf_completo);
//       model = ModelFactory.createDefaultModel();
//       model.read(instream, null);
//       Log.d("debug", "modelo leido");
// 
//   }
// }
// 
// Path: HandsOn/Group05/PlanesMadrid/app/src/main/java/com/linkeddata/vktrsm/planesmadrid/dtos/PlaceDTO.java
// public class PlaceDTO {
//   private String name;
//   private double[] location;
// 
//   public PlaceDTO(String name, double latitude, double longitude){
//     this.name = name;
//     this.location = new double[2];
//     this.location[0] = latitude;
//     this.location[1] = longitude;
//   }
// 
//   public String getName(){
//     return this.name;
//   }
// 
//   public double[] getLocation(){
//     return this.location;
//   }
// 
// }


// Path: HandsOn/Group05/PlanesMadrid/app/src/main/java/com/linkeddata/vktrsm/planesmadrid/daos/GymsDAO.java
import java.util.ArrayList;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import com.linkeddata.vktrsm.planesmadrid.ModelProvider;
import com.linkeddata.vktrsm.planesmadrid.dtos.PlaceDTO;

  private final String admingeo = "http://data.ordnancesurvey.co.uk/ontology/admingeo/";
  private final String mibarrio = "http://www.semanticweb.org/root/ontologies/2014/11/mibarrio#";

  @Override
  public ArrayList<PlaceDTO> getListByName(String nombre){
    ArrayList<PlaceDTO> result = new ArrayList<>();
    Resource mibarrio_SportFacility = model.createResource(mibarrio + "SportFacility");
    ResIterator rIter = this.model.listSubjectsWithProperty(RDF.type,mibarrio_SportFacility);
    while (rIter.hasNext() && result.size() < 10){
      Resource r = rIter.nextResource();
      Property schema_name = this.model.createProperty(schema+"name");
      Property schema_description = this.model.createProperty(schema+"description");
      if (r.hasProperty(schema_name)){
        String placeName = r.getRequiredProperty( schema_name ).getString();
        if (placeName.toLowerCase().contains(nombre.toLowerCase())){
          double[] location = this.getPlaceCoordinates(placeName.replace(" ",""));
          String placeDescription = "";
          if(r.hasProperty(schema_description)){
            placeDescription = r.getRequiredProperty( schema_description).getString();
          }
          PlaceDTO place = new PlaceDTO(placeName,location[0],location[1]);
          result.add(place);
        }
      }
    }
    return result;
  }
  public double[] getPlaceCoordinates(String place){
    double[] location = new double[2];
    Resource spatial = this.model.getResource(ns + "Spatial/" + place);
    Property geo_latitude = this.model.createProperty(geo + "lat");