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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "nratcmd"
#pragma link "nrclasses"
#pragma link "nrcomm"
#pragma link "nrcommbox"
#pragma link "nrgsm"
#pragma link "nrgsmpdu"
#pragma link "nrlogfile"
#pragma link "CSPIN"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  nrComm1->ConfigDialog();        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
  try{
    nrComm1->Active = CheckBox1->Checked;
  }catch(Exception &e) {
     throw;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  nrGsm1->SmsSend(Edit1->Text,Memo1->Text, chConfirm->Checked);
}

//---------------------------------------------------------------------------
void __fastcall TForm1::AddInfo(AnsiString sCapt, AnsiString sVal)
{
        TListItem *Itm;
        if(sVal.Trim() == "") return;
        Itm = lvInfo->Items->Add();
        Itm->Caption = sCapt;
        Itm->SubItems->Add(sVal);
}

//---------------------------------------------------------------------------
void __fastcall TForm1::nrGsm1AfterInit(TObject *Sender)
{
  ListBox1->Items->Add("GSM connected ===========");
  lvInfo->Clear();

  AddInfo("Manufacturer", nrGsm1->DeviceManufacturer);
  AddInfo("Device", nrGsm1->DeviceName);
  AddInfo("Version", nrGsm1->DeviceVersion);
  AddInfo("IMEI", nrGsm1->DeviceIMEI);
  AddInfo("Driver", nrGsm1->Driver->Caption);

  eMem->Items->Text = nrGsm1->MemTypesRead->Text;
  eMem->ItemIndex = eMem->Items->Count - 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::nrGsm1GsmError(TObject *Sender, AnsiString sData)
{
  ListBox1->Items->Add("GSM Error: " + sData);
  ListBox1->TopIndex = ListBox1->Count - 1;
  ListBox1->ItemIndex = ListBox1->Count - 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::nrGsm1Ring(TObject *Sender, AnsiString sData)
{
  ListBox1->Items->Add("RING: " + sData);
  ListBox1->TopIndex = ListBox1->Count - 1;
  ListBox1->ItemIndex = ListBox1->Count - 1;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  nrComm1->Terminal = NULL;
  pgTerminal->Free();
  Pages->ActivePageIndex = 0;
  cbOnNewSms->ItemIndex = (int)nrGsm1->NewSmsMode;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::cbOnNewSmsChange(TObject *Sender)
{
  nrGsm1->NewSmsMode = (TgsmNewSmsMode)cbOnNewSms->ItemIndex;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
  nrGsm1->RunDiagnostic();
  ShowMessage("Log file with diagnostic information has been saved as "+
               nrLogFile1->FileName + " file.\nPlease send this log to DeepSoftware.Com tech support service.");

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
  nrGsm1->MemoryRead =  eMem->Text;
  TnrPduSms *pdu = nrGsm1->SmsGet(eNo->Value);
  if(! pdu->IsEmpty){
      Memo2->Text = pdu->Text;
      ListBox1->AddItem("OK",NULL);
  } else ListBox1->AddItem("Error",NULL) ;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender)
{
  nrGsm1->MemoryRead =  eMem->Text;
  nrGsm1->SmsList(false);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
  nrGsm1->MemoryRead =  eMem->Text;
  if(nrGsm1->SmsDelete(eNo->Value))
    ListBox1->AddItem("Deleted OK",NULL);
  else
    ListBox1->AddItem("Delete Error",NULL);
  ListBox1->TopIndex = ListBox1->Count - 1;
  ListBox1->ItemIndex = ListBox1->Count - 1;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::nrGsm1SmsListItem(TObject *Sender, AnsiString aMem,
      int idSms, TnrPduSms *aSms)
{
  if(aSms != NULL){
    if(aSms->Report){
      Memo2->Lines->Add("REPORT: " + aMem + "," + IntToStr(idSms) + " ======= ");
      Memo2->Lines->Add(aSms->ReportText);
    }else{
      Memo2->Lines->Add("SMS: " + aMem + "," + IntToStr(idSms) + " ======= ");
      Memo2->Lines->Add(aSms->Text);
    }
  } else Memo2->Lines->Add("======= GET SMS LIST DONE!!! =============");

}
//---------------------------------------------------------------------------

void __fastcall TForm1::nrGsm1SmsReceived(TObject *Sender, AnsiString aMem,
      int idSms, TnrPduSms *aSms)
{
  if(aMem != "")
    ListBox1->Items->Add("Sms is received '" + aMem+"' "+ IntToStr(idSms));
  else
    ListBox1->Items->Add("Sms is received ");

  if(aSms != NULL){
    if(aSms->Report)
      ListBox1->Items->Add(aSms->ReportText);
    else
      ListBox1->Items->Add("SMS: From: " + aSms->Phone + " text: "+ aSms->Text);
  }

  ListBox1->TopIndex = ListBox1->Count - 1;
  ListBox1->ItemIndex = ListBox1->Count - 1;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::nrGsm1SmsSent(TObject *Sender, AnsiString aMem,
      int idSms, TnrPduSms *aSms)
{
  ListBox1->Items->Add("sms has been sent");
  ListBox1->TopIndex = ListBox1->Count - 1;
  ListBox1->ItemIndex = ListBox1->Count - 1;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormShow(TObject *Sender)
{
  for(int i = 0; i < lvInfo->Items->Count; i++){
    lvInfo->Items->Item[i]->SubItems->Add("...");
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button7Click(TObject *Sender)
{
  nrGsm1->PhoneListType = (TnrPhoneBookType)ePhoneBookType->ItemIndex;
  nrGsm1->PhoneBookRead();
  Label8->Caption = "Reading ... ";

}
//---------------------------------------------------------------------------

void __fastcall TForm1::nrGsm1PhoneListFinish(TObject *Sender)
{
  TListItem *itm;
  lvPhones->Clear();
  for(int i = 0; i < nrGsm1->PhoneList->Count; i++){
    itm = lvPhones->Items->Add();
    itm->Caption = nrGsm1->PhoneList->Item[i]->Caption;
    itm->SubItems->Add(nrGsm1->PhoneList->Item[i]->Phone);
  }
  Label8->Caption = "Ready. Total:" + IntToStr(nrGsm1->PhoneList->Count);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::nrGsm1PhoneListProgress(TObject *Sender,
      bool &flBreak)
{
  Label8->Caption = "Read :" + IntToStr(nrGsm1->PhoneList->Count);
}
//---------------------------------------------------------------------------

