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

import com.eactive.eai.adapter.socket2.common.SMSLogger;
import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.config.SocketLoggerManager;
import com.eactive.eai.adapter.socket2.protocol.ByteMessage;
import com.eactive.eai.adapter.socket2.protocol.ISocketAdapter;
import com.eactive.eai.common.util.Logger;
import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoSession;


abstract public class LoginFilter extends IoFilterAdapter {
	protected final Logger logger;
	protected final Logger errLogger;

//	private static final String NEXT_FILTER = LoginFilter.class.getName() + ".NextFilter";	
	public static final String FILTER_STATE_KEY = LoginFilter.class.getName() + ".STATE";

	public static final String SESSION_STATE = "SESSION_STATE";
	
	public enum State {
		STATE_START, 
		STATE_LOGIN_SEND,
		STATE_LOGIN_RECV,
		STATE_MESSAGE;
	}

	public static class FilterState {
		public State state = null;
		public byte[] key;  // token?
		
		public FilterState() {}
	}	
	
    protected SocketAdapterContext context;
	protected ISocketAdapter adapter;
	
	protected byte[] cert;
	protected byte[] key;
	protected String password;
	protected byte[] remoteCert;		

	protected LoginFilter(ISocketAdapter adapter) {
		this.adapter = adapter;
		this.context = adapter.getContext();

	    logger    = SocketLoggerManager.getInstance().getLogger(context.getAdapterGroupName());
	    errLogger = SocketLoggerManager.getInstance().getErrLogger();
	}
	
	@Override
	public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
		synchronized(session) {
			FilterState filterState = (FilterState) session.getAttribute(FILTER_STATE_KEY);
			if (filterState == null) {
				filterState = new FilterState();
				filterState.state = State.STATE_LOGIN_SEND;
				session.setAttribute(FILTER_STATE_KEY, filterState);
			}
		}
		
		// handshake future
//		session.setAttribute(ProtocolHandler.HANDSHAKE_FUTURE, new SimpleFuture());
	}	
  
	@Override
	public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
		nextFilter.sessionClosed(session);
	}

	
	@Override
    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
		ByteMessage m = null;
		
		if (writeRequest.getMessage() instanceof byte[]) {
			m = new ByteMessage((byte[]) writeRequest.getMessage());
		} else if (writeRequest.getMessage() instanceof ByteMessage) {
			m = (ByteMessage) writeRequest.getMessage();
		} else if (writeRequest.getMessage() instanceof String) {
			m = new ByteMessage((String) writeRequest.getMessage());
		} else if (writeRequest.getMessage() instanceof ByteBuffer) {
			m = new ByteMessage((ByteBuffer) writeRequest.getMessage());
		} else {
			throw new Exception("Unknown data format");
		}
		
		try {
			FilterState filterState = (FilterState) session.getAttribute(FILTER_STATE_KEY);
	
			switch (filterState.state) {			
			case STATE_MESSAGE:
//				String sessionState = (String) session.getAttribute(ProtocolHandler.SESSION_STATE);
				break;
				
			default:
				break;
			}
		} catch(Exception e) {
			adapter.addRecentError(context.getAdapterName() + " | sms error - " + e.getMessage(), e);
			logger.error(context.getAdapterName() + " | sms error - " + e.getMessage(), e);
			errLogger.error(context.getAdapterName() + " | sms error - " + e.getMessage(), e);
			SMSLogger.loggingSMS4Soc(context.getAdapterName(), context.getDescription(), "RECEAINNA012");
			throw new Exception(context.getAdapterName() + " | sms error - " + e.getMessage());
		}

		nextFilter.filterWrite(session, new WriteRequest(m, writeRequest.getFuture(), writeRequest.getDestination()));
    }
	

	protected String getSessionState(IoSession session) {
		return (String) session.getAttribute(SESSION_STATE);
	}
	
	
	protected void setSessionState(IoSession session, String state) {
		session.setAttribute(SESSION_STATE, state);
	}

}
