package com.ifactory.press.db.solr.spelling.suggest;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.suggest.InputIterator;
import org.apache.lucene.search.suggest.analyzing.AnalyzingInfixSuggester;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Version;

public class SafeInfixSuggester extends AnalyzingInfixSuggester {
    
    private final boolean highlight;
    
    public SafeInfixSuggester(Version matchVersion, Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars, boolean highlight) throws IOException {
        super(matchVersion, dir, indexAnalyzer, queryAnalyzer, minPrefixChars);
        this.highlight = highlight;
        
        if (!DirectoryReader.indexExists(dir)) {
            // no index in place -- build an empty one so we are prepared for updates

            super.build (new InputIterator() {

                @Override
                public BytesRef next() throws IOException {
                    return null;
                }

                @Override
                public Comparator<BytesRef> getComparator() {
                    return null;
                }

                @Override
                public long weight() {
                    return 0;
                }

                @Override
                public BytesRef payload() {
                    return null;
                }

                @Override
                public boolean hasPayloads() {
                    return false;
                }

                @Override
                public Set<BytesRef> contexts() {
                    return null;
                }

                @Override
                public boolean hasContexts() {
                    return false;
                }
                
            });
        }
    }

    /*
     * disable highlighting
     */
    @Override
    public List<LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, boolean onlyMorePopular, int num) throws IOException {
      return lookup(key, contexts, num, true, highlight);
    }
    
    @Override
    /**
     * erase the existing index before rebuilding!
     */
    public void build(InputIterator iter) throws IOException {
      try {
        // use reflection to get the writer and delete everything before building
        Field field = AnalyzingInfixSuggester.class.getDeclaredField("writer");
        field.setAccessible(true);
        IndexWriter writer = (IndexWriter) field.get(this);
        if (writer != null) {
          writer.deleteAll();
          writer.commit();
        }
      } catch (IllegalAccessException e) {
      } catch (NoSuchFieldException e) {
      }
      super.build(iter);
    }

}

@@ -79,40 +79,8 @@
 on;%0A
-import java.lang.reflect.Field;%0A
 impo
@@ -246,52 +246,8 @@
 er;%0A
-import org.apache.lucene.index.IndexWriter;%0A
 impo
@@ -2292,646 +2292,8 @@
    %7D
-%0A    %0A    @Override%0A    /**%0A     * erase the existing index before rebuilding!%0A     */%0A    public void build(InputIterator iter) throws IOException %7B%0A      try %7B%0A        // use reflection to get the writer and delete everything before building%0A        Field field = AnalyzingInfixSuggester.class.getDeclaredField(%22writer%22);%0A        field.setAccessible(true);%0A        IndexWriter writer = (IndexWriter) field.get(this);%0A        if (writer != null) %7B%0A          writer.deleteAll();%0A          writer.commit();%0A        %7D%0A      %7D catch (IllegalAccessException e) %7B%0A      %7D catch (NoSuchFieldException e) %7B%0A      %7D%0A      super.build(iter);%0A    %7D
 %0A%0A%7D%0A

remove unneeded override