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

#include <vcl.h>
#pragma hdrstop

#include "FormBTRDUnit.h"

#include "RtlBluetoothMP_scriptExec_base.h"
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CGAUGES"
#pragma resource "*.dfm"
TForm_BTRD *Form_BTRD;
//------------------- --------------------------------------------------------
__fastcall TForm_BTRD::TForm_BTRD(TComponent* Owner)
        : TForm(Owner)
{
        pBluetoothModule=NULL;
        Memo_msg->Lines->Clear();
        AnsiString cur_exe_path =  ExtractFilePath(Application->ExeName);
        Edit_savepath->Text=cur_exe_path+"Result.csv";
}
//---------------------------------------------------------------------------
static int rd_mode_show(int *data,char *s)
{
     String dstr;
     Form_BTRD->CGauge_RUN->MaxValue=data[1];
     Form_BTRD->CGauge_RUN->MinValue =0;
     Form_BTRD->CGauge_RUN->Progress= data[0];

     dstr.sprintf("Run Line %d(%d) => %s",data[0],data[1],s);
     Form_BTRD->Memo_msg->Lines->Add(dstr);

     return 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm_BTRD::BitBtn_SCRIPT_RUNClick(TObject *Sender)
{

        //Load DLL
        int rtn=0;
        String dstr;
	OpenDialog1->Options.Clear();
	OpenDialog1->Title = "Load rd Script"  ;
	OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
	OpenDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
	OpenDialog1->FileName="rd_scrip.txt";
	OpenDialog1->FilterIndex=0;
	if(OpenDialog1->Execute())
	{
                String savestr=  Edit_savepath->Text;
                if (CheckBox_CLEARFILE->Checked)
                {
                 FILE *fp =NULL;
                 fp=fopen(savestr.c_str(),"w+");
                 fclose(fp);
                }
                if (CheckBox_show->Checked){
                rd_mode_callbackshow((void*)rd_mode_show);
                }
                Form_BTRD->CGauge_RUN->Progress=Form_BTRD->CGauge_RUN->MinValue;
                int rtn = rd_mode_scriptfile_Excute(pBluetoothModule,OpenDialog1->FileName.c_str(),savestr.c_str());
                dstr.sprintf(">>rd mode ->result file : %s  excute end....\n",savestr);Memo_msg->Lines->Add(dstr);
                dstr.sprintf(">>rd mode ->script file : %s  excute end....\n",OpenDialog1->FileName);Memo_msg->Lines->Add(dstr);
                Form_BTRD->CGauge_RUN->Progress=Form_BTRD->CGauge_RUN->MaxValue;
        }
        return ;

}
//---------------------------------------------------------------------------
void TForm_BTRD::SetBluetoothModule(BASE_BTMPDLL_MODULE *pBtModule)
{
        pBluetoothModule= pBtModule;
        return ;
}
//---------------------------------------------------------------------------
void __fastcall TForm_BTRD::SpeedButton1Click(TObject *Sender)
{
        int rtn=0;
        String dstr;
	SaveDialog1->Options.Clear();
	SaveDialog1->Title = "save rd Script run result"  ;
	SaveDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
	SaveDialog1->Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
	SaveDialog1->FileName="result_lp.csv";
	SaveDialog1->FilterIndex=0;
	if(SaveDialog1->Execute())
	{
             Edit_savepath->Text=SaveDialog1->FileName;
             //   int rtn = rd_mode_scriptfile_Excute(pBluetoothModule,OpenDialog1->FileName.c_str());
              //  dstr.sprintf(">>rd mode ->script file : %s  excute end....\n",OpenDialog1->FileName);Memo_msg->Lines->Add(dstr);
        }
}
//---------------------------------------------------------------------------



void __fastcall TForm_BTRD::BitBtn_ClearMsgClick(TObject *Sender)
{
        Memo_msg->Lines->Clear();
}
//---------------------------------------------------------------------------




