package com.dbnt.kcscopensearch.opensearch.docSearch.service;

import com.dbnt.kcscopensearch.opensearch.docSearch.model.DocContent;
import com.dbnt.kcscopensearch.opensearch.docSearch.model.DocIndex;
import com.dbnt.kcscopensearch.opensearch.docSearch.model.DocTitle;
import com.dbnt.kcscopensearch.opensearch.docSearch.repository.DocContentRepository;
import com.dbnt.kcscopensearch.opensearch.docSearch.repository.DocIndexRepository;
import com.dbnt.kcscopensearch.opensearch.docSearch.repository.DocTitleRepository;
import lombok.RequiredArgsConstructor;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DocSearchService {

  private final DocTitleRepository titleRepository;
  private final DocIndexRepository indexRepository;
  private final DocContentRepository contentRepository;
  private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName());

  @Transactional
  public void save(Map<String, List<?>> indexingDataMap){
    Iterator<String> keys = indexingDataMap.keySet().iterator();
    while(keys.hasNext()) {
      int i = 0;
      String key = keys.next();
      List<?> dataList = indexingDataMap.get(key);
      if (!dataList.isEmpty()) {
        switch (key) {
          case "docTitle":
            List<DocTitle> titleList = (List<DocTitle>) dataList;
            while (i<=titleList.size()){
              int endNum = i+1000;
              if(endNum>titleList.size()){
                endNum = titleList.size();
              }
              titleRepository.saveAll(titleList.subList(i, endNum));
              i+=1000;
            }
            break;
          case "docIndex":
            List<DocIndex> indexList = (List<DocIndex>) dataList;
            while (i<=indexList.size()){
              int endNum = i+1000;
              if(endNum>indexList.size()){
                endNum = indexList.size();
              }
              indexRepository.saveAll(indexList.subList(i, endNum));
              i+=1000;
            }
            break;
          case "docContent":
            List<DocContent> contentList = (List<DocContent>) dataList;
            while (i<=contentList.size()){
              int endNum = i+1000;
              if(endNum>contentList.size()){
                endNum = contentList.size();
              }
              contentRepository.saveAll(contentList.subList(i, endNum));
              i+=1000;
            }
            break;
        }
      }
    }
    System.out.println("indexing end");
  }


  @Transactional
  public void saveDocTitle(List<DocTitle> titleList) {
    log.info("title indexing start listSize: "+titleList.size());
    int i = 0;
    while (i<=titleList.size()){
      int endNum = i+1000;
      if(endNum>titleList.size()){
        endNum = titleList.size();
      }
      log.info("startNUm: "+i+" , endNum: "+endNum);
      titleRepository.saveAll(titleList.subList(i, endNum));
      i+=1000;
    }
    log.info("titleIndexing end");
  }

  @Transactional
  public void saveDocIndex(List<DocIndex> indexList) {
    log.info("index indexing start listSize: "+indexList.size());
    int i = 0;
    while (i<=indexList.size()){
      int endNum = i+1000;
      if(endNum>indexList.size()){
        endNum = indexList.size();
      }
      log.info("startNUm: "+i+" , endNum: "+endNum);
      indexRepository.saveAll(indexList.subList(i, endNum));
      i+=1000;
    }
    log.info("indexIndexing end");
  }

  @Transactional
  public void saveDocContent(List<DocContent> contentList) {
    log.info("content indexing start listSize: "+contentList.size());
    int i = 0;
    while (i<=contentList.size()){
      int endNum = i+1000;
      if(endNum>contentList.size()){
        endNum = contentList.size();
      }
      log.info("startNUm: "+i+" , endNum: "+endNum);
      contentRepository.saveAll(contentList.subList(i, endNum));
      i+=1000;
    }
    log.info("contentIndexing end");
  }

  public List<DocTitle> selectDocTitle(String text, Integer pageNum) {
    return titleRepository.findByDocNmContains(text, PageRequest.of(pageNum, 20));
  }

  public List<DocIndex> selectDocIndex(String text, Integer pageNum){
    return indexRepository.findByGroupTitleContains(text, PageRequest.of(pageNum, 20));
  }

  public List<DocContent> selectDocContent(String text, Integer pageNum){
    return contentRepository.findByTableContentContains(text, PageRequest.of(pageNum, 20));
  }
}
