/*-
 * ~~~~~~licensing~~~~~~
 * resources-service
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.resources.impl;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import ru.entaxy.esb.resources.EntaxyResource;
import ru.entaxy.esb.resources.EntaxyResourceProcessor;
import ru.entaxy.esb.resources.EntaxyResourceProvider;
import ru.entaxy.platform.base.support.CommonUtils;

public abstract class AbstractResourceProvider implements EntaxyResourceProvider {

	protected String protocol;

	protected String folderNameIgnoreExpression = "";
	protected String resourceNameIgnoreExpression = "";

	protected List<EntaxyResourceProcessor> processors;
	
	protected abstract EntaxyResource doGetResource(String location); 
	
	protected abstract List<EntaxyResource> doGetResources(String location);

	@Override
	public EntaxyResource getResource(String location) {
		return new ResourceWrapper(doGetResource(location)
				, getProtocol()
				, location
				, getProcessors());
	}
	
	@Override
	public List<EntaxyResource> getResources(String location) {
		String loc = location;
		if (loc == null)
			loc = "";
		else
			if (loc.endsWith("/"))
				loc = loc.substring(0, loc.length()-1);
		final String fLoc = loc;
		List<EntaxyResource> entaxyResources = doGetResources(loc);
		if (entaxyResources != null)
			return entaxyResources
					.stream()
					.filter(res -> !isToIgnore(res))
					.map(res -> new ResourceWrapper(res
							, getProtocol()
							, fLoc + "/" + res.getName()
							, getProcessors()))
					.collect(Collectors.toList());
		else
			return null;
	}

	protected boolean isToIgnore(EntaxyResource item) {
		if (item.isFolder())
			return isToIgnoreByName(folderNameIgnoreExpression, item);
		else
			return isToIgnoreByName(resourceNameIgnoreExpression, item);
	}
	
	protected boolean isToIgnoreByName(String expression, EntaxyResource item) {
		if (!CommonUtils.isValid(expression))
			return false;
		return Pattern.matches(expression, item.getName());
	}
	
	@Override
	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public void setIgnoreFolders(String expression) {
		this.folderNameIgnoreExpression = expression;
	}

	public void setIgnoreResources(String expression) {
		this.resourceNameIgnoreExpression = expression;
	}

	public List<EntaxyResourceProcessor> getProcessors() {
		return processors;
	}

	public void setProcessors(List<EntaxyResourceProcessor> processors) {
		this.processors = processors;
	}
	
}
