package fr.insee.pogues.search.source;

import fr.insee.pogues.metadata.service.MetadataService;
import fr.insee.pogues.search.model.*;
import fr.insee.pogues.search.service.SearchService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class ColecticaSourceImporterImpl implements ColecticaSourceImporter {

    private final static Logger logger = LogManager.getLogger(ColecticaSourceImporter.class);

    @Autowired
    MetadataService metadataService;

    @Autowired
    SearchService searchService;

    List<String> groupIds;

    @PostConstruct
    public void setUp() throws Exception {
        try {
            groupIds = metadataService.getGroupIds();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    @Override
    public void source() throws Exception {
        for (String id : groupIds) {
            logger.debug("Getting data from colectica API for group " + id);
            Group f = metadataService.getGroup(id);
            searchService.save("group", f);
            saveSeries(f.getSubGroups());
        }
    }

    public void saveSeries(List<SubGroup> subGroups) throws Exception {
        for (SubGroup s : subGroups) {
            searchService.save("sub-group", s);
            saveOperations(s.getStudyUnits());
        }
    }

    public void saveOperations(List<StudyUnit> studyUnits) throws Exception {
        for (StudyUnit o : studyUnits) {
            searchService.save("study-unit", o);
            saveDataCollections(o.getDataCollections());
        }
    }

    public void saveDataCollections(List<DataCollection> dataCollections) throws Exception {
        for (DataCollection dc : dataCollections) {
            searchService.save("data-collection", dc);
            saveInstrumentSchemes(dc.getInstrumentSchemes());
        }
    }

    public void saveInstrumentSchemes(List<InstrumentScheme> instrumentSchemes) throws Exception {
        for (InstrumentScheme i : instrumentSchemes) {
            searchService.save("instrument-scheme", i);
            saveQuestionnaires(i.getInstruments());
        }
    }

    public void saveQuestionnaires(List<Instrument> instruments) throws Exception {
        for (Instrument q : instruments) {
            searchService.save("instrument", q);
        }
    }


}

@@ -742,21 +742,20 @@
 String%3E 
-g
 ro
-up
+ot
 Ids;%0A%0A  
@@ -840,21 +840,20 @@
         
-g
 ro
-up
+ot
 Ids = me
@@ -1068,21 +1068,20 @@
 ng id : 
-g
 ro
-up
+ot
 Ids) %7B%0A 
@@ -1141,21 +1141,23 @@
 API for 
-g
 ro
-up
+ot id
  %22 + id)
@@ -1174,15 +1174,20 @@
     
-Group f
+PoguesItem r
  = m
@@ -1208,16 +1208,187 @@
 .get
-Group(id
+DDIRoot(id);%0A            logger.debug(%22Root contains %22 + r.getChildren().size() + %22 groups%22);%0A            for(PoguesItem g: r.getChildren()) %7B%0A                System.out.println(g
 );%0A 
@@ -1390,32 +1390,36 @@
 g);%0A            
+ 
+   
 searchService.sa
@@ -1430,17 +1430,17 @@
 group%22, 
-f
+g
 );%0A     
@@ -1442,24 +1442,28 @@
             
+  
+  
 saveSeries(f
@@ -1465,26 +1465,39 @@
 ies(
-f
+g
 .get
-SubGroups());
+Children());%0A            %7D
 %0A   
@@ -1542,24 +1542,26 @@
 es(List%3C
-SubGroup
+PoguesItem
 %3E subGro
@@ -1601,16 +1601,18 @@
 or (
-SubGroup
+PoguesItem
  s :
@@ -1709,18 +1709,16 @@
 .get
-StudyUnits
+Children
 ());
@@ -1771,25 +1771,26 @@
 ns(List%3C
-StudyUnit
+PoguesItem
 %3E studyU
@@ -1831,17 +1831,18 @@
 or (
-StudyUnit
+PoguesItem
  o :
@@ -1942,31 +1942,24 @@
 ns(o.get
-DataCollections
+Children
 ());%0A   
@@ -2013,30 +2013,26 @@
 ns(List%3C
-DataCollection
+PoguesItem
 %3E dataCo
@@ -2074,30 +2074,26 @@
    for (
-DataCollection
+PoguesItem
  dc : da
@@ -2204,33 +2204,24 @@
 s(dc.get
-InstrumentSchemes
+Children
 ());%0A   
@@ -2277,32 +2277,26 @@
 es(List%3C
-InstrumentSch
+PoguesIt
 em
-e
 %3E instru
@@ -2340,32 +2340,26 @@
    for (
-InstrumentSch
+PoguesIt
 em
-e
  i : ins
@@ -2472,19 +2472,16 @@
 .get
-Instruments
+Children
 ());
@@ -2538,26 +2538,26 @@
 es(List%3C
-Instrument
+PoguesItem
 %3E instru
@@ -2595,26 +2595,26 @@
    for (
-Instrument
+PoguesItem
  q : ins

Fix apply level addition in metadata hierarchy to ES imports