/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.solr.handler;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spell.Dictionary;
import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.SolrCore;
import org.apache.solr.util.HighFrequencyDictionary;
import org.apache.solr.util.plugin.SolrCoreAware;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.logging.Logger;

public class BrowseRequestHandler extends RequestHandlerBase implements SolrCoreAware {

  private static Logger log = Logger.getLogger(BrowseRequestHandler.class.getName());
  
  protected Directory spellcheckerIndexDir = new RAMDirectory();
  protected String dirDescription = "(ramdir)";
  protected String termSourceField;

  protected SolrParams args = null;
  
  @Override
  public void init(NamedList args) {
    super.init(args);
    this.args = SolrParams.toSolrParams(args);
  }

  public void inform(SolrCore core) 
  {
    // termSourceField = args.get(SOURCE_FIELD, args.get("termSourceField"));
    // try {
      // String dir = args.get(INDEX_DIR, args.get("spellcheckerIndexDir"));
      // if (null != dir) {
      //   File f = new File(dir);
      //   if ( ! f.isAbsolute() ) {
      //     f = new File(core.getDataDir(), dir);
      //   }
      //   dirDescription = f.getAbsolutePath();
      //   log.info("using spell directory: " + dirDescription);
      //   spellcheckerIndexDir = FSDirectory.getDirectory(f);
      // } else {
      //   log.info("using RAM based spell directory");
      // }
      // spellChecker = new SpellChecker(spellcheckerIndexDir);
    // } catch (IOException e) {
    //   throw new RuntimeException("Cannot open SpellChecker index", e);
    // }
  }

  /**
   * Processes the following query string parameters: q, multiWords, cmd rebuild,
   * cmd reopen, accuracy, suggestionCount, restrictToField, and onlyMorePopular.
   */
  @Override
  public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
    throws Exception {
    SolrParams p = req.getParams();
    String words = p.get("q");
    String cmd = p.get("cmd");
    if (cmd != null) {
      cmd = cmd.trim();
      // } else {
      //   throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Unrecognized Command: " + cmd);
      // }
    }

    // empty query string
    if (null == words || "".equals(words.trim())) {
      return;
    }
  }

  //////////////////////// SolrInfoMBeans methods //////////////////////

  public String getVersion() {
    return "$Revision: 601055 $";
  }

  public String getDescription() {
    return "The SpellChecker Solr request handler for SpellChecker index: " + dirDescription;
  }

  public String getSourceId() {
    return "$Id: SpellCheckerRequestHandler.java 601055 2007-12-04 19:40:29Z ryan $";
  }

  public String getSource() {
    return "$URL: http://svn.apache.org/repos/asf/lucene/solr/trunk/src/java/org/apache/solr/handler/SpellCheckerRequestHandler.java $";
  }

  public URL[] getDocs() {
    return null;
  }
}
