
import java.util.logging.Logger;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.security.Principal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vo.parameter.*;

public class SearchServlet extends javax.servlet.http.HttpServlet
{
   private static final Logger         LOGGER   = Logger.getLogger("PSearch");
   private static final SearchSettings settings = SearchSettings.getInstance("search.properties");

   final String RESPONSE_ENCODING = "utf-8";
   final String DEFAULT_RESPONSEFORMAT = settings.defaults.responseFormat;
   final String DEFAULT_SKY_SYSTEM     = settings.defaults.skySystem;
   final String DEFAULT_SPEC_SYSTEM    = settings.defaults.specSystem;
   final String DEFAULT_TIME_SYSTEM = "MJD_UTC"; // FIXME take from config file


   public void init() throws ServletException
   {
      super.init();

      LOGGER.info("DB   : " + settings.dbConn.toString());
   }

   protected void doMultiValuedParamNotSupported(String message, PrintWriter printWriter)
   {
      printWriter.println("MultiValuedParamNotSupported : " + message);
   }

   protected void doUsageError(String message, PrintWriter printWriter)
   {
      printWriter.println("UsageError : " + message);
   }

   protected void doError(String message, PrintWriter printWriter)
   {
      printWriter.println("Error : " + message);
   }


   protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, UnsupportedEncodingException
   {
      LOGGER.info("trace");
      legacyLogEntry(request);

      try
      {
         Map<String, String[]> params = request.getParameterMap();

         // VLKB-legacy init
         //Coord       coord = new Coord(params);
         SubsurveyId subsurveyId = new SubsurveyId(params);


         Coord coord = new Coord();
         coord.pos  = Pos.parsePos(params, DEFAULT_SKY_SYSTEM);
         coord.band = Band.parseBand(params, DEFAULT_SPEC_SYSTEM);
         coord.time = Time.parseTime(params, DEFAULT_TIME_SYSTEM);
         coord.pol  = Pol.parsePol(params);

         coord.fov     = Interval.parseInterval(params, "FOV");
         coord.spatres = Interval.parseInterval(params, "SPATRES");
         coord.specrp  = Interval.parseInterval(params, "SPECRP");
         coord.exptime = Interval.parseInterval(params, "EXPTIME");
         coord.timeres = Interval.parseInterval(params, "TIMERES");

         coord.id = SingleStringParam.parseSingleStringParam(params, "ID");

         coord.collection = SingleStringParam.parseSingleStringParam(params, "COLLECTION");
         coord.facility   = SingleStringParam.parseSingleStringParam(params, "FACILITY");
         coord.instrument = SingleStringParam.parseSingleStringParam(params, "INSTRUMENT");
         coord.dptype     = SingleStringParam.parseSingleStringParam(params, "DPTYPE");

         coord.target = SingleStringParam.parseSingleStringParam(params, "TARGET");
         coord.format = SingleStringParam.parseSingleStringParam(params, "FORMAT");

         // coord.calib  = Integer.parseInt(SingleStringParam.parseSingleStringParam(params, "CALIB"));
         // coord.maxrec = Integer.parseInt(SingleStringParam.parseSingleStringParam(params, "MAXREC"));
         // FIXME implement checks (?in vlkb-volib?): 0<= calib <=5 and maxrec >= 0

         // FIXME add to SQL-query params starting from fov ...


         /* query Obscore table */

         DbPSearch dbps;
         synchronized(DbPSearch.class)
         {
            dbps = new DbPSearch(settings.dbConn);
         }
         String[] pubdidArr = dbps.queryOverlapingPubdid(coord, subsurveyId);

         final String RESPONSE_ENCODING = "UTF-8";

         /* if filters installed response will be wrapped */

         if(response instanceof FormatResponseWrapper)
         {
            LOGGER.info("response-type is FormatResponseWrapper");

            response.setContentType("text/plain");
            response.setCharacterEncoding(RESPONSE_ENCODING);

            /* collect all search description and set to wrapped response */

            FormatResponseWrapper responseWrapper = (FormatResponseWrapper) response;
            responseWrapper.setPubdidArr(pubdidArr);
         }
         else
         {
            LOGGER.info("response-type is HttpServletResponse");

            response.setContentType("text/plain");
            response.setCharacterEncoding(RESPONSE_ENCODING);

            PrintWriter writer = response.getWriter();
            for(String pubdid : pubdidArr)
            {
               writer.println(pubdid);
            }
            writer.close();
         }
      }
      catch(MultiValuedParamNotSupported ex)
      {
         LOGGER.info("MultiValuedParamNotSupported: " + ex.getMessage());

         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
         response.setContentType("text/plain");

         PrintWriter writer = response.getWriter();
         doMultiValuedParamNotSupported(ex.getMessage(), writer);
         writer.close();
      }
      catch(IllegalArgumentException ex)
      {
         LOGGER.info("IllegalArgumentException: " + ex.getMessage());

         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
         response.setContentType("text/plain");

         PrintWriter writer = response.getWriter();
         doUsageError(ex.getMessage(), writer);
         writer.close();
      }
      catch(Exception ex)
      {
         LOGGER.info("Exception: " + ex.getMessage());
         ex.printStackTrace();

         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         response.setContentType("text/plain");

         PrintWriter writer = response.getWriter();
         doError(ex.toString(), writer);
         writer.close();
      }

      return;
   }


   private void legacyLogEntry(HttpServletRequest request) throws UnsupportedEncodingException
   {
      StringBuffer requestURL = request.getRequestURL();
      if (request.getQueryString() != null)
      {
         requestURL.append("?").append(request.getQueryString());
         String completeURL = requestURL.toString();
         String className = this.getClass().getSimpleName();
         LOGGER.info(className + " vlkb req from: "
               + request.getRemoteAddr()
               + " doGet: " + URLDecoder.decode(completeURL, "UTF-8"));
      }
   }


}

