unit TwitchAPI.Commands.GetExtensionAnalytics.Impl;

interface

uses
  TwitchAPI.Client,
  TwitchAPI.Commands.GetExtensionAnalytics,
  TwitchAPI.Entities;

type

{ TTwitchGetExtensionAnalyticsResult }

  TTwitchGetExtensionAnalyticsResult = class(TTwitchResult, ITwitchGetExtensionAnalyticsResult)
  private
    FData: TArray<TTwitchExtensionAnalytics>;
    FPagination: TTwitchPagination;
  public
    constructor Create;
    procedure Parse(const AData: string); override;
    function GetCount: Integer;
    function GetData: TArray<TTwitchExtensionAnalytics>;
    function GetPagination: TTwitchPagination;
    property Count: Integer read GetCount;
    property Data: TArray<TTwitchExtensionAnalytics> read GetData;
    property Pagination: TTwitchPagination read GetPagination;
  end;

{ TTwitchGetExtensionAnalyticsCommand }

  TTwitchGetExtensionAnalyticsCommand = class(TTwitchCommand, ITwitchGetExtensionAnalyticsCommand)
  public
    function GetEndpoint: string; override;
    function Execute: ITwitchGetExtensionAnalyticsResult;
    function SetExtensionID(const AValue: string): ITwitchGetExtensionAnalyticsCommand;
    function SetType(const AValue: string): ITwitchGetExtensionAnalyticsCommand;
    function SetStartedAt(const AValue: TDateTime): ITwitchGetExtensionAnalyticsCommand;
    function SetEndedAt(const AValue: TDateTime): ITwitchGetExtensionAnalyticsCommand;
    function SetFirst(const AValue: Integer): ITwitchGetExtensionAnalyticsCommand;
    function SetPagination(const AValue: TTwitchPagination): ITwitchGetExtensionAnalyticsCommand;
  end;

implementation

uses
  System.DateUtils,
  System.Generics.Collections,
  System.JSON,
  System.SysUtils,
  TwitchAPI.Helpers;

{ TTwitchGetExtensionAnalyticsResult }

constructor TTwitchGetExtensionAnalyticsResult.Create;
begin
  inherited Create;
  FData := [];
  FPagination.Cursor := string.Empty;
end;

function TTwitchGetExtensionAnalyticsResult.GetCount: Integer;
begin
  Result := Length(FData);
end;

function TTwitchGetExtensionAnalyticsResult.GetData: TArray<TTwitchExtensionAnalytics>;
begin
  Result := FData;
end;

function TTwitchGetExtensionAnalyticsResult.GetPagination: TTwitchPagination;
begin
  Result := FPagination;
end;

procedure TTwitchGetExtensionAnalyticsResult.Parse(const AData: string);
begin
  var LRoot := TJSONObject.ParseJSONValue(AData);
  try
    var LData := LRoot.GetValue<TJSONArray>('data');
    SetLength(FData, LData.Count);
    for var LIndex := 0 to LData.Count - 1 do
    begin
      var LItem := TJSONObject(LData[LIndex]);
      FData[LIndex].ExtensionID := LItem.GetValue<string>('extension_id');
      FData[LIndex].URL := LItem.GetValue<string>('URL');
      FData[LIndex].&Type := LItem.GetValue<string>('type');
      FData[LIndex].StartedAt := LItem.GetValue<TDateTime>('date_range.started_at');
      FData[LIndex].EndedAt := LItem.GetValue<TDateTime>('date_range.ended_at');
    end;
    FPagination.Cursor := LRoot.GetValue<string>('pagination.cursor');
  finally
    LRoot.Free;
  end;
end;

{ TTwitchGetExtensionAnalyticsCommand }

function TTwitchGetExtensionAnalyticsCommand.Execute: ITwitchGetExtensionAnalyticsResult;
begin
  Result := inherited Execute<TTwitchGetExtensionAnalyticsResult>();
end;

function TTwitchGetExtensionAnalyticsCommand.GetEndpoint: string;
begin
  Result := inherited GetEndpoint + 'analytics/extensions';
end;

function TTwitchGetExtensionAnalyticsCommand.SetEndedAt(
  const AValue: TDateTime): ITwitchGetExtensionAnalyticsCommand;
begin
  Query.AddOrSetValue('ended_at', DateTimeToRFC3339(AValue));
end;

function TTwitchGetExtensionAnalyticsCommand.SetExtensionID(
  const AValue: string): ITwitchGetExtensionAnalyticsCommand;
begin
  Query.AddOrSetValue('extension_id', AValue);
end;

function TTwitchGetExtensionAnalyticsCommand.SetFirst(
  const AValue: Integer): ITwitchGetExtensionAnalyticsCommand;
begin
  Query.AddOrSetValue('first', IntToStr(AValue));
end;

function TTwitchGetExtensionAnalyticsCommand.SetPagination(
  const AValue: TTwitchPagination): ITwitchGetExtensionAnalyticsCommand;
begin
  Query.AddOrSetValue('after', AValue.Cursor);
end;

function TTwitchGetExtensionAnalyticsCommand.SetStartedAt(
  const AValue: TDateTime): ITwitchGetExtensionAnalyticsCommand;
begin
  Query.AddOrSetValue('started_at', DateTimeToRFC3339(AValue));
end;

function TTwitchGetExtensionAnalyticsCommand.SetType(
  const AValue: string): ITwitchGetExtensionAnalyticsCommand;
begin
  Query.AddOrSetValue('type', AValue);
end;

end.
