var Class = require("../class/Class.js").Class;
var CredentialsManager = require("../credentials/Manager.js").CredentialsManager;
var EventDispatcher = require('../event/EventDispatcher.js').EventDispatcher;
var Event = require("../event/Event.js").Event;
var ErrorEvent = require("../event/ErrorEvent.js").ErrorEvent;

//tutaj trzeba poprawic jak zrobimy pullr
//var ZK = require("kuebk-zookeeper").ZooKeeper;

var ZooKeeper = function () {
    this.Extends = EventDispatcher;

    /*
    this._options = {};
    this._retryCount = 0;
    this._retryCountMax = 10;
    this._cm = null;

    this._zooHandle = null;
    */

    this.initialize = function (options){
        this.parent();
        
        this._retryCount = 0;
        this._retryCountMax = 10;
        this._cm = null;
        this._options = options;

        var that = this;
        
        this._cm = CredentialsManager.factory();
        this._cm.addEventListener(CredentialsManager.Event.LOADED, this._options.credentialKey, function (data) {
            var zoo = new ZK(), i, hosts = data.data.hosts, hostsList = hosts[0].host + ":" + hosts[0].port;

            that._retryCount = 0;

            zoo.on(ZK.on_closed, function () {
                console.info("ZOOKEEPER: reconnecting");
                that._reconnect();
            });
            zoo.on(ZK.on_connecting, function () {
                console.warn("ZOOKEEPER: connection lost");
                that._zooHandle = null;
                zoo.close();
            });
            zoo.on(ZK.on_connected, function (zk) {
                console.info("ZOOKEEPER: connected");
                that._zooHandle = zk;
                that.dispatchEvent(new Event(ZooKeeper.Event.READY, that._zooHandle));
            });

            for (i = 1, l = hosts.length; i < l; i++) {
                hostsList += "," + hosts[i].host + ":" + hosts[i].port;
            }

            console.info("ZOOKEEPER: connecting to: %s", hostsList);
            zoo.init({
                connect: hostsList,
                timeout: (that._options.timeout || 20000),
                debug_level: 0, //ZK.ZOO_LOG_LEVEL_DEBUG,
                hosts_order_deterministic: false,
                data_as_buffer: false
            });
        });

        this._cm.addEventListener(CredentialsManager.Event.ERROR, this._options.credentialKey, this._reconnect, this);
    };

    this.connect = function(){
        var that = this;

        console.log('ZOOKEEPER: getting credentials...');
        this._cm.getCredential(this._options.credentialKey);
    };

    this._reconnect = function(){
        this._retryCount++;
        console.log('ZOOKEEPER: reconnecting: ' + this._retryCount);
        if (this._retryCount > (this._options.retryCountMax || this._retryCountMax)) {
            this._retryCount = 0;
            this.dispatchEvent(new ErrorEvent(ZooKeeper.Event.ERROR));
            return;
        } else {
            var that = this;
            setTimeout(function () {
                console.log('ZOOKEEPER: getting credentials...');
                that._cm.renewCredential(that._options.credentialKey);
            }, 1000);
        }
    };
};
ZooKeeper = new Class(new ZooKeeper());

ZooKeeper.Event = {};
ZooKeeper.Event.READY = "ZooKeeper.Event.READY";
ZooKeeper.Event.ERROR = "ZooKeeper.Event.ERROR";

exports.ZooKeeper = ZooKeeper;
