package eu.dnetlib.contract.mocks;

import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.wsaddressing.W3CEndpointReference;

import eu.dnetlib.contract.utils.FileUtils;
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;

public class ResultSetMock implements ResultSetService, IAuthzPolicySettable {

	public static final String predefinedPoliciesRSId = "predefinedPoliciesRSId";
	
	public static final String predefinedUserSecProfRSId = "predefinedUserSecProfRSId";
	
	protected String authnPolicyLocation;
	protected String authzPolicyLocation;
	protected String userSecProfLocation;
	

	@Override
	public void closeRS(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public W3CEndpointReference createPullRS(String arg0, String arg1,
			int arg2, int arg3, String arg4, Integer arg5, Integer arg6) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public W3CEndpointReference createPullRSEPR(W3CEndpointReference arg0,
			String arg1, int arg2, int arg3, String arg4, Integer arg5,
			Integer arg6) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public W3CEndpointReference createPushRS(int arg0, int arg1)
			throws ResultSetException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getNumberOfElements(String arg0) throws ResultSetException {
		if (arg0.equals(predefinedPoliciesRSId)) {
			if (authzPolicyLocation!=null) {
				return 2;
			} else {
				return 1;
			}
		} if (arg0.equals(predefinedUserSecProfRSId)) {
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public String getProperty(String arg0, String arg1)
			throws ResultSetException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getRSStatus(String arg0) throws ResultSetException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> getResult(String arg0, int arg1, int arg2, String arg3)
			throws ResultSetException {
		if (arg0.equals(predefinedPoliciesRSId)) {
			try {
				List<String> result = new ArrayList<String>();
				result.add(FileUtils.getFileContent(authnPolicyLocation));
				if (authzPolicyLocation!=null) {
					result.add(FileUtils.getFileContent(authzPolicyLocation));
				}
				return result;
			} catch (Exception e) {
				throw new ResultSetException(e);
			}
		} else if (arg0.equals(predefinedUserSecProfRSId)) {
			try {
				String content = FileUtils.getFileContent(userSecProfLocation);
				List<String> result = new ArrayList<String>();
				result.add(content);
				return result;
			} catch (Exception e) {
				throw new ResultSetException(e);
			}
		} else {
			return null;
		}
	}

	@Override
	public String populateRS(String arg0, List<String> arg1)
			throws ResultSetException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String identify() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void notify(String arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void start() {
		// TODO Auto-generated method stub

	}

	public void setAuthnPolicyLocation(String authnPolicyLocation) {
		this.authnPolicyLocation = authnPolicyLocation;
	}

	public void setUserSecProfLocation(String userSecProfLocation) {
		this.userSecProfLocation = userSecProfLocation;
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.contract.mocks.IAuthzPolicySettable#setAuthzPolicyLocation(java.lang.String)
	 */
	public void setAuthzPolicyLocation(String authzPolicyLocation) {
		this.authzPolicyLocation = authzPolicyLocation;
	}

}
