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

import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.protocol.TcpAdapterServer;
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.XecureFixedServerFilter;
import com.eactive.eai.adapter.socket2.protocol.xecure.fixedheader.handshake.XecureFixedHeaderServerFilter;
import com.eactive.eai.adapter.socket2.protocol.xecure.welcome.codec.XecureWelcomeCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.xecure.welcome.handshake.XecureWelcomeServerFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;


public class XecureServer extends TcpAdapterServer {
	public XecureServer(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 XecureFixedServerFilter(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 XecureFixedServerFilter(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 XecureWelcomeServerFilter(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 XecureFixedHeaderServerFilter(this));
		} else {
			throw new Exception(context.getAdapterName() + " | UNKNOWN Xecure Type - " + context.getXecureType());
		}
	}
}
