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

import com.eactive.eai.adapter.socket2.common.SimpleFuture;
import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.filter.polling.HexPollingFilter;
import com.eactive.eai.adapter.socket2.protocol.TcpAdapterClient;
import com.eactive.eai.adapter.socket2.protocol.login.codec.BindCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.login.handshake.LoginClientFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;


public class LazyClient extends TcpAdapterClient {
	boolean isBooting = true;
	
	public LazyClient(SocketAdapterContext context) throws Exception {
		super(context);
		this.handler = new LazyHandler(this);
		config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new BindCodecFactory(this)));
		config.getFilterChain().addAfter("codec", "login.codec", new LoginClientFilter(this));
		if (context.isPollingUse()) {
			config.getFilterChain().addLast("polling", new HexPollingFilter(this));
		}
		
	}
	

	@Override
	public SimpleFuture start() throws Exception {
    	if (isBooting) {
			if (context.getTraceLevel() >= SocketAdapterContext.TRACE_LEVEL_INFO) {
				logger.info(context.getAdapterName() + " | lazy client protocol - skip start process when booting ...");
			}
			isBooting = false;
			
			SimpleFuture future = new SimpleFuture();
			future.set(Boolean.FALSE);
			return future;
		} else {
			if (context.getTraceLevel() >= SocketAdapterContext.TRACE_LEVEL_INFO) {
				logger.info(context.getAdapterName() + " | client protocol - start process when booting ...");
			}
			return super.start();
		}
	}	
	
}
