package cn.com.pc.penguin.service;

import cn.com.pc.penguin.builder.keyword.Keyword;
import cn.com.pc.penguin.builder.keyword.KeywordDocumentBuilder;
import cn.com.pc.penguin.builder.keyword.KeywordSearchArgs;
import cn.com.pc.penguin.core.PenguinArgs;
import cn.com.pc.penguin.core.PenguinDocument;
import cn.com.pc.penguin.core.PenguinEngine;
import cn.com.pc.penguin.core.PenguinSearchHandler;
import cn.com.pc.penguin.service.impl.LocalPenguinService;
import cn.com.pc.penguin.utils.PenguinUtils;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.FSDirectory;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.*;

import static org.junit.Assert.*;

public class TestLocalPenguinService {

    @Before
    public void setUp() throws Exception {
        String indexHome = PenguinUtils.getPenguinIndexerDirectory();
        String logHome = PenguinUtils.getPenguinCatalog();
        System.out.println("indexHome:" + indexHome + "-----logHome:" + logHome);

        PenguinSearchHandler handler = new PenguinSearchHandler();
       // Map<String,Boolean> indexOnFile = new HashMap<String, Boolean>();
        //indexOnFile.put("KEYWORD",true);
       // handler.setIndexOnFile(indexOnFile);
        handler.init();
        PenguinEngine.searchHandler = handler;
        File file = new File(indexHome + "KEYWORD");
        FSDirectory fsDir = FSDirectory.open(file);
        if (!IndexReader.indexExists(fsDir)) {
            try {
                long a = System.currentTimeMillis();
                InputStream stream = this.getClass().getClassLoader().getResourceAsStream("auto_complete_material.txt");
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "GBK"));
                String line;
                line = reader.readLine();
                int lineNo = 1;
                List<Keyword> keywords = new ArrayList<Keyword>();
                while (line != null) {
                    // StringTokenizer token = new StringTokenizer(line, "|");
                    Keyword keyword = new Keyword();
                    keyword.setId((long) lineNo);
                    keyword.setKeyword(line);
                    keyword.setCreateDate(new Date());
                    keyword.setResult(0);
                    keyword.setSearches(0);
                    keywords.add(keyword);
                    if (lineNo % 1000 == 0) {
                        System.out.println(lineNo);
                        DocumentBuilder builder = new KeywordDocumentBuilder(keywords);
                        PenguinDocument pdoc = new PenguinDocument(builder);
                        PenguinEngine.searchHandler.index(pdoc);
                        keywords.clear();
                        Thread.sleep(5000);
                    }
                    line = reader.readLine();
                    lineNo++;
                }
                if (keywords.size() > 0) {
                    DocumentBuilder builder = new KeywordDocumentBuilder(keywords);
                    PenguinDocument pdoc = new PenguinDocument(builder);
                    PenguinEngine.searchHandler.index(pdoc);
                    keywords.clear();
                }
                reader.close();
                long b = System.currentTimeMillis();
                System.out.println("index success took : " + (b - a) + "ms");
                PenguinEngine.searchHandler.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


    @Test
    public void testSearch() throws Exception {
        try {
            String[] keywords = new String[]{"gz","gzdd","pg","pingg","dd","fks","三星","大道","1.6t","cool神酷","联想ThinkPad","锋锐K40","颐和A400","包头","喜欢看你"};
            for (String keyword : keywords) {
                KeywordSearchArgs args = new KeywordSearchArgs();
                args.setSuggestion(keyword.toLowerCase());
                PenguinArgs penguinArgs = new PenguinArgs(args);
                PenguinSearchResults result = PenguinEngine.searchHandler.search(penguinArgs);
                //assertEquals(result.getTotal(), 3632);
                List<String> results = result.getLists();
                System.out.println(keyword + ":");
                for (String res : results) {
                    System.out.println("keyword:" + res);
                }
                System.out.println("");
            }

          /*  for (int i = 1; i <= 100; i++) {
                long a = System.currentTimeMillis();
                result = PenguinEngine.searchHandler.search(penguinArgs);
                long b = System.currentTimeMillis();
                System.out.println(i + "'s penguin query spent:" + (b - a) + "(ms)");
            }*/

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
