unit Lib;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Dialogs, JavascriptObject,
  Soap.InvokeRegistry, Soap.SOAPHTTPClient, System.Types, Soap.XSBuiltIns,
  TypInfo, System.RTTI, Soap.Rio;


type

  TLib = class
    class function VarToStr(val: Variant): string;
    class procedure ShowMessage(Value: string);
    class function JSDecode(Value: Variant): TJSObject;

    class function EncodeMd5(const value: string): string;
    class function EncodeBase64(const value: string): string;
    class function DecodeBase64(const value: string): string;
  end;

implementation

uses  Main, uPSRuntime, uPSCompiler;

class procedure TLib.ShowMessage(Value: string);
begin
  Vcl.Dialogs.ShowMessage(Value);
end;

class function TLib.JSDecode(Value: Variant): TJSObject;
var
  JS: TJSObject;
begin
  JS:= TJSObject.Create();
  JS.ActualJSPoint := Value;
  Result := JS;
end;

class function TLib.Soap(Addr: string): ISoapInvokable;
const
  defSvc  = 'Calculator';
  defPrt  = 'CalculatorSoap12';
var
  RIO: THTTPRIO;
begin

  InvRegistry.RegisterInterface(TypeInfo(ISoapInvokable), 'http://tempuri.org/', 'utf-8');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(ISoapInvokable), 'http://tempuri.org/%operationName%');
  InvRegistry.RegisterInvokeOptions(TypeInfo(ISoapInvokable), ioDocument);
  InvRegistry.RegisterInvokeOptions(TypeInfo(ISoapInvokable), ioSOAP12);
  { CalculatorSoap.Add }
  InvRegistry.RegisterMethodInfo(TypeInfo(ISoapInvokable), 'Add', '',
                                 '[ReturnName="AddResult"]');
  { CalculatorSoap.Subtract }
  InvRegistry.RegisterMethodInfo(TypeInfo(ISoapInvokable), 'Subtract', '',
                                 '[ReturnName="SubtractResult"]');
  { CalculatorSoap.Multiply }
  InvRegistry.RegisterMethodInfo(TypeInfo(ISoapInvokable), 'Multiply', '',
                                 '[ReturnName="MultiplyResult"]');
  { CalculatorSoap.Divide }
  InvRegistry.RegisterMethodInfo(TypeInfo(ISoapInvokable), 'Divide', '',
                                 '[ReturnName="DivideResult"]');

  Result := nil;
  if Addr = '' then
    exit();

  RIO := THTTPRIO.Create(nil);
  try
    Result := RIO as ISoapInvokable;
    RIO.WSDLLocation := Addr;
    RIO.Service := defSvc;
    RIO.Port := defPrt;
  finally
    if (Result = nil) then
      RIO.Free;
  end;
end;

class function TLib.SoapCall(Service: TObject; MethodName: string; Args: array of Variant): Variant;
var
 R : TRttiContext;
 T : TRttiType;
 M : TRttiMethod;
 ResultVal: TValue;
 VArr: array of TValue;
  I: Integer;
begin
  Result := EmptyStr;
  T := R.FindType('ISoapInvokable');
  for M in t.GetMethods do
    if (m.Name = MethodName)then
    begin
      for I := Low(Args) to High(Args) do
      begin
        Varr[I] := TValue.FromVariant(Args[I]);
      end;
      ResultVal := M.Invoke(Service, Varr);
      Result := ResultVal.AsVariant;
      break;
    end;

end;


class function TLib.VarToStr(val: Variant): string;
begin
  Result := VarToStr(val);
end;

initialization





end.
