/** @param args */
  public static void main(String[] args) {

    APIClient apiClient = null;

    // 1. Build the api client

    try {

      Properties properties = loadProperties();

      PUBLISH_KEY = properties.getProperty(PROP_PUBLISH_KEY);
      PUBLISH_SECRET = properties.getProperty(PROP_PUBLISH_SECRET);

      apiClient =
          new APIClient(properties, PUBLISH_KEY, PUBLISH_SECRET, RequestStrategy.URL_SIGNATURE);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    // 2. Publish

    try {
      StringBuffer text = getText(args);
      int id = sendPush(text.toString(), "android", 120, apiClient);
      logger.debug("Push sent with id " + id);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    // 3. List tags
    try {
      List<Tag> tags = getTags(apiClient);
      logger.debug("Available tags on SaaS: ");
      for (Tag tag : tags) {
        logger.debug(
            "Tag "
                + tag.getName()
                + " - id "
                + tag.getId()
                + " - created at : "
                + tag.getCreatedAt()
                + " - devices: "
                + tag.getDevices()
                + " - deprecated: "
                + tag.isDeprecated());
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    // 4. Create, get, deprecate and remove a tag
    try {
      String tagName = "api-test";
      int id = createTag(tagName, apiClient);
      logger.debug("Tag " + tagName + " created with id " + id);
      Tag tag = getTag(id, apiClient);
      logger.debug(
          "Tag "
              + tag.getName()
              + " - id "
              + tag.getId()
              + " - created at : "
              + tag.getCreatedAt()
              + " - devices: "
              + tag.getDevices()
              + " - deprecated: "
              + tag.isDeprecated());
      deprecateTag(id, apiClient);
      logger.debug("Tag with id " + id + " was deprecated");
      tag = getTag(id, apiClient);
      logger.debug(
          "Tag "
              + tag.getName()
              + " - id "
              + tag.getId()
              + " - created at : "
              + tag.getCreatedAt()
              + " - devices: "
              + tag.getDevices()
              + " - deprecated: "
              + tag.isDeprecated());
      deleteTag(id, apiClient);
      logger.debug("Tag with id " + id + " was removed");
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    // 5. Schedule a push
    try {
      Calendar later = (Calendar) Calendar.getInstance().clone();
      later.add(Calendar.MINUTE, 15);
      Date scheduledAt = later.getTime();
      Alert<String> alert =
          buildScheduledPush("This is a scheduled push", scheduledAt, "android", 120);

      int id = apiClient.publish(alert);
      logger.debug("Push scheduled with id " + id);

      // 6. Get push info
      logger.debug("Get push info for id " + id);
      try {
        PushInfo info = apiClient.getPush(id);
        logger.debug("Info about recently created push: " + info);
      } catch (Exception e) {
        e.printStackTrace();
      }
      ;

      // 7. List all scheduled pushes
      logger.debug("List all scheduled pushes");
      try {
        ListPage<PushInfo> page;
        boolean eol = false;
        int offset = 0;
        while (!eol) {
          page = apiClient.getScheduledPushes(offset);
          for (PushInfo pushInfo : page.getItems()) {
            logger.debug(pushInfo);
          }
          offset += page.getItems().size();
          eol = page.getItems().size() < page.getPageSize();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      ;

      // 7. Update push
      logger.debug("Update scheduled push with id " + id);
      // Delay alert 15 minutes more
      later.add(Calendar.MINUTE, 15);
      alert.setScheduledAt(later.getTime());
      // And change text
      alert.setAlert("This is a nice scheduled push");
      apiClient.updatePush(id, alert);

      // 8. Remove the push we have just scheduled
      deleteScheduledPush(id, apiClient);
      logger.debug("Push scheduled with id " + id + " was removed");
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }