package com.eactive.eai.adapter.socket.service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

import com.eactive.eai.adapter.socket.common.CommonLib;

public class SessionManager {

	private static SessionManager   instance 	= new SessionManager();
	private HashMap<String, SessionChannel> pool      = new HashMap<String, SessionChannel>();
//	private Logger                  logger;

	public SessionManager() {
//        logger  = SocketLogManager.getInstance().getLogger();
    }

	public static SessionManager getInstance() {
		return instance;
	}

	public void stop() {
        this.removeAll();
    }

    public void removeAll() {
        pool.clear();
    }

    public void start(String adapterGroupName, SessionChannel channel) {
        this.addSessionGroup(adapterGroupName, channel);
    }

    // Adapter Group Session을 한번에 등록한다.
    public synchronized void addSessionGroup(String adapterGroupName, SessionChannel channel) {
        Object o = pool.get( adapterGroupName );
        if ( o == null ) {
            synchronized ( pool ) {
              o =  pool.get( adapterGroupName );
              if ( o == null ) {
                pool.put( adapterGroupName, channel );
              }
            }
        }
    }

    public void addSession(String adapterGroupName, SessionService session) {
        SessionChannel  channel = this.getChannel( adapterGroupName );
        if ( channel == null ) return;
        try {
            channel.addSession( session );
        } catch( Exception e ) {}
    }

    public void removeSession(String adapterGroupName, Object session) {
        SessionChannel  channel = this.getChannel( adapterGroupName );
        if ( channel == null ) return;
        try {
            channel.removeSession( session );
        } catch( Exception e ) {}
    }

    public void stop(String adapterGroupName) {
        // adapterGroupName의 session stop & clear
        pool.remove( adapterGroupName );
    }

    public SessionChannel getChannel( String adapterGroupName ) {
    	return (SessionChannel) pool.get( adapterGroupName );
    }

    public boolean hasChannel( String adapterGroupName ) {
    	return pool.containsKey( adapterGroupName );
    }

    public SessionService getSession( String adapterGroupName ) {
        SessionChannel  channel = this.getChannel( adapterGroupName );
        if ( channel == null ) return null;

        SessionService  service = null;
        try {
            service = (SessionService) channel.getSession();
        } catch ( Exception e ) { }

        return service;
    }

    public String getSessionInfo() {
        StringBuffer    strBuff = new StringBuffer();
        Object[] adapterGroupList = pool.keySet().toArray();
        if ( adapterGroupList == null || adapterGroupList.length < 1 ) {
            //strBuff.append("This server don't have any session infomrations");
            strBuff.append( CommonLib.getMessage("BWCEAIMSA006") );
            return strBuff.toString();
        }
        Arrays.sort( adapterGroupList );
        for( int i=0; i < adapterGroupList.length; i++ ) {
            strBuff.append( this.getSessionInfo( (String)adapterGroupList[i] )).append("\n");
        }

        return strBuff.toString();
    }

    public String getSessionInfo( String adapterGroupName ) {
        StringBuffer    strBuff = new StringBuffer();
        SessionChannel  channel = this.getChannel( adapterGroupName );
        if ( channel == null ) {
            //strBuff.append("Session not found for ").append(adapterGroupName).append(" Adapter Group ");
            strBuff.append( CommonLib.getMessage("BWCEAIMSA007", new String[]{ adapterGroupName }) );
            return strBuff.toString();
        }

        return strBuff.append(adapterGroupName).append("-").append( channel.toString() ).toString();
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append("\n■ SessionManager : ");
        for(Iterator<String> iter = pool.keySet().iterator(); iter.hasNext();) {
            Object key = iter.next();
            sb.append("\n    ▣ " + key + " : " + pool.get(key));
        }

        return sb.toString();
    }
}
