var Event = require('../event/Event.js').Event;
var ErrorEvent = require('../event/ErrorEvent.js').ErrorEvent;
var EventDispatcher = require('../event/EventDispatcher.js').EventDispatcher; 
var Class = require('../class/Class.js').Class;
var net = require('net');

var Socket = require('./Socket.js').Socket;

var ConnectionPool = function(){

    this.Extends = EventDispatcher;

    /*
    this._sockets = [];
    this._callbacks = [];
    this._options = null;
    */
    
    this.initialize = function(options){
        this.parent();

        this._sockets = [];
        this._callbacks = [];
        this._options = options;
        this._connect(); 
    };
        
    this._connect = function(){
        var that = this;
        for(var i=0;i<this._options.connections;i++){
            var socket = new Socket(this._options.port, this._options.host);
            console.info('ConnectionPool: Created socket [' + i + ']. Connecting...');
            this._refineSocketHandlers(socket);
            socket.on('connect', function(){
                console.info('ConnectionPool: Socket connected: ' + socket._host + ':' + socket._port);
                if(that._callbacks.length > 0){
                    var cb = that._callbacks.shift();
                    cb(socket);
                }
            });
            this._sockets.push(socket);
        }
    };

    this.givebackSocket = function(socket){
        var self = this;

        if(socket.bufferSize() == 0){
            this._givebackSocket(socket);
        }else{
            socket.on('drain', function(){
                self._givebackSocket(socket);
            });
        }
    };

    this._givebackSocket = function(socket){
        this._refineSocketHandlers(socket);
        this._sockets.push(socket);

        if(this._sockets.length>0 &&  this._callbacks.length > 0){
            socket = this._sockets.shift();
            var cb = this._callbacks.shift();
            cb(socket);
        }
    }

    this.getSocket = function(cb){
        if(this._sockets.length>0){
            var socket = this._sockets.shift();
            cb(socket);
        } else {
            console.error('emerg','ConnectionPool: there is no socket avalible. Please increase "connections" variable!');
            this._callbacks.push(cb);
        }
        
        // obsluz pozostale callbacki
        var socketStale, cbx;
        while(this._sockets.length > 0 && this._callbacks.length > 0){
            socketStale = this._sockets.shift();
            cbx = this._callbacks.shift();
            cbx(socketStale);
        }
        
    };
    
    this._refineSocketHandlers = function(socket){
        socket._refineHandlers();
    };
};

ConnectionPool = new Class(new ConnectionPool());
exports.ConnectionPool = ConnectionPool;
