/*******************************************************************************
 * 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 static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
import in.cdac.medinfo.csnotk.csnolib.agents.SNOMEDAgent;
import in.cdac.medinfo.csnotk.csnolib.commons.EnumComponent;
import in.cdac.medinfo.csnotk.csnolib.commons.EnumDirection;
import in.cdac.medinfo.csnotk.csnolib.commons.EnumLangRefset;
import in.cdac.medinfo.csnotk.csnolib.commons.EnumRelationshipType;
import in.cdac.medinfo.csnotk.csnolib.db.QueryManager;
import in.cdac.medinfo.csnotk.csnolib.model.ComponentFactory;
import in.cdac.medinfo.csnotk.csnolib.model.Concept;
import in.cdac.medinfo.csnotk.csnolib.model.Relationship;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import org.junit.Test;
//Log
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import in.cdac.medinfo.csnotk.csnolib.util.LoggerUtility;

/**
 * TestQueryManager class contains the test cases related to exploring details of SNOMED CT (concept Id).
 *
 */
public class TestQueryManager
{
	static{
		SNOMEDAgent agent = new SNOMEDAgent();

		agent.init();
        Logger logger = LogManager.getLogger(TestQueryManager.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));
		}
	}

	@Test
	public void testPopulateConcept() throws SQLException, java.text.ParseException
	{
        Logger logger = LogManager.getLogger(TestQueryManager.class);
		String conceptId = "22298006";
		QueryManager qm = new QueryManager();
		ComponentFactory componentFactory=ComponentFactory.getInstance();
		Concept c = (Concept)componentFactory.getComponent(EnumComponent.CONCEPT, null);
		qm.populateConcept(conceptId,c);
		assertTrue(null != c);
		logger.info(LoggerUtility.CONSOLE, c.getFullySpecifiedName());
	}

	/**
	 * test case to get concept Id(s) with a particular relation to a concept Id.
	 */
	@Test
	public void testGetRelationships() throws SQLException, ParseException
	{
		Logger logger = LogManager.getLogger(TestQueryManager.class);
		String conceptId = "22298006";
		String typeId = EnumRelationshipType.IS_A.getId();
		QueryManager qm = new QueryManager();
		List<Relationship> relationships = null;
		relationships = qm.getRelationships(conceptId, typeId,EnumDirection.DESTINATION);
		for (Relationship relationship : relationships) {
		    logger.info(LoggerUtility.CONSOLE, relationship.getSourceConcept().getId());
		}
		relationships = qm.getRelationships(conceptId, /*EnumRelationshipType.ASSOCIATED_MORPHOLOGY*/"116676008",EnumDirection.DESTINATION);
		assertTrue(relationships.size() == 0);
		relationships = qm.getRelationships(conceptId, typeId,EnumDirection.SOURCE);
		assertTrue(relationships.size() > 0);
		logger.info(LoggerUtility.CONSOLE, relationships.size());
	}

	/**
	 * test case to get all relationship associated with a concept Id
	 */
	@Test
	public void testGetAllRelationships() throws SQLException, ParseException {
		Logger logger = LogManager.getLogger(TestQueryManager.class);
		String conceptId = "80146002";
		QueryManager qm = new QueryManager();
		List<Relationship> relationships = null;
		relationships = qm.getRelationships(conceptId, "all",EnumDirection.SOURCE);
		assertTrue(relationships.size() > 0);
		logger.info(LoggerUtility.CONSOLE, "There are {} relations (source/destination) of the all type for concept {}",relationships.size(),conceptId);
	}

	/**
	 * test case to get preferred name for a concept Id
	 */
	@Test
	public void testgetPreferredName() throws SQLException {
		Logger logger = LogManager.getLogger(TestQueryManager.class);
		String conceptId = "38082009";
		String langRefset = "uk";
		QueryManager qm = new QueryManager();
		String preferredName=null;
		preferredName = qm.getPreferredName(conceptId, EnumLangRefset.getEnum(langRefset));
		assertNotNull(preferredName);
		logger.info(LoggerUtility.CONSOLE, "Preferred Name for the ConceptId : {} with LangRefset : {} is : {}", conceptId ,langRefset,preferredName);
	}

}
