/**
   * Load all the Leaderboards for the Game. Warning: If using iOS5 or less the Leaderboard object
   * will only include the Category (identifier)
   */
  public void loadLeaderboards() {
    // If player is not authenticated, do nothing
    if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
      listener.leaderboardsLoadFailed();
      return;
    }

    // If iOS version is 6 or more we use the new method
    if (getIosVersion() >= IOS_6) {
      GKLeaderboard.loadLeaderboards(
          new VoidNSArrayNSErrorBlock() {
            @SuppressWarnings({"rawtypes", "unchecked"})
            @Override
            public void invoke(NSArray array, NSError error) {
              if (error != null) {
                listener.leaderboardsLoadFailed();
              } else {
                ArrayList<GKLeaderboard> leaderboards = new ArrayList<GKLeaderboard>();
                for (NSObject leaderboard : (NSArray<NSObject>) array) {
                  leaderboards.add((GKLeaderboard) leaderboard);
                }
                listener.leaderboardsLoadCompleted(leaderboards);
              }
            }
          });
    } else { // If iOS version is 6 or less we use the deprecated method
      GKLeaderboard.loadCategories(
          new VoidNSArrayNSArrayNSErrorBlock() {
            @SuppressWarnings({"rawtypes", "unchecked"})
            @Override
            public void invoke(NSArray array, NSArray array2, NSError error) {
              if (error != null) {
                listener.leaderboardsLoadFailed();
              } else {
                ArrayList<GKLeaderboard> leaderboards = new ArrayList<GKLeaderboard>();
                for (NSObject category : (NSArray<NSObject>) array) {
                  GKLeaderboard leaderboard = new GKLeaderboard();
                  leaderboard.setCategory(((NSString) category).toString());

                  leaderboards.add(leaderboard);
                }
                listener.leaderboardsLoadCompleted(leaderboards);
              }
            }
          });
    }
  }
  /**
   * Reset the achievements progress for the local player. All the entries for the local player are
   * removed from the server.
   */
  public void resetAchievements() {
    // If player is not authenticated, do nothing
    if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
      listener.achievementsResetFailed();
      return;
    }

    GKAchievement.resetAchievements(
        new VoidNSErrorBlock() {
          @Override
          public void invoke(NSError error) {
            if (error != null) {
              listener.achievementsResetFailed();
            } else {
              listener.achievementsResetCompleted();
            }
          }
        });
  }
  /**
   * Report an achievement with a percentComplete
   *
   * @param identifier
   * @param percentComplete
   */
  public void reportAchievement(String identifier, double percentComplete) {
    // If player is not authenticated, do nothing
    if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
      listener.achievementReportFailed();
      return;
    }

    GKAchievement achievement = new GKAchievement(identifier);
    achievement.setPercentComplete(percentComplete);
    achievement.setShowsCompletionBanner(true);

    // If iOS version is 6 or more we use the new method
    if (getIosVersion() >= IOS_6) {
      // Create an array with the achievement
      NSArray<NSObject> achievements = new NSArray<NSObject>(achievement);

      GKAchievement.reportAchievements(
          achievements,
          new VoidNSErrorBlock() {
            @Override
            public void invoke(NSError error) {
              if (error != null) {
                listener.achievementReportFailed();
              } else {
                listener.achievementReportCompleted();
              }
            }
          });
    } else { // If iOS version is 5 or less we use the deprecated method
      achievement.reportAchievement(
          new VoidNSErrorBlock() {
            @Override
            public void invoke(NSError error) {
              if (error != null) {
                listener.achievementReportFailed();
              } else {
                listener.achievementReportCompleted();
              }
            }
          });
    }
  }
  /**
   * Report a score to GameCenter
   *
   * @param identifier
   * @param score
   */
  public void reportScore(String identifier, long score) {
    // If player is not authenticated, do nothing
    if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
      listener.scoreReportFailed();
      return;
    }

    GKScore scoreReporter = new GKScore(identifier);
    scoreReporter.setValue(score);

    // If iOS version is 7 or more we use the new method
    if (getIosVersion() >= IOS_7) {
      NSArray<NSObject> scores = new NSArray<NSObject>(scoreReporter);

      GKScore.reportScores(
          scores,
          new VoidNSErrorBlock() {
            @Override
            public void invoke(NSError error) {
              if (error != null) {
                listener.scoreReportFailed();
              } else {
                listener.scoreReportCompleted();
              }
            }
          });
    } else { // If iOS version is 6 or less we use the deprecated method
      scoreReporter.reportScore(
          new VoidNSErrorBlock() {
            @Override
            public void invoke(NSError error) {
              if (error != null) {
                listener.scoreReportFailed();
              } else {
                listener.scoreReportCompleted();
              }
            }
          });
    }
  }
  /** Load all the achievements for the local player */
  public void loadAchievements() {
    // If player is not authenticated, do nothing
    if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
      listener.achievementsLoadFailed();
      return;
    }

    GKAchievement.loadAchievements(
        new VoidNSArrayNSErrorBlock() {
          @SuppressWarnings({"unchecked", "rawtypes"})
          @Override
          public void invoke(NSArray array, NSError error) {
            if (error != null) {
              listener.achievementsLoadFailed();
            } else {
              ArrayList<GKAchievement> achievements = new ArrayList<GKAchievement>();
              for (NSObject achievement : (NSArray<NSObject>) array) {
                achievements.add((GKAchievement) achievement);
              }
              listener.achievementsLoadCompleted(achievements);
            }
          }
        });
  }