package com.k_int.srusolr.util;

import com.k_int.svc.spatial.service.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.io.*;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.k_int.srusolr.action.GeneralQueryModel;

public class DirectGeoConverter implements SolrIndexConverter, ApplicationContextAware {

  private Gazetteer geocoder = null;
  private static Log log = LogFactory.getLog(PostcodeConverter.class);
  private ApplicationContext ctx;


  public DirectGeoConverter() {
    log.debug("New DirectGeoConverter");
  }

  public void init() {
    log.debug("Init");
  }

  public void setApplicationContext(ApplicationContext ctx) {
    this.ctx = ctx;
  }

  public Map<String, String> convert(String geo_query, GeneralQueryModel gqm) {

    log.debug("Attempting to geo query "+geo_query);
    Map<String, String> ret = new HashMap<String, String>();

    String[] geo_components = geo_query.split(",");

    if ( geo_components.length < 2 ) {
      throw new RuntimeException("Invalid format for geo query. Format should be Lat,Long[,Radius]");
    }

    gqm.query_type = "geo";
    gqm.lat = geo_components[0];
    gqm.lng = geo_components[1];
    if ( geo_components.length > 2 ) {
      gqm.radius = geo_components[2];
    }

    return ret;
  }
}
