package com.groupbyinc.api;

import com.groupbyinc.api.config.ConnectionConfiguration;
import com.groupbyinc.common.apache.http.Header;
import com.groupbyinc.common.apache.http.message.BasicHeader;

import java.util.Iterator;

/**
 * @author Ben Teichman
 */
public class CloudBridge extends AbstractBridge {

  private static final String DOT = ".";
  private static final String CLOUD_HOST = "groupbycloud.com";
  private static final int CLOUD_PORT = 443;
  private static final String CLOUD_PATH = "/api/v1";
  private static final String URL_SUFFIX = DOT + CLOUD_HOST + COLON + CLOUD_PORT + CLOUD_PATH;

  /**
   * <code>
   * Constructor to create a bridge object that connects to the search api.
   *
   * JSON Reference:
   * The key as found in your key management page in the command center
   *
   *     {"clientKey": "--clientKey--"}
   *
   * </code>
   *
   * @param clientKey
   *         The key as found in your key management page in the command
   *         center.
   * @param customerId
   *         The name of your subdomain.  For example, https://--customerId--.groupbycloud.com
   */
  public CloudBridge(String clientKey, String customerId) {
    super(clientKey, HTTPS + customerId + URL_SUFFIX);
  }

  /**
   * <code>
   * Constructor to create a bridge object that connects to the search api.
   *
   * JSON Reference:
   * The key as found in your key management page in the command center
   *
   *     {"clientKey": "--clientKey--"}
   *
   * </code>
   *
   * @param clientKey
   *         The key as found in your key management page in the command
   *         center.
   * @param customerId
   *         The name of your subdomain.  For example, https://--customerId--.groupbycloud.com
   * @param config
   *         Configuration for the underlying HttpClient instance.
   */
  public CloudBridge(String clientKey, String customerId, ConnectionConfiguration config) {
    super(clientKey, HTTPS + customerId + URL_SUFFIX, config);
  }

  /**
   * <code>
   * If using the semantic layer, this tells the semantic layer to not cache the response.
   * By default caching is on when using the semantic layer.
   *
   * Currently this is undocumented until the semantic layer is globally enabled.
   *
   * @param cachingEnabled
   *         Turn off caching by setting this to false.
   *
   * @internal
   */
  public void setCachingEnabled(boolean cachingEnabled) {
    if (containsSkipCachingHeader()) {
      if (cachingEnabled) {
        removeSkipCachingHeader();
      }
    } else {
      if (!cachingEnabled) {
        addSkipCachingHeader();
      }
    }
  }

  private boolean containsSkipCachingHeader() {
    for (Header header : getHeaders()) {
      if (header.getName()
          .equalsIgnoreCase("Skip-Caching")) {
        return true;
      }
    }
    return false;
  }

  private void removeSkipCachingHeader() {
    Iterator<Header> iterator = getHeaders().iterator();
    while (iterator.hasNext()) {
      Header header = iterator.next();
      if (header.getName()
          .equalsIgnoreCase("Skip-Caching")) {
        iterator.remove();
      }
    }
  }

  private void addSkipCachingHeader() {
    getHeaders().add(new BasicHeader("Skip-Caching", "true"));
  }
}

@@ -680,34 +680,34 @@
 ects to the 
-s
+S
 earch 
-api
+API
 .%0A   *%0A   * 
@@ -744,37 +744,37 @@
 s found in your 
-key m
+Key M
 anagement page i
@@ -914,37 +914,37 @@
 s found in your 
-key m
+Key M
 anagement page i
@@ -941,33 +941,33 @@
 ent page in the 
-c
+C
 ommand%0A   *     
@@ -962,33 +962,33 @@
 nd%0A   *         
-c
+C
 enter.%0A   * @par
@@ -1047,32 +1047,61 @@
 n.  For example,
+ if your Command Center is at
  https://--custo
@@ -1120,24 +1120,55 @@
 pbycloud.com
+, this should be --customerId--
 %0A   */%0A  pub
@@ -1424,37 +1424,37 @@
 s found in your 
-key m
+Key M
 anagement page i
@@ -1606,13 +1606,13 @@
 our 
-key m
+Key M
 anag
@@ -1625,25 +1625,25 @@
 page in the 
-c
+C
 ommand%0A   * 
@@ -1650,17 +1650,17 @@
         
-c
+C
 enter.%0A 
@@ -1723,28 +1723,86 @@
 omain.  
-For example,
+The name of your subdomain.  For example, if your Command Center at is
  https:/
@@ -1833,16 +1833,47 @@
 loud.com
+, this should be --customerId--
 %0A   * @p

Clarify wording for client key / id