//=-- DailyGraphPlugin.cpp - Graphs accesses by day --------------------------=
//
//  This class 
//
//=---------------------------------------------------------------------------=
//  This file is copyright (c) 1997-1998 Chris Lattner
//=---------------------------------------------------------------------------=

#include "DataGraph.h"
#include "DailyGraphPlugin.h"
#include "SerializeEx.h"
#include "Table.h"
#include "VarTable.h"

INIT_PLUGIN(DailyGraphPlugin);

DailyGraphPlugin::DailyGraphPlugin(int &CE) {
  Flags = FAutoFilter | FIgnoreErrors;
  ResetState();
  CE = 0;
}

void DailyGraphPlugin::GetFullName(VarTable &Params, String &fullName, 
                                   int updateFreq) {
  String Filt;
  if (Params["Filter"] != 0) {
    Filt = Params["Filter"]->GetStringValue();
  } else {
    Filt = DefFilter.ToParamStr();
  }

  fullName = String::IntToStr(updateFreq) + Name + "\t" + Filt;
}

void DailyGraphPlugin::ResetState() { 
  DaysInMonth = -1;
  for (int i = 0; i < 31; i++) 
    DayCount[i] = 0;
}

void DailyGraphPlugin::ProcessAccess(AccessFormatPlugin &A) {
  DayCount[A.GetDate().GetDay()-1]++;
  if (DaysInMonth == -1) DaysInMonth = A.GetDate().GetDaysInMonth();
}

void DailyGraphPlugin::LoadState(Serialize &I) {
  unsigned int Version = 0;

  I >> Version;

  // Check version number...
  switch (Version) {
  case 100:                  // Version 1.00
    DeSerializeArray(I, DayCount, 31);
    break;
    
  default:
    cout << "Serialized " << Name << " Page plugin is of a version that "
         << "cannot be deserialized\nwith this version of the plugin.  "
	 << "Reseting state.\n";
    break;
  }
}

void DailyGraphPlugin::SaveState(Serialize &O) {
  O << GetCurrentVersion();  // Serialize the Version number

  SerializeArray(O, DayCount, 31);
}

void DailyGraphPlugin::OutputHTML(ostream &O, VarTable &Params) {
  DataGraph Data(Params);
  if (DaysInMonth == -1) DaysInMonth = 31;

  LinkedList<String> Days, Count;
  int AccessSum = 0, DayMax = 0, DayMin = 0, 
      CurDay = MSOptions::CurDate.GetDay();
  int i = 0;

  Data.SetRowLabelFormat(1);     // Numeric bar labels
  Data.AddColumn(Days);
  Data.AddColumn(Count);

  Days.AddToTail("Day ");
  Count.AddToTail("Hits:");

  for (i = 0; i < DaysInMonth; i++) {  
    if (CurDay >= i) {          // Only include days that have happened so far...
      if (DayCount[i] > DayMax) DayMax = DayCount[i];
      if (DayCount[i] < DayMin) DayMin = DayCount[i];
    }

    AccessSum += DayCount[i];
    Count.AddToTail(String::IntToStr(DayCount[i]));
    Days.AddToTail(String::IntToStr(i+1));
  }

  if (DayMax == 0) DayMax = 1;

  O << "<TABLE width=100% border=0 cellspacing=0 cellpadding=0><TR><TD\n>";
  Data.WriteHTML(O);

  if (CurDay == -1) CurDay = 0;
  O << "<p>\n</TD><TD VALIGN=TOP>" << endl

    << "Average Accesses per Day: <b>" << AccessSum / (CurDay+1) << "</b><br>" << endl
    << "Maximum Accesses per Day: <b>" << DayMax << "</b><br>" << endl
    << "Minimum Accesses per Day: <b>" << DayMin << "</b><br>" << endl
    << "</TD></TR></TABLE><p><p>" << endl;
}
