package io.galeb.router.services;

import io.galeb.router.Application;
import io.galeb.router.client.etcd.EtcdClient;
import io.galeb.router.client.etcd.EtcdGenericNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.zalando.boot.etcd.EtcdNode;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class ExternalData {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public static final String ROOT_KEY         = "/";
    public static final String PREFIX_KEY       = ROOT_KEY + Application.PREFIX;
    public static final String VIRTUALHOSTS_KEY = PREFIX_KEY + "/virtualhosts";
    public static final String POOLS_KEY        = PREFIX_KEY + "/pools";

    private final EtcdClient client;

    @Autowired
    public ExternalData(@Value("#{etcdClient}") final EtcdClient etcdClient) {
        this.client = etcdClient;
    }

    public EtcdClient client() {
        return client;
    }

    public List<EtcdNode> listFrom(String key) {
        return listFrom(key, false);
    }

    public List<EtcdNode> listFrom(String key, boolean recursive) {
        return listFrom(node(key, recursive));
    }

    public List<EtcdNode> listFrom(EtcdNode node) {
        return Optional.ofNullable(node.getNodes()).orElse(Collections.emptyList());
    }

    public EtcdNode node(String key) {
        return node(key, false);
    }

    public EtcdNode node(String key, boolean recursive) {
        return node(key, recursive, EtcdGenericNode.NULL);
    }

    public EtcdNode node(String key, EtcdGenericNode def) {
        return node(key, false, def);
    }

    public synchronized EtcdNode node(String key, boolean recursive, EtcdGenericNode def) {
        try {
            final EtcdNode node = client.get(key, recursive).getNode();
            return node != null ? node : def.get();
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getMessage());
            }
            return def.get();
        }
    }

    public synchronized boolean exist(String key) {
        final EtcdNode node = node(key);
        return node.getValue() != null || node.isDir();
    }

}

@@ -1928,38 +1928,32 @@
 y %7B%0A            
-final 
 EtcdNode node = 
@@ -1998,30 +1998,30 @@
             
-return
+node =
  node != nul
@@ -2054,104 +2054,224 @@
     
-%7D catch (Exception e) %7B%0A            if (logger.isDebugEnabled()) %7B%0A                logger.debug(
+    logger.info(%22GET %22 + key + %22: %22 +  %22EtcNode(value=%22 + node.getValue() + %22, dir=%22 + node.isDir() + %22)%22);%0A            return node;%0A        %7D catch (Exception e) %7B%0A            logger.warn(%22GET %22 + key + %22 FAIL: %22 + 
 e.ge
@@ -2287,22 +2287,8 @@
 ));%0A
-            %7D%0A
     

improve logging