//---------------------------------------------------------------------------

#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "FIT.h"
#include "Data.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFitForm *FitForm;
//---------------------------------------------------------------------------
__fastcall TFitForm::TFitForm(TComponent* Owner)
  : TForm(Owner)
{
  FitMemo->Clear();
  ResChart->LeftAxis->LabelStyle = talValue;      //Value in the Label
  ResChart->BottomAxis->LabelStyle = talValue;

  if (!VAR == NULL) VAR->Free();
  VAR = new TLineSeries(this);
  VAR->SeriesColor = clRed;
  VAR->ParentChart = ResChart;


  if (!PTS == NULL) PTS->Free();
  PTS = new TPointSeries(this);
  PTS->SeriesColor = clBlue;
  PTS->ParentChart = ResChart;
  PTS->Pointer->Style = psCircle;
  PTS->Pointer->VertSize = 3;
  PTS->Pointer->HorizSize = 3;
  PTS->Pointer->Brush->Color = clBlue;
  PTS->Pointer->Pen->Color = clBlue;
  PTS->SeriesColor = clRed;

  if (!CEN == NULL) CEN->Free();
  CEN = new TPointSeries(this);
  CEN->SeriesColor = clRed;
  CEN->ParentChart = ResChart;
  CEN->Pointer->Style = psCircle;
  CEN->Pointer->VertSize = 3;
  CEN->Pointer->HorizSize = 3;
  CEN->Pointer->Brush->Color = clRed;
  CEN->Pointer->Pen->Color = clRed;
  CEN->SeriesColor = clRed;
  delfactor = 1.0;
  SelectButtons();
  FindMultiStep = false;
  StepVal = 0.5;
  FindSelBtn->Checked = FindMultiStep;
  StepSelBtn->Checked = !FindMultiStep;
}
//---------------------------------------
void __fastcall TFitForm::SelectButtons()
{
  FitAlp1Btn->Visible = false;
  FitTau1Btn->Visible = false;
  FitAlp2Btn->Hide(); FitTau2Btn->Hide();
  FitAlp3Btn->Hide(); FitTau3Btn->Hide();
  if (DataForm->NoTaus > 1)
  {
    FitAlp1Btn->Visible = true; FitTau1Btn->Visible = true;
  }
  if (DataForm->NoTaus > 2)
  {
    FitAlp2Btn->Show(); FitTau2Btn->Show();
  }
  if (DataForm->NoTaus > 3)
  {
    FitAlp3Btn->Show(); FitTau3Btn->Show();
  }
}

//---------------------------------------------------------------------------
void __fastcall TFitForm::FitFoBtnClick(TObject *Sender)
{
  if(!FindMultiStep)DataForm->FitfoFRR();
  else
  {
    DataForm->ResetDel(0.2);
    float xo = DataForm->fo;
    for(int n = 0; n < 10; n++) DataForm->FitfoFRR();
    DataForm->fo = xo +  (DataForm->fo - xo) * StepVal;
  }
  DataForm->ShowFitBtnClick(Sender);
}

void __fastcall TFitForm::FitFmBtnClick(TObject *Sender)
{
  if(!FindMultiStep)DataForm->FitfmFRR();
  else
  {
    DataForm->ResetDel(0.2);
    float xo = DataForm->fm;
    for(int n = 0; n < 10; n++) DataForm->FitfmFRR();
    DataForm->fm = xo +  (DataForm->fm - xo) * StepVal;
  }
  DataForm->ShowFitBtnClick(Sender);
}

void __fastcall TFitForm::FitSigBtnClick(TObject *Sender)
{
  if(!FindMultiStep)DataForm->FitsigFRR();
  else
  {
    DataForm->ResetDel(0.2);
    float xo = DataForm->sig;
    for(int n = 0; n < 10; n++) DataForm->FitsigFRR();
    DataForm->sig = xo + (DataForm->sig - xo) * StepVal;
  }
  DataForm->ShowFitBtnClick(Sender);
}

void __fastcall TFitForm::FitCBtnClick(TObject *Sender)
{
  if(!FindMultiStep)DataForm->FitC();
  else
  {
    DataForm->ResetDel(0.2);
    float xo = DataForm->CStart;
    for(int n = 0; n < 10; n++) DataForm->FitC();
    DataForm->CStart = xo + (DataForm->CStart - xo) * StepVal;
  }
  DataForm->ShowFitBtnClick(Sender);
}

void __fastcall TFitForm::FitpBtnClick(TObject *Sender)
{
  if(!FindMultiStep)DataForm->FitpFRR();
  else
  {
    DataForm->ResetDel(0.2);
    float xo = DataForm->p;
    for(int n = 0; n < 10; n++) DataForm->FitpFRR();
    DataForm->p = xo + (DataForm->p - xo) * StepVal;
  }
  DataForm->ShowFitBtnClick(Sender);
}

void __fastcall TFitForm::FitTau0BtnClick(TObject *Sender)
{
  int ntau;
  if (Sender == FitTau0Btn) ntau = 0;
  if (Sender == FitTau1Btn) ntau = 1;
  if (Sender == FitTau2Btn) ntau = 2;
  if (Sender == FitTau3Btn) ntau = 3;
  //if(!FindMultiStep)DataForm->Fittau();
  else
  {
    DataForm->ResetDel(0.2);
    float xo = DataForm->tau[ntau];
    //for(int n = 0; n < 10; n++) DataForm->Fittau();
    DataForm->tau[ntau] = xo +  (DataForm->tau[ntau] - xo) * StepVal;
  }
  DataForm->ShowFitBtnClick(Sender);
}

void __fastcall TFitForm::FitAlp0BtnClick(TObject *Sender)
{
  int nalp;
  if (Sender == FitAlp0Btn) nalp = 0;
  if (Sender == FitAlp1Btn) nalp = 1;
  if (Sender == FitAlp2Btn) nalp = 2;
  if (Sender == FitAlp3Btn) nalp = 3;
  if(!FindMultiStep)DataForm->FitalpFRR(nalp);
  else
  {
    DataForm->ResetDel(0.2);
    float xo = DataForm->alp[nalp];
    for(int n = 0; n < 10; n++) DataForm->FitalpFRR(nalp);
    DataForm->alp[nalp] = xo +  (DataForm->alp[nalp] - xo) * StepVal;
  }
  DataForm->ShowFitBtnClick(Sender);
}

void __fastcall TFitForm::RestDelBtnClick(TObject *Sender)
{
  DataForm->ResetDel(0.2);
}
//---------------------------------------------------------------------------

void __fastcall TFitForm::StepValEdChange(TObject *Sender)
{
  StepVal = atof(StepValEd->Text.c_str());
}
//---------------------------------------------------------------------------

void __fastcall TFitForm::StepSelBtnClick(TObject *Sender)
{
  FindMultiStep = FindSelBtn->Checked;
}
//---------------------------------------------------------------------------



//---------------------------------------------------------------------------

