package com.dbnt.kcscopensearch.scheduler;

import com.dbnt.kcscopensearch.kcsc.docView.model.DocContentView;
import com.dbnt.kcscopensearch.kcsc.docView.model.DocIndexView;
import com.dbnt.kcscopensearch.kcsc.docView.model.DocTitleView;
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.service.DocSearchService;
import com.dbnt.kcscopensearch.kcsc.docView.service.DocViewService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@RequiredArgsConstructor
public class IndexingScheduler {

  private final DocViewService docViewService;
  private final DocSearchService docSearchService;

  @Scheduled(cron = "40 18 23 * * *")
  public void viewIndexing(){
    Map<String, List<?>> viewDataMap = docViewService.selectAllView();
    Map<String, List<?>> indexingDataMap = new HashMap<>();
    Iterator<String> keys = viewDataMap.keySet().iterator();
    while(keys.hasNext()){
      String key = keys.next();
      List<?> viewDataList = viewDataMap.get(key);
      if(!viewDataList.isEmpty()){
        switch (key){
          case "docTitle":
            List<DocTitle> titleList = new ArrayList<>();
            for(Object obj: viewDataList){
              titleList.add(new DocTitle((DocTitleView) obj));
            }
            indexingDataMap.put("docTitle", titleList);
            break;
          case "docIndex":
            List<DocIndex> indexList = new ArrayList<>();
            for(Object obj: viewDataList){
              indexList.add(new DocIndex((DocIndexView) obj));
            }
            indexingDataMap.put("docIndex", indexList);
            break;
          case "docContent":
            List<DocContent> contentList = new ArrayList<>();
            for(Object obj: viewDataList){
              contentList.add(new DocContent((DocContentView) obj));
            }
            indexingDataMap.put("docContent", contentList);
            break;
        }
      }
    }

    docSearchService.save(indexingDataMap);

    //docSearchService.save(indexingDataMap);
  }

  @Scheduled(cron = "40 20 23 * * *")
  public void titleIndexing(){
    List<DocTitleView> titleViewList = docViewService.selectAllDocTitle();
    List<DocTitle> titleList = new ArrayList<>();
    for(DocTitleView view: titleViewList){
      titleList.add(new DocTitle(view));
    }
    docSearchService.saveDocTitle(titleList);
  }
  @Scheduled(cron = "40 21 23 * * *")
  public void indexIndexing(){
    List<DocIndexView> indexViewList = docViewService.selectAllDocIndex();
    List<DocIndex> indexList = new ArrayList<>();
    for(DocIndexView view: indexViewList){
      indexList.add(new DocIndex(view));
    }
    docSearchService.saveDocIndex(indexList);
  }
  @Scheduled(cron = "40 25 23 * * *")
  public void contentIndexing(){
    List<DocContentView> contentViewList = docViewService.selectAllDocContent();
    List<DocContent> contentList = new ArrayList<>();
    for(DocContentView view: contentViewList){
      contentList.add(new DocContent(view));
    }
    docSearchService.saveDocContent(contentList);
  }
}
