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

#include <clx.h>
#pragma hdrstop

#include "Main.h"
#include "SQL.h"
#include "Detail.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.xfm"
TOverviewWin *OverviewWin;
//---------------------------------------------------------------------------
__fastcall TOverviewWin::TOverviewWin(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TOverviewWin::FormCreate(TObject *Sender)
{
   // Default sort
   const int defaultChanMin = 1;
   const int defaultChanMax = 9;

   // Channel Combos
   ChannelDataSet->Active = true;
   ChannelMinCombo->KeyValue = defaultChanMin;
   ChannelMaxCombo->KeyValue = defaultChanMax;

   TimeMinCombo->Text = TDateTime::CurrentTime().FormatString("hh:nn");
   TimeMaxCombo->ItemIndex = 0; // 00:00
   TDateTime today = TDateTime::CurrentDate();
   for (TDateTime i = TDateTime::CurrentDate(),
        end = TDateTime::CurrentDate() + 7;
        i < end;
        i++)
   {
     DateMinCombo->Items->Add(i);
     DateMaxCombo->Items->Add(i);
   }
   DateMinCombo->ItemIndex = 0; // today (current time)
   DateMaxCombo->ItemIndex = 1; // tomorrow (midnight, i.e. till end of today)
   RefreshActionExecute(this);

   AutoRefreshCheck->Checked = true;
        // AutoRefreshAction triggers *before* FormCreate()!
}
//---------------------------------------------------------------------------

void __fastcall TOverviewWin::DetailActionExecute(TObject *Sender)
{
  int chanid = ProgramDataSet->FieldValues["chanid"];
  AnsiString start_str = ProgramDataSet->FieldValues["starttime"];
  TDateTime start_dt(start_str);

  TDetailWin* win = new TDetailWin(this);
  win->SetID(chanid, start_dt);
//  win->ShowModal();
//  delete win;
  // XXX leaks. Same in Detail.cpp
  win->Show();
}
//---------------------------------------------------------------------------

void __fastcall TOverviewWin::RefreshActionExecute(TObject *Sender)
{
   // Time and channel
   AnsiString timemin =
        TDateTime(AnsiString(DateMinCombo->Text)).FormatString("yyyy-mm-dd") + " " +
        TDateTime(AnsiString(TimeMinCombo->Text)).FormatString("hh:nn:ss");
   AnsiString timemax =
        TDateTime(AnsiString(DateMaxCombo->Text)).FormatString("yyyy-mm-dd") + " " +
        TDateTime(AnsiString(TimeMaxCombo->Text)).FormatString("hh:nn:ss");
   AnsiString chanmax = ChannelMaxCombo->KeyValue.IsNull()
                        ? 0 : (int) ChannelMaxCombo->KeyValue;
   AnsiString chanmin = ChannelMinCombo->KeyValue.IsNull()
                        ? 0 : (int) ChannelMinCombo->KeyValue;

   // Category type
   int cattype = CatTypeCombo->ItemIndex;
   AnsiString cattype_str;
   // do nothing for 0 (All)
   if (cattype == 1)
     cattype_str = "movie";
   else if (cattype == 2)
     cattype_str = "series";
   else if (cattype == 3)
     cattype_str = "tvshow";
   else if (cattype == 4)
     cattype_str = "sports";
   AnsiString clause;
   if (!cattype_str.IsEmpty())
     clause.sprintf("category_type = '%s'", cattype_str);

   // Apply
   ProgramDataSet->Params->ParamByName("timemin")->AsString = timemin;
   ProgramDataSet->Params->ParamByName("timemax")->AsString = timemax;
   ProgramDataSet->Params->ParamByName("chanmin")->AsString = chanmin;
   ProgramDataSet->Params->ParamByName("chanmax")->AsString = chanmax;
   ProgramDataSet->Params->ParamByName("clause")->AsString = "";
//   if (!clause.IsEmpty())
//     ProgramDataSet->Params->ParamByName("clause")->AsString = "AND " + clause;
   ProgramDataSet->Filter = clause;
   ProgramDataSet->Filtered = true;
//   ProgramDataSet->CommandText = "select program.chanid as chanid, channum, callsign, name, title, subtitle, category, starttime, endtime from program left join channel on program.chanid = channel.chanid where program.chanid >= :minchan and program.chanid <= :maxchan and endtime >= :timemin  and starttime <= :timemax order by starttime, program.chanid";
   ProgramDataSet->Active = false;
   ProgramDataSet->Active = true;

   // Update UI
   CountLabel->Caption = String(ProgramDataSet->RecordCount);
}
//---------------------------------------------------------------------------

void __fastcall TOverviewWin::AutoRefreshActionExecute(TObject *Sender)
{
  if (AutoRefreshCheck->Checked)
    RefreshActionExecute(Sender);
}
//---------------------------------------------------------------------------

// broken
void __fastcall TOverviewWin::SearchActionExecute(TObject *Sender)
{
   //SearchDataSet->Params->ParamByName("term")->AsString = AnsiString(SearchEdit->Text);
   //SearchDataSet->Active = false;
   //SearchDataSet->Active = true;
   //ProgramGrid->DataSource = SearchDataSource;
   AnsiString query;
   query.sprintf("select program.chanid as chanid, channum, callsign, name, title, subtitle, category, starttime, endtime from program left join channel on program.chanid = channel.chanid where title LIKE '%s' order by starttime, program.chanid",
                 SearchEdit->Text);
   ProgramDataSet->Active = false;
   ProgramDataSet->CommandText = query;
   ProgramDataSet->Active = true;
}
//---------------------------------------------------------------------------

