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

#include <clx.h>
#pragma hdrstop

#include "Main.h"
#include "SQL.h"
#include "Channel.h"
#include "Card.h"
#include "Settings.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.xfm"
TMainWin *MainWin;
//---------------------------------------------------------------------------
__fastcall TMainWin::TMainWin(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TMainWin::FormCreate(TObject *Sender)
{
  SourceDataSet->Active = true;
  CardDataSet->Active = true;
}
//---------------------------------------------------------------------------

void __fastcall TMainWin::SourceEditActionExecute(TObject *Sender)
{
  int sourceid = SourceDataSet->FieldValues["sourceid"];
  TChannelWin* win = new TChannelWin(this);
  win->SetSourceID(sourceid);
  win->ShowModal();
  delete win;
  SourceDataSet->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TMainWin::CardEditActionExecute(TObject *Sender)
{
  int cardid = CardDataSet->FieldValues["cardid"];
  TCardWin* win = new TCardWin(this);
  win->SetCardID(cardid);
  win->ShowModal();
  delete win;
  CardDataSet->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TMainWin::SourceInsertActionExecute(TObject *Sender)
{
  TChannelWin* win = new TChannelWin(this);
  win->NewSource();
  win->ShowModal();
  delete win;
  SourceDataSet->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TMainWin::CardInsertActionExecute(TObject *Sender)
{
  TCardWin* win = new TCardWin(this);
  win->NewCard();
  win->ShowModal();
  delete win;
  CardDataSet->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TMainWin::SourceDeleteActionExecute(TObject *Sender)
{
  int request = MessageDlg("Delete",
                           "Are you sure that you want to delete "
                           "the selected source, all its channels and "
                           "reset the source of the corresponding "
                           "cardinputs?",
                           mtConfirmation,
                           TMsgDlgButtons() << mbYes << mbCancel,
                           0, mbCancel);
  if (request == mrYes)
  {
    int sourceid = SourceDataSet->FieldValues["sourceid"];
    // channels
    SQLModule->Conn->ExecuteDirect(AnsiString().sprintf(
                     "DELETE FROM channel WHERE sourceid=%d", sourceid));
    // XXX channel_dvb
    // reset inputs
    SQLModule->Conn->ExecuteDirect(AnsiString().sprintf(
                     "UPDATE cardinput SET sourceid=0 "
                     "WHERE sourceid=%d", sourceid));
    // source
    SourceDataSet->Delete();
    SourceDataSet->ApplyUpdates(0);
  }
}
//---------------------------------------------------------------------------

void __fastcall TMainWin::CardDeleteActionExecute(TObject *Sender)
{
  int request = MessageDlg("Delete",
                           "Are you sure that you want to delete the "
                           "selected card and its inputs from the database?",
                           mtConfirmation,
                           TMsgDlgButtons() << mbYes << mbCancel,
                           0, mbCancel);
  if (request == mrYes)
  {
    // inputs
    int cardid = CardDataSet->FieldValues["cardid"];
    SQLModule->Conn->ExecuteDirect(AnsiString().sprintf(
                     "DELETE FROM cardinput WHERE cardid=%d", cardid));
    // card
    CardDataSet->Delete();
    CardDataSet->ApplyUpdates(0);
  }
}
//---------------------------------------------------------------------------

void __fastcall TMainWin::SettingsOpenActionExecute(TObject *Sender)
{
  TSettingsWin* win = new TSettingsWin(this);
  win->ShowModal();
  delete win;
}
//---------------------------------------------------------------------------

