package com.eactive.eai.adapter.socket2.protocol.xecure;

import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.protocol.TcpAdapterClient;
import com.eactive.eai.adapter.socket2.protocol.fixedheader.codec.FixedHeaderCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.xecure.envelope.XecureEnvelopeFilter;
import com.eactive.eai.adapter.socket2.protocol.xecure.fixed.handshake.XecureFixedClientFilter;
import com.eactive.eai.adapter.socket2.protocol.xecure.fixedheader.handshake.XecureFixedHeaderClientFilter;
import com.eactive.eai.adapter.socket2.protocol.xecure.welcome.codec.XecureWelcomeCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.xecure.welcome.handshake.XecureWelcomeClientFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;


public class XecureClient extends TcpAdapterClient {

	public XecureClient(SocketAdapterContext context) throws Exception {
		super(context);

	    if ("BASIC".equals(context.getXecureType())) {
			context.setLlFieldType("CHAR");
			context.setLlFieldOffset(0);
			context.setLlFieldLength(7);
			context.setLlFieldInclude("N");
			context.setLlOffsetExclude("N");
			context.setLlInclude("Y");
	    } else if ("WELCOME".equals(context.getXecureType())) {
			context.setLlFieldType("CHAR");
			context.setLlFieldOffset(0);
			context.setLlFieldLength(4);
			context.setLlFieldInclude("Y");
			context.setLlOffsetExclude("N");
			context.setLlInclude("Y");
	    } else if ("FIXEDHEADER".equals(context.getXecureType())) {
			context.setLlFieldType("CHAR");
			context.setLlFieldOffset(0);
			context.setLlFieldLength(7);
			context.setLlFieldInclude("N");
			context.setLlOffsetExclude("N");
			context.setLlInclude("Y");
	    }
	    
		if ("FIXED".equals(context.getXecureType()) || "BASIC".equals(context.getXecureType())) {
			this.handler = new XecureHandler(this);
			config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new FixedHeaderCodecFactory(this)));
			config.getFilterChain().addAfter("codec", "xecure.fixed", new XecureFixedClientFilter(this));
		} else if ("ENVELOPE".equals(context.getXecureType())) {
			this.handler = new XecureHandler(this);
			config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new FixedHeaderCodecFactory(this)));
			config.getFilterChain().addAfter("codec", "xecure.fixed", new XecureFixedClientFilter(this));
			config.getFilterChain().addAfter("xecure.fixed", "xecure.envelope", new XecureEnvelopeFilter(this));
		} else if ("WELCOME".equals(context.getXecureType())) {
			this.handler = new XecureHandler(this);
			config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new XecureWelcomeCodecFactory(this)));
			config.getFilterChain().addAfter("codec", "xecure.welcome", new XecureWelcomeClientFilter(this));
		} else if ("FIXEDHEADER".equals(context.getXecureType())) {
			this.handler = new XecureHandler(this);
			config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new FixedHeaderCodecFactory(this)));
			config.getFilterChain().addAfter("codec", "xecure.fixedheader", new XecureFixedHeaderClientFilter(this));
		} else {
			throw new Exception(context.getAdapterName() + " | UNKNOWN Xecure Type - " + context.getXecureType());
		}
	}
}
