package in.cdac.medinfo.csnotk.csnolib.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import in.cdac.medinfo.csnotk.csnolib.disb.agent.DISBAgent;
//Log4j
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import in.cdac.medinfo.csnotk.csnolib.util.LoggerUtility;

public class DISBAgentAPI {
	static DISBAgent disbObj=new DISBAgent();
	public static void main(String[] args) {

		try{
		disbObj.init();
		//for testing init properties
		Logger logger = LogManager.getLogger(DISBAgentAPI.class);
		disbObj.testDISBConnection();
		BufferedReader serviceInput = new BufferedReader(new InputStreamReader(System.in));
		logger.info(LoggerUtility.CONSOLE, "Sample for DISB Lookup API. Please select the "
				+ "service you want to use\n1.Get information of Substance\n2.Get information of Medicine\n"
				+ "3.Get information of Generic\n"
				+ "Please enter any of the above options:");
		try {

			// converts the user input to integer value
			int param = Integer.parseInt(serviceInput.readLine());

			logger.info(LoggerUtility.CONSOLE, "Parameter: conceptId\n"
					+ "It refers to the SNOMED CT concept id that is to be searched"
					+ " in the SNOMED CT Repository. \nExamples- \nCoceptId's :\n"
					+ "For substance: 387517004\nFor medicine: 562731000189107\nFor generic: 318276009\nPlease enter concept id:");
			/**
			 * termInput takes user Input for the search term.
			 */
			BufferedReader conceptIdBF = new BufferedReader(new InputStreamReader(System.in));
			String conceptId=conceptIdBF.readLine();
			switch (param) {
			case 1: {
				getSubstanceInfo(conceptId);
				break;
			}
			case 2: {
				getMedicineInfo(conceptId);
				break;
			}
			case 3: {
				getGenericInfo(conceptId);
				break;
			}
			default:
				break;
			}
		} catch (Exception e) {
			logger.error(LoggerUtility.CONSOLE, LoggerUtility.createErrorMessage(e));
		}

		}catch(Exception e){
			Logger logger = LogManager.getLogger(DISBAgentAPI.class);
			logger.info(LoggerUtility.CONSOLE, e.getMessage());
		}

	}

	static void getSubstanceInfo(String conceptId){
		Logger logger = LogManager.getLogger(DISBAgentAPI.class);
		try{
			logger.info(LoggerUtility.CONSOLE, disbObj.getSubstanceDetail(conceptId));
		}catch(Exception e){
			logger.info(LoggerUtility.CONSOLE, e.getMessage());
		}
	}

	static void getGenericInfo(String conceptId){
		Logger logger = LogManager.getLogger(DISBAgentAPI.class);
		try{
			logger.info(LoggerUtility.CONSOLE, disbObj.getGenericDetail(conceptId));
		}catch(Exception e){
			logger.info(LoggerUtility.CONSOLE, e.getMessage());
		}
	}

	static void getMedicineInfo(String conceptId){
		Logger logger = LogManager.getLogger(DISBAgentAPI.class);
		try{
			logger.info(LoggerUtility.CONSOLE, disbObj.getMedicineDetail(conceptId));
		}catch(Exception e){
			logger.info(LoggerUtility.CONSOLE, e.getMessage());
		}
	}

}
