//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include "AdvGrid.hpp"
#include "AdvObj.hpp"
#include "AdvUtil.hpp"
#include "BaseGrid.hpp"
#include "DBAdvGrid.hpp"
#include <Vcl.Graphics.hpp>
#include <Vcl.Grids.hpp>
#include <Data.Bind.Components.hpp>
#include <Data.Bind.ObjectScope.hpp>
#include <Data.DB.hpp>
#include <FireDAC.Comp.Client.hpp>
#include <FireDAC.Comp.DataSet.hpp>
#include <FireDAC.DApt.Intf.hpp>
#include <FireDAC.DatS.hpp>
#include <FireDAC.Phys.Intf.hpp>
#include <FireDAC.Stan.Error.hpp>
#include <FireDAC.Stan.Intf.hpp>
#include <FireDAC.Stan.Option.hpp>
#include <FireDAC.Stan.Param.hpp>
#include <REST.Client.hpp>
#include <REST.Response.Adapter.hpp>
#include <REST.Types.hpp>
#include "AdvSplitter.hpp"
#include <Vcl.ExtCtrls.hpp>
#include "AdvEdit.hpp"
#include "vcl.wwdbcomb.hpp"
#include "vcl.wwdbedit.hpp"
#include "vcl.wwdotdot.hpp"
#include <Vcl.Mask.hpp>
#include "AdvOfficePager.hpp"
#include "MemDS.hpp"
#include "VirtualTable.hpp"
#include "DBAccess.hpp"
#include "Uni.hpp"
#include "vcl.wwdbdatetimepicker.hpp"
#include "PostgreSQLUniProvider.hpp"
#include "UniProvider.hpp"
#include <dinkumware\map>
#include <SysUtils.hpp>
#include <DateUtils.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
  TRESTClient *RESTClient1;
  TRESTRequest *RESTRequest1;
  TRESTResponse *RESTResponse1;
  TPanel *Panel1;
  TPanel *Panel2;
  TPanel *Panel3;
  TAdvSplitter *AdvSplitter1;
  TMemo *Memo1;
  TAdvSplitter *AdvSplitter2;
  TPanel *Panel4;
  TMemo *Memo2;
  TAdvSplitter *AdvSplitter3;
  TMemo *Memo3;
  TButton *btnExit;
  TAdvOfficePager *AdvOfficePager1;
  TAdvEdit *txtNextPageUri;
  TAdvOfficePage *AdvOfficePage1;
  TAdvStringGrid *grdData;
  TAdvOfficePage *AdvOfficePage2;
  TAdvStringGrid *grdFields;
  TAdvOfficePage *AdvOfficePage3;
  TAdvStringGrid *asgData;
  TButton *btnMessages;
	TUniConnection *ucTwilioDB;
	TUniQuery *uqMessages;
	TwwDBDateTimePicker *dtpStartDate;
	TwwDBDateTimePicker *dtpEndDate;
	TwwDBComboBox *dbcbYear;
	TLabel *Label2;
	TAdvEdit *edtPageSize;
	TLabel *Label1;
	TLabel *Label3;
	TLabel *Label4;
	TLabel *Label5;
	TwwDBComboBox *dbcbMonth;
	TLabel *Label6;
	TButton *btnNextPage;
	TCheckBox *cbSaveToDB;
	TPostgreSQLUniProvider *PostgreSQLUniProvider1;
	TMemoField *uqMessagessid;
	TMemoField *uqMessagesaccount_sid;
	TMemoField *uqMessagesapi_version;
	TMemoField *uqMessagesbody;
	TDateTimeField *uqMessagesdate_created;
	TDateTimeField *uqMessagesdate_sent;
	TDateTimeField *uqMessagesdate_updated;
	TMemoField *uqMessagesdirection;
	TIntegerField *uqMessageserror_code;
	TMemoField *uqMessageserror_message;
	TMemoField *uqMessagesfrom_number;
	TMemoField *uqMessagesmessaging_service_sid;
	TIntegerField *uqMessagesnum_media;
	TIntegerField *uqMessagesnum_segments;
	TFloatField *uqMessagesprice;
	TMemoField *uqMessagesprice_unit;
	TMemoField *uqMessagesstatus;
	TMemoField *uqMessagesto_number;
	TMemoField *uqMessagesuri;
  void __fastcall btnExitClick(TObject *Sender);
  void __fastcall btnMessagesClick(TObject *Sender);
	void __fastcall dbcbMonthCloseUp(TwwDBComboBox *Sender, bool Select);
	void __fastcall dbcbYearCloseUp(TwwDBComboBox *Sender, bool Select);
	void __fastcall btnNextPageClick(TObject *Sender);
private:	// User declarations
  String accountSid;
  String authHeader;
  TStringList *fieldsList;
  int pagesLoaded;

  void TForm1::SaveMessagesToDB(TJSONArray* jaMessages);
  void GetFieldsFromJsonArray(TJSONArray* jaData);
  void LoadJsonArray(TJSONArray* jaData, String detail);
  void SetDatePickersFromMonthYear();
  void AppendJsonArray(TJSONArray* jaData);
  String TForm1::BuildMessageRequestURL();

public:		// User declarations
  __fastcall TForm1(TComponent* Owner);
  TDateTime ParseTwilioDateTime(const UnicodeString &dateStr);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
