package com.nokia.sms.sgip.filter;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;
import org.apache.mina.filter.logging.LogLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SGIPLoggingFilter extends IoFilterAdapter {
    private final String name;
    private final Logger logger;
    private LogLevel exceptionCaughtLevel = LogLevel.WARN;
    private LogLevel messageSentLevel = LogLevel.INFO;
    private LogLevel messageReceivedLevel = LogLevel.INFO;
    private LogLevel sessionCreatedLevel = LogLevel.INFO;
    private LogLevel sessionOpenedLevel = LogLevel.INFO;
    private LogLevel sessionIdleLevel = LogLevel.INFO;
    private LogLevel sessionClosedLevel = LogLevel.INFO;

    public SGIPLoggingFilter() {
        this(SGIPLoggingFilter.class.getName());
    }

    public SGIPLoggingFilter(Class<?> clazz) {
        this(clazz.getName());
    }

    public SGIPLoggingFilter(String name) {
        if (name == null) {
            this.name = SGIPLoggingFilter.class.getName();
        } else {
            this.name = name;
        }
        this.logger = LoggerFactory.getLogger(this.name);
    }

    public String getName() {
        return this.name;
    }

  private void log(LogLevel eventLevel, String message, Throwable cause)
  {
    switch (eventLevel.ordinal())
    {
    case 1: 
      this.logger.trace(message, cause);return;
    case 2: 
      this.logger.debug(message, cause);return;
    case 3: 
      this.logger.info(message, cause);return;
    case 4: 
      this.logger.warn(message, cause);return;
    case 5: 
      this.logger.error(message, cause);return;
    }
  }

  private void log(LogLevel eventLevel, String message, Object param)
  {
    switch (eventLevel.ordinal())
    {
    case 1: 
      this.logger.trace(message, param);return;
    case 2: 
      this.logger.debug(message, param);return;
    case 3: 
      this.logger.info(message, param);return;
    case 4: 
      this.logger.warn(message, param);return;
    case 5: 
      this.logger.error(message, param);return;
    }
  }

  private void log(LogLevel eventLevel, String message)
  {
    switch (eventLevel.ordinal())
    {
    case 1: 
      this.logger.trace(message);return;
    case 2: 
      this.logger.debug(message);return;
    case 3: 
      this.logger.info(message);return;
    case 4: 
      this.logger.warn(message);return;
    case 5: 
      this.logger.error(message);return;
    }
  }

    public void exceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, Throwable cause)
            throws Exception {
        log(this.exceptionCaughtLevel, "EXCEPTION :", cause);
        nextFilter.exceptionCaught(session, cause);
    }

    public void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message)
            throws Exception {
        if (IoBuffer.class.isAssignableFrom(message.getClass())) {
            IoBuffer buffer = (IoBuffer) message;
            int oldPosition = buffer.position();
            int lengthLimit = buffer.getInt();
            buffer.position(oldPosition);
            log(this.messageReceivedLevel, "RECEIVED: {}", "HexDump[" + buffer.getHexDump(lengthLimit) + "]");
        } else {
            log(this.messageReceivedLevel, "RECEIVED: {}", message);
        }
        nextFilter.messageReceived(session, message);
    }

    public void messageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
            throws Exception {
        log(this.messageSentLevel, "SENT: {}", writeRequest.getMessage());
        nextFilter.messageSent(session, writeRequest);
    }

    public void sessionCreated(IoFilter.NextFilter nextFilter, IoSession session)
            throws Exception {
        log(this.sessionCreatedLevel, "CREATED " + session.getRemoteAddress());
        nextFilter.sessionCreated(session);
    }

    public void sessionOpened(IoFilter.NextFilter nextFilter, IoSession session)
            throws Exception {
        log(this.sessionOpenedLevel, "OPENED " + session.getRemoteAddress());
        nextFilter.sessionOpened(session);
    }

    public void sessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status)
            throws Exception {
        log(this.sessionIdleLevel, "IDLE");
        nextFilter.sessionIdle(session, status);
    }

    public void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session)
            throws Exception {
        log(this.sessionClosedLevel, "CLOSED " + session.getRemoteAddress());
        nextFilter.sessionClosed(session);
    }

    public void setExceptionCaughtLogLevel(LogLevel level) {
        this.exceptionCaughtLevel = level;
    }

    public LogLevel getExceptionCaughtLogLevel() {
        return this.exceptionCaughtLevel;
    }

    public void setMessageReceivedLogLevel(LogLevel level) {
        this.messageReceivedLevel = level;
    }

    public LogLevel getMessageReceivedLogLevel() {
        return this.messageReceivedLevel;
    }

    public void setMessageSentLogLevel(LogLevel level) {
        this.messageSentLevel = level;
    }

    public LogLevel getMessageSentLogLevel() {
        return this.messageSentLevel;
    }

    public void setSessionCreatedLogLevel(LogLevel level) {
        this.sessionCreatedLevel = level;
    }

    public LogLevel getSessionCreatedLogLevel() {
        return this.sessionCreatedLevel;
    }

    public void setSessionOpenedLogLevel(LogLevel level) {
        this.sessionOpenedLevel = level;
    }

    public LogLevel getSessionOpenedLogLevel() {
        return this.sessionOpenedLevel;
    }

    public void setSessionIdleLogLevel(LogLevel level) {
        this.sessionIdleLevel = level;
    }

    public LogLevel getSessionIdleLogLevel() {
        return this.sessionIdleLevel;
    }

    public void setSessionClosedLogLevel(LogLevel level) {
        this.sessionClosedLevel = level;
    }

    public LogLevel getSessionClosedLogLevel() {
        return this.sessionClosedLevel;
    }
}
