%% NBA Standings for current year.

% Create STATS object 
sBasketball = statsllc('se33pc658r9abcahm4a9j93p','frvaBcb33y',...
                    'DataType','stats','LeagueAbbreviation','nba',...
                    'Resource','standings','SportName','basketball',...
                    'VersionNumber','v1');

% retrieve standings data for latest year
data = fetch(sBasketball);

% Parse data into standings table
numConferences = length(data.apiResults.league.season.eventType.conferences);
for i = 1:numConferences
  numDivisions = length(data.apiResults.league.season.eventType.conferences(i).divisions);
  for j = 1:numDivisions
    t = struct2table(data.apiResults.league.season.eventType.conferences(i).divisions(j).teams);
    numTeams = length(data.apiResults.league.season.eventType.conferences(i).divisions(j).teams);
    recordTable = [];
    for k = 1:numTeams
      divisionStatusTable = struct2table(data.apiResults.league.season.eventType.conferences(i).divisions(j).teams(k).division);
      recordTable = [recordTable;struct2table(data.apiResults.league.season.eventType.conferences(i).divisions(j).teams(k).record) table(divisionStatusTable.gamesBehind,'VariableNames',{'GamesBack'})]; %#ok
    end
    standingsTable = table(strcat(t.location,strcat('.',t.nickname)),recordTable.wins,recordTable.losses,recordTable.percentage,recordTable.GamesBack,'VariableNames',...
                           {'Team','Wins','Losses','Percentage','GamesBehind'});
    disp([data.apiResults.league.season.eventType.conferences(i).name ' ' data.apiResults.league.season.eventType.conferences(i).divisions(j).name])
    disp(' ')
    disp(standingsTable)
  end
end