//
/*******************************************************************************
 * 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.csnoserv;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
import jakarta.ws.rs.Path;
import com.google.gson.Gson;
import in.cdac.medinfo.csnotk.csnolib.agents.SNOMEDAgent;
import in.cdac.medinfo.csnotk.csnolib.api.ISNOMEDAgent;
import in.cdac.medinfo.csnotk.csnolib.commons.Constants;
import in.cdac.medinfo.csnotk.csnolib.disb.agent.DISBAgent;
import in.cdac.medinfo.csnotk.csnolib.disb.api.IDISBAgent;
import in.cdac.medinfo.csnotk.csnolib.disb.common.DISBContants;
import in.cdac.medinfo.csnotk.csnolib.icdmap.ICDMapAgent;
import in.cdac.medinfo.csnotk.csnolib.model.CompositeDescription;
import in.cdac.medinfo.csnotk.csnolib.model.Description;
import in.cdac.medinfo.csnotk.csnolib.util.PropertyReader;

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


/**
 * Provides basic search facility on the SNOMED Repository for searching the
 * SNOMED Concepts.
 *
 */

/**
 * @author root
 * @param
 *
 */
@Path("")
public class CSnoServResource {
	public static String errorLogPath = null;
	public static final String _0_9 = "[0-9]*";
	public static final String UTF_8 = "UTF-8";
	public static final String INVALID_QUERY_PARAMETER_VALUE_ID = "Invalid Query Parameter Value: id: {}";

	public static final String DOES_NOT_BELONG_TO=";does not belong to";
	public static final String BELONGS_TO=";belongs to";
	public static final ISNOMEDAgent agent = new SNOMEDAgent();
	public static final ICDMapAgent mapAgent = new ICDMapAgent();
	public static final IDISBAgent disbAgent = new DISBAgent();

	public static final Properties prop = new Properties();
	public static final Properties propICD = new Properties();

	public static  File jarFile = null;
	public static  String confPath = null;
	public static  String confICDPath = null;
	public static final Gson gson = new Gson();



	static {
		try {
			jarFile = new File(SNOMEDAgent.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
			confPath = jarFile.getParentFile().getParentFile().getParentFile().getPath() + File.separator
					+ "system.properties";
			confICDPath = jarFile.getParentFile().getParentFile().getParentFile().getPath() + File.separator
					+ "icd.properties";

			InputStream input = new FileInputStream(new File(confPath));
			if (input != null) {
				prop.load(input);
				input.close();
			}

			input = new FileInputStream(new File(confICDPath));

			if (input != null) {
				propICD.load(input);
				input.close();
			}

		} catch (Exception e) {
			// Log the Exception
			Logger logger = LogManager.getLogger(CSnoServResource.class);
			logger.error(LoggerUtility.Exception, LoggerUtility.createErrorMessage(e));
		}

	}

	/*
	 * This function is to initialize and set the configuration using defined
	 * properties
	 */
	public static void initProperties() {
		agent.init(prop.getProperty("data.directory"), prop.getProperty("log.directory"),
				prop.getProperty("searchReturnlimit") != null ? Integer.valueOf(prop.getProperty("searchReturnlimit"))
						: null,
				prop.getProperty("suggestReturnlimit") != null ? Integer.valueOf(prop.getProperty("suggestReturnlimit"))
						: null,
				prop.getProperty("topSuggestReturnlimit") != null
						? Integer.valueOf(prop.getProperty("topSuggestReturnlimit"))
						: null);
		PropertyReader.loadSystemProperties();
		errorLogPath = PropertyReader.getProperty(Constants.ERROR_LOG_DIR);

	}
	/*
	 * This function is to initialize and set the ICD configuration using defined
	 * properties
	 */
	public static void initICDProperties() {
		mapAgent.initICDDB(propICD.getProperty("jdbc_url_icd"), propICD.getProperty("icd.db.username"),
				propICD.getProperty("icd.db.password"), propICD.getProperty("icd.jdbc.driver.class"),
				propICD.getProperty("icd.jar.name"), propICD.getProperty("icd.query"));
	}


	/*
	 * This function is to initialize DISBAgern and set the configuration using
	 * defined properties
	 */
	public static void initDISBProperties() {
		disbAgent.init(prop.getProperty(DISBContants.PROPERTY_PROTOCOL),
				prop.getProperty(DISBContants.PROPERTY_HOSTNAME),
				prop.getProperty(DISBContants.PROPERTY_PORT) != null
						? Integer.valueOf(prop.getProperty(DISBContants.PROPERTY_PORT))
						: null,
				prop.getProperty(DISBContants.PROPERTY_CONTEXT), prop.getProperty(DISBContants.PROPERTY_LOG_DIRECTORY));
	}




	/**
	 * A method to get preferred name of a valid SCT-ID
	 *
	 * @param {@link CompositeDescription} is extended from {@link Description}
	 * @return {@link String} TermName
	 *
	 **/
	private String getPreferredName(Set<CompositeDescription> descriptions) {
		for (CompositeDescription description : descriptions) {
			if (description.getLangCode().equals("en") && description.getIsPreferredTerm().equals("1")
					&& description.getType().equals("900000000000013009")) {
				return description.getTerm();
			}
		}
		return "";
	}


}
