/**
   * Deletes a group from the database
   *
   * @param group The group to be deleted
   */
  public void deleteGroupFromDatabase(Group group) {

    String where = GroupColumns._ID + " = '" + group.getId() + "'";
    context.getContentResolver().delete(GroupProvider.CONTENT_URI, where, null);
    context.getContentResolver().delete(UserGroupProvider.CONTENT_URI, where, null);
    Log.i("USER GROUP MANAGER", "Group: " + group.toString() + " deleted from database");
  }
  /**
   * Add a group to the database
   *
   * @param group The group to be added
   */
  public void addGroupToGroupDatabase(Group group) {
    ContentValues values = new ContentValues();

    values.put(GroupColumns._ID, group.getId());
    values.put(GroupColumns.GROUP_NAME, group.getName());

    context.getContentResolver().insert(GroupColumns.CONTENT_URI, values);

    Log.i("USER GROUP MANAGER", "Group: " + group.getName() + " added to the database");
  }
  public ArrayList<Experience> LoadAllSharedExperiencesOnGroupFromDatabase(Group group) {

    ArrayList<Experience> allExperiences = new ArrayList<Experience>();

    String[] experienceTableColumns =
        new String[] {
          ExperienceColumns._ID,
          ExperienceColumns.EXPERIENCE_NAME,
          ExperienceColumns.EXPERIENCE_SHARED,
          ExperienceColumns.EXPERIENCE_CREATOR,
          ExperienceColumns.EXPERIENCE_SHARED_WITH
        };

    whereStatement =
        ExperienceColumns.EXPERIENCE_SHARED
            + "='1' AND "
            + ExperienceColumns.EXPERIENCE_SHARED_WITH
            + "='"
            + group.getId()
            + "'";

    Cursor c =
        context
            .getContentResolver()
            .query(
                ExperienceColumns.CONTENT_URI, experienceTableColumns, whereStatement, null, null);

    if (c.moveToFirst()) {
      do {
        Experience experience =
            new Experience(
                c.getString(c.getColumnIndex(ExperienceColumns._ID)),
                c.getString(c.getColumnIndex(ExperienceColumns.EXPERIENCE_NAME)),
                (c.getInt((c.getColumnIndex(ExperienceColumns.EXPERIENCE_SHARED))) == 1)
                    ? true
                    : false,
                new Account(
                    c.getString(c.getColumnIndex(ExperienceColumns.EXPERIENCE_CREATOR)),
                    "com.google"));

        if (experience.isShared()) {
          experience.setSharingGroupObject(
              getGroupSharedWithExperience(
                  c.getString(c.getColumnIndex(ExperienceColumns.EXPERIENCE_SHARED_WITH))));
        }

        allExperiences.add(experience);
      } while (c.moveToNext());
    }
    c.close();

    Log.i("CONTENTLOADER - EXPERIENCES", allExperiences.size() + " experiences loaded");

    return allExperiences;
  }
  /**
   * Checks if a user already is in a group
   *
   * @param group The group to be checked
   * @param user The user to be checked
   * @return True if the user is in the group
   */
  private boolean userExistsInGroup(Group group, User user) {

    ArrayList<Group> userConnectedGroups = getAllGroupsConnectedToAUser(user);
    System.out.println("Number of connected Groups :" + userConnectedGroups.size());
    for (Group g : userConnectedGroups) {
      if (g.getId().equals(group)) {
        return true;
      }
    }

    return false;
  }
  /**
   * Add a user to a group in the database
   *
   * @param group The group the user should be added to
   * @param user The user to be added
   */
  public void addUserToAGroupInTheDatabase(Group group, User user) {
    ContentValues values = new ContentValues();
    values.put(GroupColumns._ID, group.getId());
    values.put(UserColumns.USER_NAME, user.getUserName());

    System.out.println(userExistsInGroup(group, user));
    if (!userExistsInGroup(group, user)) {
      context.getContentResolver().insert(UserGroupProvider.CONTENT_URI, values);
    }
    Log.i(
        "USER GROUP MANAGER",
        "User: "******" added to group: " + group.getName());
  }
  /**
   * Removes a user from a group in the database
   *
   * @param group The group the user should be removed from
   * @param user The user to be removed
   */
  public void removeUserFromAGroupInTheDatabase(Group group, User user) {

    String where =
        GroupColumns._ID
            + " = '"
            + group.getId()
            + "'"
            + " AND "
            + UserColumns.USER_NAME
            + " = '"
            + user.getUserName()
            + "'";
    context.getContentResolver().delete(UserGroupProvider.CONTENT_URI, where, null);

    Log.i(
        "USER GROUP MANAGER",
        "User: "******" removed from group: " + group.getName());
  }
  /**
   * Gets all users in a group
   *
   * @param group The group to be checked
   * @return A list of all the users connected to the group
   */
  private ArrayList<User> getUsersConnectedToAGroup(Group group) {

    String[] userGroupsTableColumns = new String[] {GroupColumns._ID, UserColumns.USER_NAME};
    ArrayList<User> users = new ArrayList<User>();

    String where = GroupColumns._ID + " = '" + group.getId() + "'";

    Cursor c =
        context
            .getContentResolver()
            .query(UserGroupProvider.CONTENT_URI, userGroupsTableColumns, where, null, null);

    if (c.moveToFirst()) {
      do {
        users.add(new User(c.getString(c.getColumnIndex(UserColumns.USER_NAME))));
      } while (c.moveToNext());
    }
    c.close();
    return users;
  }