unit XData.Payload.Writer;

interface

uses
  Generics.Collections, System.Classes, System.Rtti,
  Bcl.Json.Converters,
  XData.Json.Solver,
  XData.Model.Classes;

type
  IPayloadWriter = interface
  ['{EF10FD9D-ECF8-4256-978D-8D106254AF0B}']
    function ContentType: string;
    procedure Flush;
  end;

  TXDataPayloadWriter = class abstract(TInterfacedObject, IPayloadWriter)
  strict private
    FStream: TStream;
    FModel: TXDataModel;
    FBaseUrl: string;
//    FVersion: TXDataVersion;
  strict protected
    procedure Flush; virtual; abstract;
    function ContentType: string; virtual; abstract;
  public
    constructor Create(const AStream: TStream; const AModel: TXDataModel; const ABaseUrl: string);
    property Stream: TStream read FStream;
    property Model: TXDataModel read FModel;
    property BaseUrl: string read FBaseUrl;
//    property Version: TXDataVersion read FVersion;
  end;

  IErrorWriter = interface(IPayloadWriter)
  ['{3C34C867-9191-4C5D-A34B-5561C4409CD6}']
    procedure WriteError(const ErrorCode, ErrorMessage: string);
  end;

  IServiceDocumentWriter = interface(IPayloadWriter)
  ['{882C9CEE-96F0-4DFB-B585-5119732C4395}']
    procedure WriteDocument;
  end;

  IEntityCollectionWriter = interface(IPayloadWriter)
  ['{5CD911FB-628A-4599-93C2-D9EC34F8D03B}']
    procedure Start(ABaseClass: TClass);
    procedure SerializeItem(const AObject: TObject);
    procedure Finish;
    procedure SetExpandLevel(const Value: Integer);
    procedure SetExpander(Value: IJsonExpander);
    procedure SetSerializeInstanceRef(Value: TInstanceRefSerialization);
    procedure SetSerializeInstanceType(Value: TInstanceTypeSerialization);
    procedure SetInlineCount(const Value: Integer);
  end;

  IEntityWriter = interface(IPayloadWriter)
  ['{8D1A4DC3-7065-4317-8EFB-9E024C1E31B3}']
    procedure WriteEntity(const AObject: TObject; BaseClass: TClass);
    procedure SetExpandLevel(const Value: integer);
    procedure SetExpander(Value: IJsonExpander);
    procedure SetSerializeInstanceRef(Value: TInstanceRefSerialization);
    procedure SetSerializeInstanceType(Value: TInstanceTypeSerialization);
  end;

  IPropertyWriter = interface(IPayloadWriter)
  ['{BA528BD8-090E-422F-8BD0-8F7D9BEF0F51}']
    procedure WriteProperty(const AObject: TObject; const AProp: TXDataSimpleProperty);
  end;

  IResultWriter = interface(IPayloadWriter)
  ['{49F6CFCC-A26A-4D9C-9E99-F60B0DA39310}']
    procedure WriteResult(const ResultValue: TValue; const Args: TArray<TValue>; Action: TXDataAction);
    function WrittenObjects: TEnumerable<TObject>;
    procedure SetExpandLevel(const Value: integer);
    procedure SetExpander(Value: IJsonExpander);
    procedure SetSerializeInstanceRef(Value: TInstanceRefSerialization);
    procedure SetSerializeInstanceType(Value: TInstanceTypeSerialization);
  end;

implementation

{ TXDataWriter }

constructor TXDataPayloadWriter.Create(const AStream: TStream; const AModel: TXDataModel;
  const ABaseUrl: string);
begin
  FStream := AStream;
  FModel := AModel;
  FBaseUrl := ABaseUrl;
end;

end.
