unit LocEndpoints;

interface

uses
  Classes, Conexoes, IdHTTP, XSuperObject, IdGlobal, Vcl.Dialogs, System.SysUtils, System.StrUtils, System.Types,
  MainCode, Vcl.Graphics, Vcl.Imaging.pngimage, DelphiZXIngQRCode, ModalParametrosDatas, Forms, ModalParametrosIdent,
  ModalParametrosBody;

procedure SGetv2Loc;
procedure SGetv2Loc_id;
procedure SGetv2Loc_id_QrCode;
procedure SPostv2Loc;
procedure SDelv2Loc_id_txid;
procedure QrCodeGenerator(texto: WideString);

implementation

threadvar
  HttpClient: TIdHTTP;
  AccessToken, ObjResult : iSuperObject;
  JsonToSend: TMemoryStream;


var
  ModalParametrosDatas : TModalParametrosDatas;
  ModalParametrosBody : TModalParametrosBody;
  ModalParametrosIdent : TModalParametrosIdent;

procedure SGetv2Loc;
var
  sResponse: string;
begin
  try
    try
      if not Assigned(ModalParametrosDatas) then
        Application.CreateForm(TModalParametrosDatas, ModalParametrosDatas);

      if ModalParametrosDatas.ShowModal = 1 then
      begin
        AccessToken := Conexoes.OauthToken;
        HttpClient := Conexoes.SetupClient;

        HttpClient.Request.CustomHeaders.Values['Authorization'] := ('Bearer '+ AccessToken.S['access_token']);
        sResponse := HttpClient.Get(MainCode.MainFrm.Enviroment + '/v2/loc/?inicio='
                                  +ModalParametrosDatas.DataInicio+'&fim='+ModalParametrosDatas.DataFim);

        MainCode.MainFrm.MemoResponse.text := sresponse;
      end;

    except
      on E: Exception do
      begin
        ShowMessage('Erro na requisição GET /v2/loc '#13#10#13#10 + e.Message);
      end;
    end;
  finally
    ModalParametrosDatas.txtDataInicio := '';
    ModalParametrosDatas.txtDataFim := '';
  end;
end;

procedure SGetv2Loc_id;
var
  sResponse: string;
begin
  try
    try
      if not Assigned(ModalParametrosIdent) then
        Application.CreateForm(TModalParametrosIdent, ModalParametrosIdent);

      if ModalParametrosIdent.ShowModal = 1 then
      begin
        AccessToken := Conexoes.OauthToken;
        HttpClient := Conexoes.SetupClient;

        HttpClient.Request.CustomHeaders.Values['Authorization'] := ('Bearer '+ AccessToken.S['access_token']);
        sResponse := HttpClient.Get(MainCode.MainFrm.Enviroment + '/v2/loc/'+ ModalParametrosIdent.Identificador);

        MainCode.MainFrm.MemoResponse.text := sresponse;
      end;

    except
      on E: Exception do
      begin
        ShowMessage('Erro na requisição GET /v2/loc/:id '#13#10#13#10 + e.Message);
      end;
    end;
  finally
    ModalParametrosIdent.txtIdent := '';
  end;
end;

procedure SGetv2Loc_id_QrCode;
var
  sResponse: WideString;
begin
  try
    try
      if not Assigned(ModalParametrosIdent) then
        Application.CreateForm(TModalParametrosIdent, ModalParametrosIdent);

      if ModalParametrosIdent.ShowModal = 1 then
      begin
        AccessToken := Conexoes.OauthToken;
        HttpClient := Conexoes.SetupClient;

        HttpClient.Request.CustomHeaders.Values['Authorization'] := ('Bearer '+ AccessToken.S['access_token']);
        sResponse := HttpClient.Get(MainCode.MainFrm.Enviroment + '/v2/loc/'+ ModalParametrosIdent.Identificador +'/qrcode');

        ObjResult := SO(sResponse);

        MainCode.MainFrm.MemoResponse.text := sresponse;

        QrCodeGenerator(ObjResult.S['qrcode']);
      end;

    except
      on E: Exception do
      begin
        ShowMessage('Erro na requisição GET /v2/loc/:id/qrcode '#13#10#13#10 + e.Message);
      end;
    end;
  finally
    ModalParametrosIdent.txtIdent := '';
  end;
end;

procedure SPostv2Loc;
var
  sResponse: string;
begin
  try
    try
      if not Assigned(ModalParametrosBody) then
        Application.CreateForm(TModalParametrosBody, ModalParametrosBody);

      if ModalParametrosBody.ShowModal = 1 then
      begin
        AccessToken := Conexoes.OauthToken;
        HttpClient := Conexoes.SetupClient;

        HttpClient.Request.CustomHeaders.Values['Authorization'] := ('Bearer '+ AccessToken.S['access_token']);

        JsonToSend := TMemoryStream.Create;
        WriteStringToStream(JsonToSend, ModalParametrosBody.Body, IndyTextEncoding_UTF8);
        JsonToSend.Position := 0;

        sResponse := HttpClient.Post( MainCode.MainFrm.Enviroment  + '/v2/loc', JsonToSend);

        MainCode.MainFrm.MemoResponse.text := sresponse;
      end;

    except
      on E: Exception do
      begin
        ShowMessage('Erro na requisição POST /v2/loc '#13#10#13#10 + e.Message);
      end;
    end;
  finally
    ModalParametrosBody.txtBody := '';
  end;
end;

procedure SDelv2Loc_id_txid;
var
  sResponse: string;
begin
  try
    try
      if not Assigned(ModalParametrosIdent) then
        Application.CreateForm(TModalParametrosIdent, ModalParametrosIdent);

      if ModalParametrosIdent.ShowModal = 1 then
      begin
        AccessToken := Conexoes.OauthToken;
        HttpClient := Conexoes.SetupClient;

        HttpClient.Request.CustomHeaders.Values['Authorization'] := ('Bearer '+ AccessToken.S['access_token']);
        sResponse := HttpClient.Delete(MainCode.MainFrm.Enviroment + '/v2/loc/'+ModalParametrosIdent.Identificador+'/txid');

        MainCode.MainFrm.MemoResponse.text := sresponse;
      end;

    except
      on E: Exception do
      begin
        ShowMessage('Erro na requisição DELETE /v2/loc/:id/txid '#13#10#13#10 + e.Message);
      end;
    end;
  finally
    ModalParametrosIdent.txtIdent := '';
  end;
end;

///Procedure de imprimir o QRCode na tela
procedure QrCodeGenerator(texto: WideString);
var
  bitmap: TBitmap;
  QRCode: TDelphiZXingQRCode;
  Row, Column: Integer;
  scala: Double;
  textoSeparado: TStringDynArray;
begin
  bitmap := TBitmap.create;
  QRCode := TDelphiZXingQRCode.create;

  textoSeparado := SplitString(texto, '/');

  QRCode.Data := texto;

  bitmap.SetSize(QRCode.Rows, QRCode.Columns);

  for Row := 0 to QRCode.Rows do
    for Column := 0 to QRCode.Columns do
      if QRCode.IsBlack[Row, Column] then
        bitmap.Canvas.Pixels[Column, Row] := clBlack
      else
        bitmap.Canvas.Pixels[Column, Row] := clWhite;

  if (MainCode.MainFrm.QrCodeImg.Width < bitmap.Height) then
    scala := MainCode.MainFrm.QrCodeImg.Width / bitmap.Width
  else
    scala := MainCode.MainFrm.QrCodeImg.Height / bitmap.Height;

  MainCode.MainFrm.QrCodeImg.Canvas.StretchDraw(Rect(0, 0, Trunc(scala * bitmap.Width), Trunc(scala * bitmap.Height)), bitmap);
end;

end.
