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

#include <clx.h>
#pragma hdrstop

#include "Card.h"
#include "SQL.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.xfm"
TCardWin *CardWin;
//---------------------------------------------------------------------------
__fastcall TCardWin::TCardWin(TComponent* Owner)
  : TForm(Owner)
{
}

void __fastcall TCardWin::SetCardID(int a_cardid)
{
  cardid = a_cardid;

  // filters
  AnsiString filter;
  filter.sprintf("cardid=%d", cardid);
  CardDataSet->Filter = filter;
  CardDataSet->Active = true;
  InputDataSet->Filter = filter;
  InputDataSet->Active = true;

  SourceDataSet->Active = true;
  ChannelDataSet->Active = true;

  // UI
  CardtypeComboChange(this);
}

void __fastcall TCardWin::NewCard()
{
  CardDataSet->Active = true;
  CardDataSet->Append();
  cardid = SQLModule->GetNewID("capturecard", "cardid");
  CardDataSet->FieldValues["cardid"] = cardid;

  SetCardID(cardid);
}
//---------------------------------------------------------------------------
void __fastcall TCardWin::ApplyActionExecute(TObject *Sender)
{
  CardDataSet->ApplyUpdates(0);
  InputDataSet->ApplyUpdates(0);
}
//---------------------------------------------------------------------------


void __fastcall TCardWin::InputDataSetNewRecord(TDataSet *DataSet)
{
  DataSet->FieldValues["cardid"] = cardid;
  DataSet->FieldValues["cardinputid"] = SQLModule->GetNewID("cardinput", "cardinputid");
  DataSet->FieldValues["tunechan"] = 0; // XXX
}
//---------------------------------------------------------------------------

void __fastcall TCardWin::CardtypeComboChange(TObject *Sender)
{
  WideString type = CardtypeCombo->Text;
  if (type == "V4L")
  {
    AnalogPane->Visible = true;
    DVBPane->Visible = false;
    AudioDevLabel->Visible = true;
    AudioDevCombo->Visible = true;
    AudioRateLabel->Visible = true;
    AudioRateCombo->Visible = true;
  }
  else if (type == "MPEG")
  {
    AnalogPane->Visible = true;
    DVBPane->Visible = false;
    AudioDevLabel->Visible = false;
    AudioDevCombo->Visible = false;
    AudioRateLabel->Visible = false;
    AudioRateCombo->Visible = false;
  }
  else if (type == "DVB")
  {
    AnalogPane->Visible = false;
    DVBPane->Visible = true;
  }
  else
  {
    AnalogPane->Visible = false;
    DVBPane->Visible = false;
  }
}
//---------------------------------------------------------------------------
bool __fastcall TCardWin::isDirty()
{
  // see TChannelWin::isDirty()
  return CardDataSet->State == dsEdit || CardDataSet->ChangeCount > 0 ||
         InputDataSet->State == dsEdit || InputDataSet->ChangeCount > 0;
}

void __fastcall TCardWin::FormCloseQuery(TObject *Sender, bool &CanClose)
{
  if (!isDirty())
  {
    CanClose = true;
    return;
  }

  int request = MessageDlg("Save changes",
                           "Do you want to save your changes in the database?",
                           mtConfirmation,
                           TMsgDlgButtons() << mbYes << mbNo << mbCancel,
                           0, mbYes);
  if (request == mrYes)
  {
    ApplyActionExecute(this);
    CanClose = true;
  }
  else if (request == mrNo)
  {
    CardDataSet->CancelUpdates();
    InputDataSet->CancelUpdates();
    CanClose = true;
  }
  else
    CanClose = false;
}
//---------------------------------------------------------------------------

void __fastcall TCardWin::SourceComboClick(TObject *Sender)
{
  Variant sourceid_var = InputDataSet->FieldValues["sourceid"];
  if (sourceid_var.IsNull())
    return;
  int sourceid = sourceid_var;
  ChannelDataSet->Filter = AnsiString().sprintf("sourceid=%d", sourceid);
  ChannelDataSet->Active = true;
}
//---------------------------------------------------------------------------

void __fastcall TCardWin::InputDataSetAfterScroll(TDataSet *DataSet)
{
  //SourceCombo->Clear();
  SourceComboClick(this);
}
//---------------------------------------------------------------------------

void __fastcall TCardWin::SourceDataSetAfterScroll(TDataSet *DataSet)
{
  SourceComboClick(this);
}
//---------------------------------------------------------------------------

