// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String CREATED_AT = "created_at";
// 
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String REMOTE_ID = "remote_id";
// 
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String UPDATED_AT = "updated_at";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_ASSETS = "assets";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_ENTRY_TYPES = "entry_types";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_LINKS = "links";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_SYNC_INFO = "sync_info";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static String escape(String name) {
//   return String.format("`%s`", name);
// }
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static String localizeName(String name, String locale) {
//   return String.format("%s$%s", name, locale);
// }


import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import com.contentful.java.cda.CDAAsset;
import com.contentful.java.cda.CDAEntry;
import com.contentful.java.cda.CDAResource;
import com.contentful.java.cda.CDAType;
import com.contentful.java.cda.LocalizedResource;
import com.contentful.java.cda.SynchronizedSpace;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import okhttp3.HttpUrl;
import static android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE;
import static com.contentful.java.cda.CDAType.ASSET;
import static com.contentful.java.cda.CDAType.ENTRY;
import static com.contentful.vault.BaseFields.CREATED_AT;
import static com.contentful.vault.BaseFields.REMOTE_ID;
import static com.contentful.vault.BaseFields.UPDATED_AT;
import static com.contentful.vault.Sql.TABLE_ASSETS;
import static com.contentful.vault.Sql.TABLE_ENTRY_TYPES;
import static com.contentful.vault.Sql.TABLE_LINKS;
import static com.contentful.vault.Sql.TABLE_SYNC_INFO;
import static com.contentful.vault.Sql.escape;
import static com.contentful.vault.Sql.localizeName;
  private void saveLink(String parentId, String fieldId, String linkType, String targetId,
                        int position, String locale) {
    AutoEscapeValues values = new AutoEscapeValues();

    values.put("parent", parentId);
    values.put("field", fieldId);
    values.put("child", targetId);
    values.put("position", position);
    values.put("is_asset", CDAType.valueOf(linkType.toUpperCase(Vault.LOCALE)) == ASSET);

    db.insertWithOnConflict(escape(localizeName(TABLE_LINKS, locale)), null, values.get(),
        CONFLICT_REPLACE);
  }

  private void deleteResourceLinks(String parentId, String field) {
    for (String locale : spaceHelper.getLocales()) {
      deleteResourceLinks(parentId, field, locale);
    }
  }

  private void deleteResourceLinks(String parentId, String field, String locale) {
    String where = "parent = ? AND field = ?";
    String[] args = new String[]{parentId, field};

    db.delete(escape(localizeName(TABLE_LINKS, locale)), where, args);
  }

  private static void putResourceFields(CDAResource resource, AutoEscapeValues values) {
    values.put(REMOTE_ID, resource.id());
    values.put(CREATED_AT, (String) resource.getAttribute("createdAt"));
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String CREATED_AT = "created_at";
// 
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String REMOTE_ID = "remote_id";
// 
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String UPDATED_AT = "updated_at";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_ASSETS = "assets";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_ENTRY_TYPES = "entry_types";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_LINKS = "links";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_SYNC_INFO = "sync_info";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static String escape(String name) {
//   return String.format("`%s`", name);
// }
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static String localizeName(String name, String locale) {
//   return String.format("%s$%s", name, locale);
// }


// Path: core/src/main/java/com/contentful/vault/SyncRunnable.java
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import com.contentful.java.cda.CDAAsset;
import com.contentful.java.cda.CDAEntry;
import com.contentful.java.cda.CDAResource;
import com.contentful.java.cda.CDAType;
import com.contentful.java.cda.LocalizedResource;
import com.contentful.java.cda.SynchronizedSpace;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import okhttp3.HttpUrl;
import static android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE;
import static com.contentful.java.cda.CDAType.ASSET;
import static com.contentful.java.cda.CDAType.ENTRY;
import static com.contentful.vault.BaseFields.CREATED_AT;
import static com.contentful.vault.BaseFields.REMOTE_ID;
import static com.contentful.vault.BaseFields.UPDATED_AT;
import static com.contentful.vault.Sql.TABLE_ASSETS;
import static com.contentful.vault.Sql.TABLE_ENTRY_TYPES;
import static com.contentful.vault.Sql.TABLE_LINKS;
import static com.contentful.vault.Sql.TABLE_SYNC_INFO;
import static com.contentful.vault.Sql.escape;
import static com.contentful.vault.Sql.localizeName;

  private void saveLink(String parentId, String fieldId, String linkType, String targetId,
                        int position, String locale) {
    AutoEscapeValues values = new AutoEscapeValues();

    values.put("parent", parentId);
    values.put("field", fieldId);
    values.put("child", targetId);
    values.put("position", position);
    values.put("is_asset", CDAType.valueOf(linkType.toUpperCase(Vault.LOCALE)) == ASSET);

    db.insertWithOnConflict(escape(localizeName(TABLE_LINKS, locale)), null, values.get(),
        CONFLICT_REPLACE);
  }

  private void deleteResourceLinks(String parentId, String field) {
    for (String locale : spaceHelper.getLocales()) {
      deleteResourceLinks(parentId, field, locale);
    }
  }

  private void deleteResourceLinks(String parentId, String field, String locale) {
    String where = "parent = ? AND field = ?";
    String[] args = new String[]{parentId, field};

    db.delete(escape(localizeName(TABLE_LINKS, locale)), where, args);
  }

  private static void putResourceFields(CDAResource resource, AutoEscapeValues values) {
    values.put(REMOTE_ID, resource.id());
    values.put(CREATED_AT, (String) resource.getAttribute("createdAt"));
    values.put(UPDATED_AT, (String) resource.getAttribute("updatedAt"));