/*******************************************************************************
 * Copyright 2019 Observational Health Data Sciences and Informatics & The Hyve
 *
 * This file is part of WhiteRabbit
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.ohdsi.rabbitInAHat.dataModel;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConceptsMap {

    private Map<String, Map<String, List<Concept>>> conceptMap;

    public ConceptsMap() {
        this.conceptMap = new HashMap<>();
    }

    ConceptsMap(String filename) throws IOException{
        this();
        this.load(filename);
    }

    private void load(String filename) throws IOException{
        InputStream conceptStream = Database.class.getResourceAsStream(filename);

        try {
            for (CSVRecord conceptRow : CSVFormat.RFC4180.withHeader().parse(new InputStreamReader(conceptStream))) {
                String tableName = conceptRow.get("omop_cdm_table");
                String fieldName = conceptRow.get("omop_cdm_field");

                Concept concept = new Concept();
                concept.setConceptId(conceptRow.get("concept_id"));
                concept.setConceptName(conceptRow.get("concept_name"));
                concept.setStandardConcept(conceptRow.get("standard_concept"));

                this.put(tableName, fieldName, concept);
            }
        } catch (IOException e) {
            throw new IOException("Could not load concept_id hints: " + e.getMessage());
        }
    }

    public void put(String targetTable, String targetField, Concept concept) {
        if (!this.conceptMap.containsKey(targetTable)) {
            this.conceptMap.put(targetTable, new HashMap<>());
        }
        if (!this.conceptMap.get(targetTable).containsKey(targetField)) {
            this.conceptMap.get(targetTable).put(targetField, new ArrayList<>());
        }
        this.conceptMap.get(targetTable).get(targetField).add(concept);
    }

    public List<Concept> get(String targetTable, String targetField) {
        if (!this.containsKey(targetTable, targetField)) {
            return null;
        }
        return this.conceptMap.get(targetTable).get(targetField);
    }

    public boolean containsKey(String targetTable, String targetField) {
        if (!this.conceptMap.containsKey(targetTable)) {
            return false;
        }
        if (!this.conceptMap.get(targetTable).containsKey(targetField)) {
            return false;
        }
        return true;
    }

    public class Concept {
        private String conceptId;
        private String conceptName;
        private String standardConcept;

        public Concept() {
        }

        public Concept(String conceptId, String conceptName) {
            this.conceptId = conceptId;
            this.conceptName = conceptName;
        }

        public String getConceptId() {
            return conceptId;
        }

        public void setConceptId(String conceptId) {
            this.conceptId = conceptId;
        }

        public String getConceptName() {
            return conceptName;
        }

        public void setConceptName(String conceptName) {
            this.conceptName = conceptName;
        }

        public String getStandardConcept() {
            return standardConcept;
        }

        public void setStandardConcept(String standardConcept) {
            this.standardConcept = standardConcept;
        }

        public String toString() {
            return this.conceptId + " -- " + this.conceptName;
        }
    }
}

@@ -1276,29 +1276,30 @@
 ap;%0D%0A%0D%0A    p
-ublic
+rivate
  ConceptsMap
@@ -3095,21 +3095,22 @@
 %0A%0D%0A    p
-ublic
+rivate
  boolean
@@ -3261,37 +3261,39 @@
      %7D%0D%0A        
-if (!
+return 
 this.conceptMap.
@@ -3337,70 +3337,8 @@
 eld)
-) %7B%0D%0A            return false;%0D%0A        %7D%0D%0A        return true
 ;%0D%0A 
@@ -3352,24 +3352,31 @@
 %0A    public 
+static 
 class Concep
@@ -3499,212 +3499,8 @@
 %0D%0A%0D%0A
-        public Concept() %7B%0D%0A        %7D%0D%0A%0D%0A        public Concept(String conceptId, String conceptName) %7B%0D%0A            this.conceptId = conceptId;%0D%0A            this.conceptName = conceptName;%0D%0A        %7D%0D%0A%0D%0A
     
@@ -3579,39 +3579,32 @@
    %7D%0D%0A%0D%0A        
-public 
 void setConceptI
@@ -3768,39 +3768,32 @@
    %7D%0D%0A%0D%0A        
-public 
 void setConceptN
@@ -3977,31 +3977,24 @@
 %0D%0A%0D%0A        
-public 
 void setStan

refactor ConceptsMap