package eu.dnetlib.enabling.datasources;

import java.io.StringReader;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
import eu.dnetlib.enabling.resultset.client.ResultSetClient;
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
import eu.dnetlib.rmi.common.ResultSet;
import eu.dnetlib.rmi.data.DatabaseException;
import eu.dnetlib.rmi.data.DatabaseService;
import eu.dnetlib.rmi.datasource.DatasourceDesc;
import eu.dnetlib.rmi.datasource.DatasourceManagerServiceException;
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
import eu.dnetlib.rmi.enabling.ISLookUpException;
import eu.dnetlib.rmi.enabling.ISLookUpService;
import eu.dnetlib.rmi.enabling.ISRegistryService;
import org.antlr.stringtemplate.StringTemplate;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class DatasourceManagerClients {

	private static final Resource xslt = new ClassPathResource("/eu/dnetlib/enabling/datasources/repo_2_is.xslt");
	private static final String REPOSITORY_SERVICE_RESOURCE_TYPE = "RepositoryServiceResourceType";
	private static final Log log = LogFactory.getLog(DatasourceManagerClients.class);
	private static final String TMPLS_BASEDIR = "/eu/dnetlib/enabling/datasources/";
	private String db;

	@Autowired
	private UniqueServiceLocator serviceLocator;

	@Autowired
	private ResultSetClient resultSetClient;

	public String findDatasourceId(final String profileId) throws DatasourceManagerServiceException {
		try {
			return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
					"/*[.//RESOURCE_IDENTIFIER/@value='" + profileId + "']//EXTRA_FIELDS/FIELD[./key='OpenAireDataSourceId']/value/text()");
		} catch (Exception e) {
			log.error("Error finding dsId of profile " + profileId, e);
			throw new DatasourceManagerServiceException("Error finding dsId of profile " + profileId, e);
		}
	}

	public String getDatasourceProfile(final String dsId) throws DatasourceManagerServiceException {
		try {
			return serviceLocator.getService(ISLookUpService.class)
					.getResourceProfileByQuery(
							"collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')/*[.//EXTRA_FIELDS/FIELD[./key='OpenAireDataSourceId']/value/text() = '"
									+ dsId + "']");
		} catch (Exception e) {
			return null;
		}
	}

	public boolean deleteProfile(final String dsId) throws DatasourceManagerServiceException {
		try {
			final SAXReader reader = new SAXReader();

			final String profile = getDatasourceProfile(dsId);

			if (profile != null) {
				final Document docOld = reader.read(new StringReader(profile));
				final String profId = docOld.valueOf("//RESOURCE_IDENTIFIER/@value");
				serviceLocator.getService(ISRegistryService.class).deleteProfile(profId);
			}
			return true;
		} catch (Exception e) {
			log.error("Error deleting profile", e);
			throw new DatasourceManagerServiceException("Error deleting profile", e);
		}
	}

	public boolean regenerateProfile(final String dsId) throws DatasourceManagerServiceException {

		try {
			final SAXReader reader = new SAXReader();

			final List<String> list = getTransformedDatasourcesByCondition("ds.id= '" + dsId + "'", new ApplyXslt(xslt));

			if (list.size() != 1) { throw new DatasourceManagerServiceException("Illegal number of datasource with id " + dsId + ", size: " + list.size()); }

			final Document doc = reader.read(new StringReader(list.get(0)));

			final ISRegistryService registry = serviceLocator.getService(ISRegistryService.class);

			final String profile = getDatasourceProfile(dsId);

			if (profile != null) {
				final Document docOld = reader.read(new StringReader(profile));
				final String profId = docOld.valueOf("//RESOURCE_IDENTIFIER/@value");

				doc.selectSingleNode("//RESOURCE_IDENTIFIER/@value").setText(profId);

				registry.updateProfile(profId, doc.asXML(), REPOSITORY_SERVICE_RESOURCE_TYPE);
				log.info("Profile " + profId + " UPDATED for ds " + dsId);
			} else {
				final String profId = registry.registerProfile(doc.asXML());
				log.info("Valid Profile " + profId + " REGISTERED for ds " + dsId);
			}
			return true;
		} catch (Exception e) {
			log.error("Error saving profile: ", e);
			throw new DatasourceManagerServiceException("Error regenerating profile", e);
		}
	}

	public Iterable<String> searchSQL(final String sql) throws DatabaseException {
		final ResultSet<String> epr = serviceLocator.getService(DatabaseService.class).searchSQL(getDb(), sql);
		return resultSetClient.iter(epr, String.class);
	}

	public boolean updateSQL(final String dsId, final String sql, final boolean delete, final boolean updateprofile) throws DatasourceManagerServiceException {
		log.debug("Executing query SQL: " + sql);

		try {
			serviceLocator.getService(DatabaseService.class).updateSQL(getDb(), sql);
		} catch (DatabaseException e) {
			throw new DatasourceManagerServiceException(e);
		}

		if (updateprofile) {
			if (delete) {
				return deleteProfile(dsId);
			} else {
				return regenerateProfile(dsId);
			}
		}
		return false;
	}

	public boolean updateSQL(final String dsId, final String sqlTemplate, final Map<String, Object> params, final boolean delete, final boolean updateProfile)
			throws DatasourceManagerServiceException {

		verifyParams(params.values());
		verifyParams(params.keySet().toArray());

		try {
			final Resource resource = new ClassPathResource(TMPLS_BASEDIR + sqlTemplate);
			final StringTemplate st = new StringTemplate(IOUtils.toString(resource.getInputStream()));
			st.setAttributes(params);
			return updateSQL(dsId, st.toString(), delete, updateProfile);
		} catch (Exception e) {
			log.error("Error in updateSQL", e);
			throw new DatasourceManagerServiceException("Error in updateSQL", e);
		}
	}

	public List<DatasourceDesc> getDatasourcesByCondition(final String condition) throws DatasourceManagerServiceException {
		final SAXReader reader = new SAXReader();
		final List<DatasourceDesc> list = Lists.newArrayList();
		try {
			for (String s : getXmlDatasourcesByCondition(condition)) {
				final Document doc = reader.read(new StringReader(s));
				list.add(DatasourceFunctions.xmlToDatasourceDesc(doc));
			}
		} catch (Exception e) {
			log.error("Error obtaining datasources from db", e);
			throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
		}
		return list;

	}

	private void verifyParams(final Object... params) throws DatasourceManagerServiceException {

		final Pattern pattern = Pattern.compile("\\n");

		for (Object p : params) {
			log.debug("TESTING SQL PARAM:" + p);
			if (p == null || p.toString().isEmpty()) {
				log.error("Parameter null or empty");
				throw new DatasourceManagerServiceException("Parameter null or empty");
			} else if (pattern.matcher(p.toString()).matches()) {
				log.error("Parameter [" + p + "] contains an invalid character");
				throw new DatasourceManagerServiceException("Parameter [" + p + "] contains an invalid character");
			} else {
				log.debug("TEST OK");
			}
		}
	}

	private List<String> getTransformedDatasourcesByCondition(final String condition, final Function<String, String> function)
			throws DatasourceManagerServiceException {
		final List<String> list = Lists.newArrayList();
		try {
			for (String s : getXmlDatasourcesByCondition(condition)) {
				list.add(function.apply(s));
			}
		} catch (Exception e) {
			log.error("Error obtaining datasources from db", e);
			throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
		}
		return list;
	}

	private Iterable<String> getXmlDatasourcesByCondition(final String condition) throws DatasourceManagerServiceException {
		try {
			final Map<String, Object> params = Maps.newHashMap();

			if (condition != null && !condition.trim().isEmpty()) {
				params.put("condition", condition);
			}
			return searchSQL("getDatasources.sql.st", params);
		} catch (Exception e) {
			log.error("Error obtaining datasources from db", e);
			throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
		}
	}

	public Iterable<String> searchSQL(final String sqlTemplate, final Map<String, Object> params) throws DatasourceManagerServiceException {
		try {
			final Resource resource = new ClassPathResource(TMPLS_BASEDIR + sqlTemplate);
			final StringTemplate st = new StringTemplate(IOUtils.toString(resource.getInputStream()));
			if (params != null) {
				st.setAttributes(params);
			}

			final String sql = st.toString();

			log.debug("Executing SQL: " + sql);

			return searchSQL(sql);
		} catch (Exception e) {
			log.error("Error executing sql", e);

			throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
		}
	}

	public boolean isDefinedParam(final String ifaceId, final String field) throws DatasourceManagerServiceException {
		final Map<String, Object> params = Maps.newHashMap();
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
		params.put("field", DatasourceFunctions.asSqlValue(field));

		final List<String> list = Lists.newArrayList(searchSQL("searchApiCollectionParam.sql.st", params));

		return !list.isEmpty();
	}

	public Date findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
		final String xquery = "/*[.//DATAPROVIDER/@interface='" + ifaceId + "' and .//SCHEDULING/@enabled='true']//CRON/text()";
		try {
			final String cronExpression = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
			final CronExpression cron = new CronExpression(cronExpression);
			return cron.getNextValidTimeAfter(new Date());
		} catch (ISLookUpDocumentNotFoundException e) {
			// When the value is not found a null value must be returned
			return null;
		} catch (ISLookUpException e) {
			log.error("Error in xquery: " + xquery, e);
			throw new DatasourceManagerServiceException("Error in xquery: " + xquery, e);
		} catch (ParseException e) {
			log.error("Error parsing cron expression", e);
			throw new DatasourceManagerServiceException("Error parsing cron expression", e);
		}
	}

	public String getDb() {
		return db;
	}

	@Required
	public void setDb(final String db) {
		this.db = db;
	}

	public UniqueServiceLocator getServiceLocator() {
		return serviceLocator;
	}

	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

}
