package org.lionsoul.jcseg.elasticsearch.indices.analysis.jcseg;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.analysis.AnalyzerScope;
import org.elasticsearch.index.analysis.PreBuiltAnalyzerProviderFactory;
import org.elasticsearch.index.analysis.PreBuiltTokenFilterFactoryFactory;
import org.elasticsearch.index.analysis.PreBuiltTokenizerFactoryFactory;
import org.elasticsearch.index.analysis.TokenFilterFactory;
import org.elasticsearch.index.analysis.TokenizerFactory;
import org.elasticsearch.indices.analysis.IndicesAnalysisService;
import org.lionsoul.jcseg.analyzer.v5x.JcsegAnalyzer5X;
import org.lionsoul.jcseg.analyzer.v5x.JcsegTokenizer;
import org.lionsoul.jcseg.tokenizer.core.DictionaryFactory;
import org.lionsoul.jcseg.tokenizer.core.JcsegException;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;

/**
 * Registers indices level analysis components so, if not explicitly configured, will be shared
 * among all indices.
 */
public class JcsegIndicesAnalysis extends AbstractComponent
{
    @Inject
    public JcsegIndicesAnalysis(Settings settings, IndicesAnalysisService indicesAnalysisService)
    {
        super(settings);

        // Register jcseg analyzer
        indicesAnalysisService.analyzerProviderFactories().put(
            "jcseg", 
            new PreBuiltAnalyzerProviderFactory("jcseg", AnalyzerScope.INDICES, new JcsegAnalyzer5X(JcsegTaskConfig.COMPLEX_MODE))
        );

        // Register jcseg_tokenizer tokenizer
        indicesAnalysisService.tokenizerFactories().put("jcseg_tokenizer", 
                new PreBuiltTokenizerFactoryFactory(new TokenizerFactory() {
            @Override
            public String name() {
                return "jcseg_tokenizer";
            }

            @Override
            public Tokenizer create() {
                try {
                    /*
                     * @Added at 2016-05-02
                     * default to load the jcseg.properties configuration file
                     * in the jcseg plugin base directory {ES_HOME}/plugins/jcseg/
                    */
                    File proFile = new File("plugins/jcseg/jcseg.properties");
                    JcsegTaskConfig config = proFile.exists() ? new JcsegTaskConfig(proFile.getPath()) : new JcsegTaskConfig();
                    return new JcsegTokenizer(
                        JcsegTaskConfig.COMPLEX_MODE,
                        config,
                        DictionaryFactory.createSingletonDictionary(config)
                    );
                } catch (JcsegException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                return null;
            }
        }));

        // Register jcseg_word token filter
        indicesAnalysisService.tokenFilterFactories().put("jcseg_word", 
                new PreBuiltTokenFilterFactoryFactory(new TokenFilterFactory() {
            @Override
            public String name() {
                return "jcseg_word";
            }

            @Override
            public TokenStream create(TokenStream tokenStream) {
                return tokenStream;
            }
        }));
    }
}

@@ -923,32 +923,86 @@
 JcsegTokenizer;%0A
+import org.lionsoul.jcseg.tokenizer.core.ADictionary;%0A
 import org.lions
@@ -1522,24 +1522,313 @@
 eg analyzer%0A
+        File proFile = new File(%22plugins/jcseg/jcseg.properties%22);%0A        JcsegTaskConfig config = proFile.exists() ? new JcsegTaskConfig(proFile.getPath()) : new JcsegTaskConfig();%0A        ADictionary dic = DictionaryFactory.createSingletonDictionary(config);%0A        %0A        //default%0A
         indi
@@ -1949,24 +1949,58 @@
 Factory(
-%22jcseg%22,
+%0A                %22jcseg%22, %0A               
  Analyze
@@ -2015,16 +2015,33 @@
 NDICES, 
+%0A                
 new Jcse
@@ -2080,17 +2080,1070 @@
 LEX_MODE
-)
+, config, dic)%0A            )%0A        );%0A        %0A        //complex mode%0A        indicesAnalysisService.analyzerProviderFactories().put(%0A            %22jcseg_complex%22, %0A            new PreBuiltAnalyzerProviderFactory(%0A                %22jcseg%22, %0A                AnalyzerScope.INDICES, %0A                new JcsegAnalyzer5X(JcsegTaskConfig.COMPLEX_MODE, config, dic)%0A            )%0A        );%0A        %0A        //simple mode%0A        indicesAnalysisService.analyzerProviderFactories().put(%0A            %22jcseg_simple%22,%0A            new PreBuiltAnalyzerProviderFactory(%0A                %22jcseg%22, %0A                AnalyzerScope.INDICES, %0A                new JcsegAnalyzer5X(JcsegTaskConfig.SIMPLE_MODE, config, dic)%0A            )%0A        );%0A        %0A        //detect mode%0A        indicesAnalysisService.analyzerProviderFactories().put(%0A            %22jcseg_detect%22,%0A            new PreBuiltAnalyzerProviderFactory(%0A                %22jcseg%22, %0A                AnalyzerScope.INDICES, %0A                new JcsegAnalyzer5X(JcsegTaskConfig.DETECT_MODE, config, dic)%0A            
 )%0A      

add three more analyzer[jcseg_complex,jcseg_simple,jcseg_detect]