/*******************************************************************************
 * Copyright 2014 Centre for Development of Advanced Computing(C-DAC), Pune
 *
 * 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 in.cdac.medinfo.csnotk.csnolib.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import in.cdac.medinfo.csnotk.csnolib.agents.SNOMEDAgent;
import in.cdac.medinfo.csnotk.csnolib.model.CompositeDescription;
import in.cdac.medinfo.csnotk.csnolib.model.Concept;
import in.cdac.medinfo.csnotk.csnolib.model.Description;
//Logs
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import in.cdac.medinfo.csnotk.csnolib.util.LoggerUtility;

/**
 * SemanticTagList class finds the semantic tag of a concept Id
 */
public class SemanticTagList {

	/**
	 * main method
	 * @param args
	 */
	public static void main(String[] args)
	{
		SNOMEDAgent agent = new SNOMEDAgent();
		agent.init();
        Logger logger = LogManager.getLogger(SemanticTagList.class);
		logger.info(LoggerUtility.CONSOLE, "Do you want to create SNOMED CT database (y/n)");
		BufferedReader serviceInput = new BufferedReader(
				new InputStreamReader(System.in));
		try {
			String dbOverride = serviceInput.readLine();
			if(dbOverride.equalsIgnoreCase("y"))
			{
				logger.info(LoggerUtility.CONSOLE, "Please Enter SNOMED CT Release file path till Snapshot");
				String snomedDirPath=serviceInput.readLine();
				agent.createSNOMEDdb(snomedDirPath, true);
			}
		} catch (IOException e1) {
			//Log the Exception
			logger.error(LoggerUtility.Exception, LoggerUtility.createErrorMessage(e1));
		}

logger.info(LoggerUtility.CONSOLE, "Do you want to create SNOMED CT Extension database (y/n)");

		try {
			String dbOverride = serviceInput.readLine();
			if(dbOverride.equalsIgnoreCase("y"))
			{
				logger.info(LoggerUtility.CONSOLE, "Please Enter SNOMED CT Extension Release directory path till Snapshot");
				String snomedExtDirPath=serviceInput.readLine();
				agent.createExtensiondb(snomedExtDirPath, true);
			}
		} catch (IOException e1) {
			//Log the Exception
			logger.error(LoggerUtility.Exception, LoggerUtility.createErrorMessage(e1));
		}


		Set<CompositeDescription> descriptions = agent.search("223369002", false,null,null,false);
		Set<String> conceptIds = new HashSet<String>(descriptions.size());
		logger.info(LoggerUtility.CONSOLE, "SIZE OF DESC : {}" ,descriptions.size());

		for(Description description : descriptions)
		{
			conceptIds.add(String.valueOf(description.getConceptId()));
		}

		List<Concept> concepts = agent.getConcept(new ArrayList<String>(conceptIds));

		Map<String, List<Concept>> map = new HashMap<String, List<Concept>>();
		for(Concept concept : concepts){
			String semantictag = null;
			try
			{
				 semantictag = concept.getSemanticTag();
			}
			catch (Exception e)
			{
				//Log the Exception
                logger.error(LoggerUtility.Exception, LoggerUtility.createErrorMessage(e));
			}
			if(!map.containsKey(semantictag))
			{
				List<Concept> list = new ArrayList<Concept>();
				list.add(concept);
				map.put(semantictag, list);
			}
			else
			{
				List<Concept> list = map.get(semantictag);
				list.add(concept);
			}
		}

		Set<Entry<String, List<Concept>>> entries = map.entrySet();
		for(Entry<String, List<Concept>> entry : entries)
		{
			logger.info(LoggerUtility.CONSOLE, "{} --> {}",entry.getKey() , entry.getValue().size());
		}
	}

}
