package eu.dnetlib.data.mdstore;

import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;

import com.google.common.collect.Lists;

import eu.dnetlib.enabling.tools.ResourceLoaderHelper;
import eu.dnetlib.miscutils.collections.AffixCollection;
import eu.dnetlib.miscutils.collections.MappedCollection;
import eu.dnetlib.miscutils.functional.UnaryFunction;

/**
 * Handles mock mdstores.
 *
 * @author marko
 *
 */
public class MockMDStoreDao implements MDStoreDao {

	/**
	 * logger.
	 */
	private static final Log log = LogFactory.getLog(MockMDStoreDao.class); // NOPMD by marko on 11/24/08 5:02 PM

	/**
	 * filename extension length.
	 */
	private static final int EXT_LENGTH = 4;

	/**
	 * mdstore ID suffix.
	 */
	protected static final String MDSTORE_ID_SUFFIX = "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ==";

	/**
	 * list of spring resource instances representing the mdstore descriptive "xml" file, obtained with pattern matching
	 * from mdstoresPath.
	 */
	private List<Resource> mdstoreResources;

	/**
	 * list of mdstore IDs.
	 */
	private List<String> mdstores;

	/**
	 * path pattern to the xml files describing the mdstores. These files are currently dummy and are used only to infer
	 * the name of the mdstore mock data directory.
	 */
	private String mdstoresPath;

	/**
	 * spring resource loader.
	 */
	@Autowired
	private ResourceLoaderHelper resourceLoader;

	/**
	 * {@inheritDoc}
	 * @see eu.dnetlib.data.mdstore.MDStoreDao#getMDStore(java.lang.String)
	 */
	public MDStore getMDStore(final String mdId) {
		final String shortId = mdId.split("_")[0];

		return new MockMDStore(mdstoresPath.substring(0, mdstoresPath.length() - EXT_LENGTH - 1) + shortId + "/", resourceLoader);
	}

	/**
	 * {@inheritDoc}
	 * @see eu.dnetlib.data.mdstore.MDStoreDao#listMDStores()
	 */
	public List<String> listMDStores() {
		return Lists.newArrayList(new AffixCollection(getMdstores(), MDSTORE_ID_SUFFIX));
	}

	/**
	 * {@inheritDoc}
	 * @see eu.dnetlib.data.mdstore.MDStoreDao#createMDStore(java.lang.String)
	 */
	public boolean createMDStore(final String mdId) {
		// mock mdstore are read-only
		return false;
	}

	/**
	 * Initializes the mdstoreResources from the mdstoresPath.
	 *
	 * @throws IOException
	 *             could happen
	 */
	@PostConstruct
	public void initMDStores() throws IOException {
		mdstoreResources = Lists.newArrayList(resourceLoader.getResourcePatternResolver().getResources(mdstoresPath));
		mdstores = Lists.newArrayList(new MappedCollection<String, Resource>(mdstoreResources, new UnaryFunction<String, Resource>() {

			public String evaluate(final Resource res) {
				final String filename = res.getFilename();
				return filename.substring(0, filename.length() - EXT_LENGTH);
			}
		}));

		log.debug("MDSTORES: " + mdstores);
	}

	public List<Resource> getMdstoreResources() {
		return mdstoreResources;
	}

	public void setMdstoreResources(final List<Resource> mdstoreResources) {
		this.mdstoreResources = mdstoreResources;
	}

	public List<String> getMdstores() {
		return mdstores;
	}

	public void setMdstores(final List<String> mdstores) {
		this.mdstores = mdstores;
	}

	public String getMdstoresPath() {
		return mdstoresPath;
	}

	public void setMdstoresPath(final String mdstoresPath) {
		this.mdstoresPath = mdstoresPath;
	}

	public ResourceLoaderHelper getResourceLoader() {
		return resourceLoader;
	}

	public void setResourceLoader(final ResourceLoaderHelper resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

}
