package eu.dnetlib.enabling.aas.ctx.tools;

/**
 * Context recoder processing assertions. No version prefix is 
 * being attached. Chain is simply concatenation of assertions.
 * Public keys are not being in use as well.
 * @author mhorst
 *
 */
public class AssertionContextRecoder implements ICtxRecoder {

	public static final String VERSION = "assertion";
	
	/**
	 * TODO this is lame approach, but will work for now.
	 */
	protected String endOfAssertionMarkup = "</saml:Assertion>";
	
	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.aas.ctx.tools.ICtxRecoder#appendVersionPrefix(java.lang.String)
	 */
	@Override
	public String appendVersionPrefix(String secCtxChain) {
		return secCtxChain;
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.aas.ctx.tools.ICtxRecoder#decode(eu.dnetlib.enabling.aas.ctx.tools.CodingContext, java.lang.String, byte[])
	 */
	@Override
	public DecodingResponse decode(CodingContext ctx, String currentChain,
			byte[] otherPubKey) {
//		I know this is ugly, but should suffice for now
		if (currentChain==null) {
			return null;
		}
		DecodingResponse response = new DecodingResponse();
		String trimmedCurrentChain = currentChain.trim(); 
		int index = trimmedCurrentChain.indexOf(endOfAssertionMarkup);
		if (index==-1) {
			response.chain = null;
			response.secCtxId = trimmedCurrentChain;
		} else if (index==trimmedCurrentChain.length()-endOfAssertionMarkup.length()) {
			response.chain = null;
			response.secCtxId = trimmedCurrentChain;
		} else {
			response.chain = trimmedCurrentChain.substring(index +
					endOfAssertionMarkup.length());
			response.secCtxId = trimmedCurrentChain.substring(0,index + 
					endOfAssertionMarkup.length());
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.aas.ctx.tools.ICtxRecoder#encode(eu.dnetlib.enabling.aas.ctx.tools.CodingContext, java.lang.String, byte[])
	 */
	@Override
	public String encode(CodingContext ctx, String currentChain,
			byte[] otherPubKey) {
		if (ctx==null || ctx.secCtxId==null) {
			return null;
		} 
		if (currentChain==null) {
			return ctx.secCtxId;
		} else {
			return currentChain + ctx.secCtxId;
		}
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.aas.ctx.tools.ICtxRecoder#identify()
	 */
	@Override
	public String identify() {
		return VERSION;
	}

	public String getEndOfAssertionMarkup() {
		return endOfAssertionMarkup;
	}

	public void setEndOfAssertionMarkup(String endOfAssertionMarkup) {
		this.endOfAssertionMarkup = endOfAssertionMarkup;
	}
	
	public static void main(String[] args) {
//		simple tests
		AssertionContextRecoder recoder = new AssertionContextRecoder();
		String currentChain ="<saml:Assertion>1</saml:Assertion><saml:Assertion>2</saml:Assertion>";
		DecodingResponse response = recoder.decode(null, currentChain, null);
		System.out.println("secCtx: " + response.secCtxId);
		System.out.println("chain: " + response.chain);
		response = recoder.decode(null, response.chain, null);
		System.out.println("secCtx: " + response.secCtxId);
		System.out.println("chain: " + response.chain);
	}
}
