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

#ifndef fMainH
#define fMainH
//---------------------------------------------------------------------------
#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 <Vcl.DBGrids.hpp>
#include <dinkumware\map>
#include <SysUtils.hpp>
#include <DateUtils.hpp>
//---------------------------------------------------------------------------
class TfrmMain : 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;
  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;
	TLabel *Label6;
	TCheckBox *cbSaveToDB;
	TPostgreSQLUniProvider *PostgreSQLUniProvider1;
  TButton *Button1;
  TDBGrid *dbgrdDatabase;
  TButton *btnDBMessages;
  TDataSource *dsMessages;
  TStringField *uqMessagessid;
  TStringField *uqMessagesaccount_sid;
  TStringField *uqMessagesapi_version;
  TMemoField *uqMessagesbody;
  TDateTimeField *uqMessagesdate_created;
  TDateTimeField *uqMessagesdate_sent;
  TDateTimeField *uqMessagesdate_updated;
  TStringField *uqMessagesdirection;
  TIntegerField *uqMessageserror_code;
  TMemoField *uqMessageserror_message;
  TStringField *uqMessagesfrom_number;
  TStringField *uqMessagesmessaging_service_sid;
  TIntegerField *uqMessagesnum_media;
  TIntegerField *uqMessagesnum_segments;
  TFloatField *uqMessagesprice;
  TStringField *uqMessagesprice_unit;
  TStringField *uqMessagesstatus;
  TStringField *uqMessagesto_number;
  TStringField *uqMessagesuri;
  TwwDBComboBox *dbcbMonth;
  TUniQuery *uqDBMessages;
  TStringField *StringField1;
  TStringField *StringField2;
  TStringField *StringField3;
  TMemoField *MemoField1;
  TDateTimeField *DateTimeField1;
  TDateTimeField *DateTimeField2;
  TDateTimeField *DateTimeField3;
  TStringField *StringField4;
  TIntegerField *IntegerField1;
  TMemoField *MemoField2;
  TStringField *StringField5;
  TStringField *StringField6;
  TIntegerField *IntegerField2;
  TIntegerField *IntegerField3;
  TFloatField *FloatField1;
  TStringField *StringField7;
  TStringField *StringField8;
  TStringField *StringField9;
  TStringField *StringField10;
  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 Button1Click(TObject *Sender);
  void __fastcall btnDBMessagesClick(TObject *Sender);
private:	// User declarations
  String accountSid;
  String authHeader;
  TStringList *fieldsList;
  int pagesLoaded;

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

public:		// User declarations
  __fastcall TfrmMain(TComponent* Owner);
  TDateTime ParseTwilioDateTime(String dateStr);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmMain *frmMain;
//---------------------------------------------------------------------------
#endif
