unit MainForm;

interface

uses
  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
  WEBLib.Forms, WEBLib.Dialogs, Data.DB, WEBLib.DB, Vcl.Controls, Vcl.Grids,
  WEBLib.DBCtrls, XData.Web.JsonDataset, XData.Web.Dataset, XData.Web.Connection,
  Vcl.StdCtrls, WEBLib.StdCtrls, JS, WEBLib.ExtCtrls;

type
  TForm1 = class(TWebForm)
    XDataWebConnection1: TXDataWebConnection;
    XDataWebDataSet1: TXDataWebDataSet;
    WebDBGrid1: TWebDBGrid;
    WebDataSource1: TWebDataSource;
    XDataWebDataSet1Id: TIntegerField;
    XDataWebDataSet1Name: TStringField;
    XDataWebDataSet1Descriptionxdataproxy: TStringField;
    WebButton1: TWebButton;
    WebMemo1: TWebMemo;
    WebButton2: TWebButton;
    WebDBNavigator1: TWebDBNavigator;
    WebImageControl1: TWebImageControl;
    WebButton3: TWebButton;
    WebEdit1: TWebEdit;
    WebButton4: TWebButton;
    WebButton5: TWebButton;
    procedure XDataWebConnection1Connect(Sender: TObject);
    procedure WebFormCreate(Sender: TObject);
    procedure WebButton1Click(Sender: TObject);
    procedure WebHttpRequest1RequestResponse(Sender: TObject;
      ARequest: TJSXMLHttpRequest; AResponse: string);
    procedure WebButton2Click(Sender: TObject);
    procedure WebButton3Click(Sender: TObject);
    procedure WebButton4Click(Sender: TObject);
    procedure WebButton5Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WebButton1Click(Sender: TObject);
var
  xhr: TJSXmlHttpRequest;

  procedure _Load;
  begin
    WebMemo1.Lines.Text := xhr.responseText;
  end;

begin
  xhr := TJSXMLHttpRequest.new;
  xhr.open('GET',
    XDataWebConnection1.URL + '/' +
    string(TJSObject(XDataWebDataset1.CurrentData)['Description@xdata.proxy'])
  );
  xhr.overrideMimeType('text/plain; charset=utf-16');
  xhr.addEventListener('load', @_Load);
  xhr.send;
end;

procedure TForm1.WebButton2Click(Sender: TObject);
var
  xhr: TJSXmlHttpRequest;

  function strEncodeUTF16(str: string): TJSArrayBuffer;
  var
    BufView: TJSUInt16Array;
    I: Integer;
  begin
    Result := TJSArrayBuffer.new(Length(str) * 2);
    BufView := TJSUInt16Array.new(Result);
    for I := 0 to Length(str) - 1 do
      BufView[I] := TJSString(str).charCodeAt(I);
  end;

begin
  xhr := TJSXMLHttpRequest.new;
  xhr.open('PUT',
    XDataWebConnection1.URL + '/' +
    string(TJSObject(XDataWebDataset1.CurrentData)['Description@xdata.proxy'])
  );
//  xhr.overrideMimeType('text/plain; charset=utf-16');
//  http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
//  xhr.addEventListener('load', @_Load);
  xhr.send(strEncodeUTF16(WebMemo1.Lines.Text));
end;

procedure TForm1.WebButton3Click(Sender: TObject);
begin
  WebImageControl1.Url := XDataWebConnection1.URL + '/' +
    string(TJSObject(XDataWebDataset1.CurrentData)['Photo@xdata.proxy']);
end;

procedure TForm1.WebButton4Click(Sender: TObject);
begin
  WebImageControl1.Url := WebEdit1.Text;
end;

procedure TForm1.WebButton5Click(Sender: TObject);
var
  xhr: TJSXmlHttpRequest;

  function Base64ToArrayBuffer(str: string): TJSArrayBuffer;
  var
    BufView: TJSUInt8Array;
    BinaryString: string;
    I: Integer;
  begin
    BinaryString := window.atob(str);
    Result := TJSArrayBuffer.new(Length(BinaryString));
    BufView := TJSUInt8Array.new(Result);
    for I := 0 to Length(BinaryString) - 1 do
      BufView[I] := TJSString(BinaryString).charCodeAt(I);
  end;

begin
  xhr := TJSXMLHttpRequest.new;
  xhr.open('PUT',
    XDataWebConnection1.URL + '/' +
    string(TJSObject(XDataWebDataset1.CurrentData)['Photo@xdata.proxy'])
  );
  xhr.send(Base64ToArrayBuffer(WebImageControl1.Base64Image));
end;

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  XDataWebConnection1.Open;
end;

procedure TForm1.WebHttpRequest1RequestResponse(Sender: TObject;
  ARequest: TJSXMLHttpRequest; AResponse: string);
begin
  WebMemo1.Lines.Text := AResponse;
end;

procedure TForm1.XDataWebConnection1Connect(Sender: TObject);
begin
  XDataWebDataSet1.Load;
end;

end.