unit XData.Bind.Converter;

interface

uses
  TypInfo, SysUtils,
  XData.Model.Classes;

function BindValueToUrl(const Value: JSValue; const Info: TTypeInfo;
  UrlConverter: TXDataTypeConverter): string;
//function BindUrlToValue(const Literal: string; const Info: TTypeInfo;
//  UrlConverter: TXDataTypeConverter): JSValue;

implementation

function BindValueToUrl(const Value: JSValue; const Info: TTypeInfo;
  UrlConverter: TXDataTypeConverter): string;
var
  TempValue: JSValue;
begin
  // TGUID??
//  if Info = TypeInfo(TDateTime) then
//  begin
//    DateTimeValue := TDateTime(TempValue.AsType<double>);
//    TValue.Make(@DateTimeValue, Info, Result);
//  end
//  else
//  if Info = TypeInfo(TDate) then
//  begin
//    DateValue := TDate(TempValue.AsType<double>);
//    TValue.Make(@DateValue, Info, Result);
//  end
//  else
//  if Info = TypeInfo(TTime) then
//  begin
//    TimeValue := TTime(TempValue.AsType<double>);
//    TValue.Make(@TimeValue, Info, Result);
//  end
//  else
//  if Info = TypeInfo(TArray<byte>) then
//  begin
//    Bytes := TempValue.AsType<TBytes>;
//    TValue.Make(@Bytes, Info, Result);
//  end
//  else
//  else
//  if Info.Kind = tkEnumeration then
//    Result := TValue.FromOrdinal(Info, TempValue.AsOrdinal)
{  if Info = TypeInfo(TBlob) then
  begin
    TempValue := TValue.From<TBytes>(Value.AsType<TBlob>.AsBytes);
  end
  else                 }
    TempValue := Value;

  Result := UrlConverter.ValueToUrl(TempValue, nil);
end;

(*function BindUrlToValue(const Literal: string; const Info: TTypeInfo;
  UrlConverter: TXDataTypeConverter): JSValue;
var
  TempValue: JSValue;
  DateTimeValue: TDateTime;
  DateValue: TDate;
  TimeValue: TTime;
  Bytes: TBytes;
//  Blob: TBlob;
begin
  TempValue := UrlConverter.UrlToValue(Literal, nil);

  // Make sure value is same type of parameter type
  // This should be replaced by a factory/registration system
  // The following code was borrowed from XData.Aurelius.Types

  // TGUID??
  if Info = TypeInfo(TDateTime) then
  begin
    DateTimeValue := TDateTime(TempValue.AsType<double>);
    TValue.Make(@DateTimeValue, Info, Result);
  end
  else
  if Info = TypeInfo(TDate) then
  begin
    DateValue := TDate(TempValue.AsType<double>);
    TValue.Make(@DateValue, Info, Result);
  end
  else
  if Info = TypeInfo(TTime) then
  begin
    TimeValue := TTime(TempValue.AsType<double>);
    TValue.Make(@TimeValue, Info, Result);
  end
  else
  if Info = TypeInfo(TArray<byte>) then
  begin
    Bytes := TempValue.AsType<TBytes>;
    TValue.Make(@Bytes, Info, Result);
  end
  else
  if Info = TypeInfo(TBlob) then
  begin
    Blob.AsBytes := TempValue.AsType<TBytes>;
    Result := TValue.From<TBlob>(Blob);
  end
  else
  if Info.Kind = tkEnumeration then
    Result := TValue.FromOrdinal(Info, TempValue.AsOrdinal)
  else
  if not TempValue.TryCast(Info, Result) then
    raise EConvertFromUrlError.Create(nil, Literal);
end;*)

end.