package com.orhanobut.hawk;

public class DefaultHawkFacade implements HawkFacade {

  private final Storage storage;
  private final Converter converter;
  private final Encryption encryption;
  private final Serializer serializer;
  private final LogInterceptor logInterceptor;

  public DefaultHawkFacade(HawkBuilder builder) {
    encryption = builder.getEncryption();
    storage = builder.getStorage();
    converter = builder.getConverter();
    serializer = builder.getSerializer();
    logInterceptor = builder.getLogInterceptor();
  }

  @Override public <T> boolean put(String key, T value) {
    // Validate
    HawkUtils.checkNull("Key", key);
    log("Hawk.put -> key: " + key + ", value: " + value);

    // If the value is null, delete it
    if (value == null) {
      log("Hawk.put -> Value is null. Any existing value will be deleted with the given key");
      return delete(key);
    }

    // 1. Convert to text
    String plainText = converter.toString(value);
    log("Hawk.put -> " + value + " is converted to " + plainText);
    if (plainText == null) {
      log("Hawk.put -> converter failed to convert " + value);
      return false;
    }

    // 2. Encrypt the text
    String cipherText = null;
    try {
      cipherText = encryption.encrypt(key, plainText);
      log("Hawk.put -> Converted value is encrypted to  " + cipherText);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (cipherText == null) {
      log("Hawk.put -> Encryption failed");
      return false;
    }

    // 3. Serialize the given object along with the cipher text
    String serializedText = serializer.serialize(cipherText, value);
    log("Hawk.put -> Cipher text is encoded to " + serializedText);
    if (serializedText == null) {
      log("Hawk.put -> Serialization failed");
      return false;
    }

    // 4. Save to the storage
    if (storage.put(key, serializedText)) {
      log("Hawk.put -> Serialized text is stored successfully");
      return true;
    } else {
      log("Hawk.put -> Store operation failed");
      return false;
    }
  }

  @Override public <T> T get(String key) {
    log("Hawk.get -> key: " + key);
    if (key == null) {
      log("Hawk.get -> null key, returning null value ");
      return null;
    }

    // 1. Get serialized text from the storage
    String serializedText = storage.get(key);
    log("Hawk.get -> Fetched from storage, serialized text : " + serializedText);
    if (serializedText == null) {
      log("Hawk.get -> Fetching from storage failed");
      return null;
    }

    // 2. Deserialize
    DataInfo dataInfo = serializer.deserialize(serializedText);
    log("Hawk.get -> Deserializing the fetched data");
    if (dataInfo == null) {
      log("Hawk.get -> Deserialization failed");
      return null;
    }

    // 3. Decrypt
    String plainText = null;
    try {
      plainText = encryption.decrypt(key, dataInfo.cipherText);
      log("Hawk.get -> Decrypting.... : " + plainText);
    } catch (Exception e) {
      log("Hawk.get -> Decrypt failed: " + e.getMessage());
    }
    if (plainText == null) {
      log("Hawk.get -> Decrypt failed");
      return null;
    }

    // 4. Convert the text to original data along with original type
    T result = null;
    try {
      result = converter.fromString(plainText, dataInfo);
      log("Hawk.get -> Converting...    : " + result);
    } catch (Exception e) {
      log("Hawk.get -> Converter failed");
    }

    return result;
  }

  @Override public <T> T get(String key, T defaultValue) {
    T t = get(key);
    if (t == null) return defaultValue;
    return t;
  }

  @Override public long count() {
    return storage.count();
  }

  @Override public boolean deleteAll() {
    return storage.deleteAll();
  }

  @Override public boolean delete(String key) {
    return storage.delete(key);
  }

  @Override public boolean contains(String key) {
    return storage.contains(key);
  }

  @Override public boolean isBuilt() {
    return true;
  }

  @Override public void destroy() {
  }

  private void log(String message) {
    logInterceptor.onLog(message);
  }
}

@@ -533,16 +533,113 @@
 eptor();
+%0A%0A    logInterceptor.onLog(%22Hawk.init -%3E Encryption : %22 + encryption.getClass().getSimpleName());
 %0A  %7D%0A%0A  
@@ -1099,26 +1099,9 @@
  -%3E 
-%22 + value + %22 is c
+C
 onve
@@ -1176,17 +1176,17 @@
 .put -%3E 
-c
+C
 onverter
@@ -1196,29 +1196,9 @@
 iled
- to convert %22 + value
+%22
 );%0A 
@@ -1372,28 +1372,9 @@
  -%3E 
-Converted value is e
+E
 ncry
@@ -1719,34 +1719,21 @@
  -%3E 
-Cipher text is encod
+Serializ
 ed to
- 
 %22 + 
@@ -1959,27 +1959,8 @@
 -%3E S
-erialized text is s
 tore
@@ -2413,25 +2413,8 @@
 rage
-, serialized text
  : %22
@@ -2669,28 +2669,10 @@
 aliz
-ing the fetched data
+ed
 %22);%0A
@@ -2929,15 +2929,13 @@
 rypt
-ing....
+ed to
  : %22
@@ -3331,17 +3331,13 @@
 vert
-ing...   
+ed to
  : %22

Improve log messages