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

#include <vcl.h>
#pragma hdrstop

#include "SaveToXlsThread.h"
#include "DataModule.h"
#include "MainForm.h"

#pragma package(smart_init)

WORD CXlsBof[6]    = {0x809, 8, 0, 0x10, 0, 0};
WORD CXlsEof[2]    = {0x0A, 0};
WORD CXlsLabel[6]  = {0x204, 0, 0, 0, 0, 0};
WORD CXlsNumber[5] = {0x203, 14, 0, 0, 0};
WORD CXlsRk[5]     = {0x27E, 10, 0, 0, 0};

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

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall SaveToExlThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------


__fastcall SaveToXlsThread::SaveToXlsThread(bool CreateSuspended,int num)
	: TThread(CreateSuspended)
{
     Priority = tpNormal;
     FreeOnTerminate = true;
     ListViewNum = num;
}
//---------------------------------------------------------------------------
void __fastcall SaveToXlsThread::Execute()
{
     Synchronize(SaveExl);

     switch(ListViewNum) {
       case 1:
              Form1->F1_ExcelSaveThread = NULL;
              if(Form1->F1_Autosave_CB->Tag){
                 DModule->F1_DataCount = 0;
                 Form1->F1_ListView->Clear();
                 Form1->F1_Autosave_CB->Tag = 0;
                 DModule->F1_Timer->Enabled = true;
              }
              break;
       case 2:
              Form1->F2_ExcelSaveThread = NULL;
              if(Form1->F2_Autosave_CB->Tag){
                 DModule->F2_DataCount = 0;
                 Form1->F2_ListView->Clear();
                 Form1->F2_Autosave_CB->Tag = 0;
                 DModule->F2_Timer->Enabled = true;
              }
              break;
       case 3:
              Form1->F3_ExcelSaveThread = NULL;
              if(Form1->F3_Autosave_CB->Tag){
                 DModule->F3_DataCount = 0;
                 Form1->F3_ListView->Clear();
                 Form1->F3_Autosave_CB->Tag = 0;
                 DModule->F3_Timer->Enabled = true;
              }
              break;
       case 4:
              Form1->F4_ExcelSaveThread = NULL;
              if(Form1->F4_Autosave_CB->Tag){
                 DModule->F4_DataCount = 0;
                 Form1->F4_ListView->Clear();
                 Form1->F4_Autosave_CB->Tag = 0;
                 DModule->F4_Timer->Enabled = true;
              }
              break;
      }
}
//---------------------------------------------------------------------------

void __fastcall SaveToXlsThread::XlsBeginStream(TStream *XlsStream, const WORD BuildNumber)
{
    CXlsBof[4] = BuildNumber;
    XlsStream->WriteBuffer(CXlsBof, sizeof(CXlsBof));
}
//---------------------------------------------------------------------------

void __fastcall SaveToXlsThread::XlsEndStream(TStream *XlsStream)
{
    XlsStream->WriteBuffer(CXlsEof, sizeof(CXlsEof));
}
//--------------------------------------------------------------------------------

void __fastcall SaveToXlsThread::XlsWriteCellNumber(TStream *XlsStream, const WORD ACol,
const WORD ARow, const double AValue)
{
    CXlsNumber[2] = ARow;
    CXlsNumber[3] = ACol;
    XlsStream->WriteBuffer(CXlsNumber, sizeof(CXlsNumber));
    XlsStream->WriteBuffer(&AValue, 8);
}
//------------------------------------------------------------------------------

void __fastcall SaveToXlsThread::XlsWriteCellLabel(TStream *XlsStream, const WORD ACol,
const WORD ARow, const AnsiString AValue)
{
    WORD L = AValue.Length();
    CXlsLabel[1] = 8 + L;
    CXlsLabel[2] = ARow;
    CXlsLabel[3] = ACol;
    CXlsLabel[5] = L;
    XlsStream->WriteBuffer(CXlsLabel, sizeof(CXlsLabel));
    XlsStream->WriteBuffer(AValue.c_str(), L);
}
//------------------------------------------------------------------------------

void __fastcall SaveToXlsThread::SaveExl()
{
  int   i;
  int   j;
  int   EndCount;

  AnsiString strT1;
  AnsiString strXls;
  AnsiString SaveFileName;
  AnsiString DateName;
  AnsiString TempData[8];
  AnsiString Temp;

  TListView * MyListView;
  TCheckBox * MyCB;
  TProgressBar * MyPB;

  switch(ListViewNum) {
   case 1:
          MyListView = Form1->F1_ListView;
          MyCB = Form1->F1_Autosave_CB;
          SaveFileName = Form1->F1_SaveFileName;
          MyPB = Form1->F1_PB;
          break;
   case 2:
          MyListView = Form1->F2_ListView;
          MyCB = Form1->F2_Autosave_CB;
          SaveFileName = Form1->F2_SaveFileName;
          MyPB = Form1->F2_PB;
          break;
   case 3:
          MyListView = Form1->F3_ListView;
          MyCB = Form1->F3_Autosave_CB;
          SaveFileName = Form1->F3_SaveFileName;
          MyPB = Form1->F3_PB;
          break;
   case 4:
          MyListView = Form1->F4_ListView;
          MyCB = Form1->F4_Autosave_CB;
          SaveFileName = Form1->F4_SaveFileName;
          MyPB = Form1->F4_PB;
          break;
  }

  EndCount = MyListView->Items->Count;
  MyPB->Max = EndCount;

  if(!MyCB->Tag) {
     strXls = ChangeFileExt(SaveFileName, ".xls");
     Form1->StatusBar1->Panels->Items[2]->Text = strXls;
  }else {
          DateTimeToString(DateName, "_yyyy-mm-dd_hh-mm-ss", Now());
          SaveFileName = Form1->App_Dir+ "\\AutoSave\\F"+IntToStr(ListViewNum) + DateName;
          strXls = ChangeFileExt(SaveFileName,".xls");
  }

  TFileStream *FStream = new TFileStream(strXls, fmCreate);

try {

    XlsBeginStream(FStream, 0);

    for(j = 1; j<9;j++)
      XlsWriteCellLabel(FStream,j-1,0,MyListView->Column[j]->Caption);


    for(i = 1; i<EndCount; i++)
    {
       Temp = MyListView->Items->Item[i-1]->SubItems->Text;
       DivideData(Temp,TempData);

       XlsWriteCellLabel(FStream,0,i,TempData[j]);
       for(j = 1; j<8;j++)
           XlsWriteCellNumber(FStream,j,i,StrToFloat(TempData[j]));
       MyPB->Position++;
    }
  }//try
   __finally {
         XlsEndStream(FStream);
         MyPB->Position = 0;
         delete FStream;
   }
}

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

void __fastcall SaveToXlsThread::DivideData(AnsiString Tp,AnsiString TD[])
{
  AnsiString Temp = "";
  int PointCount=0;

  for(int i=0;i< Tp.Length(); i++){
      if(Tp.SubString(i+1,1) == '\n'){
          TD[PointCount] = Temp;
          PointCount ++;
          Temp = "";
      }else if(Tp.SubString(i+1,1) != '\r')
                Temp += Tp.SubString(i+1,1);
  }

}

