#include <sys/time.h>
#include <iostream>

#include <TH2F.h>
#include <TCanvas.h>
#include <TROOT.h>
#include <TStyle.h>
#include <TColor.h>
#include <TBenchmark.h>

#include "ISlowControlDatabase.hxx"
#include "ITPCTemperature.hxx"
#include "ITPCTempAvg.hxx"


void mypalette(){
  int nbCol=36;
  int offset=0;
 
  int iJul;
  TColor **colJul;
  colJul = new TColor*[36];
  for (iJul=0;iJul<36;iJul++) colJul[iJul] = new TColor(999-iJul,0.0,0.7,0.7);
 
  colJul[0]->SetRGB(1.0,0.9,1.0);
  colJul[1]->SetRGB(1.0,0.8,1.0);
  colJul[2]->SetRGB(1.0,0.6,1.0);
  colJul[3]->SetRGB(1.0,0.0,1.0);
  colJul[4]->SetRGB(0.88,0.0,1.0);
  colJul[5]->SetRGB(0.7,0.0,1.0);
  colJul[6]->SetRGB(0.55,0.0,1.0);
  colJul[7]->SetRGB(0.0,0.0,0.98);
  colJul[8]->SetRGB(0.0,0.5,1.0);
  colJul[9]->SetRGB(0.0,0.65,1.0);
  colJul[10]->SetRGB(0.0,0.8,1.0);
  colJul[11]->SetRGB(0.0,0.9,1.0);
  colJul[12]->SetRGB(0.0,1.0,1.0);
  colJul[13]->SetRGB(0.0,0.97,0.85);
  colJul[14]->SetRGB(0.0,0.95,0.7);
  colJul[15]->SetRGB(0.0,0.9,0.58);
  colJul[16]->SetRGB(0.0,0.87,0.0);
  colJul[17]->SetRGB(0.5,0.9,0.0);
  colJul[18]->SetRGB(0.65,0.93,0.0);
  colJul[19]->SetRGB(0.80,0.96,0.0);
  colJul[20]->SetRGB(0.9,0.98,0.0);
  colJul[21]->SetRGB(1.0,1.0,0.0);
  colJul[22]->SetRGB(1.0,0.95,0.0);
  colJul[23]->SetRGB(1.0,0.90,0.0);
  colJul[24]->SetRGB(1.0,0.85,0.0);
  colJul[25]->SetRGB(1.0,0.78,0.0);
  colJul[26]->SetRGB(1.0,0.68,0.0);
  colJul[27]->SetRGB(1.0,0.58,0.0);
  colJul[28]->SetRGB(1.0,0.48,0.0);
  colJul[29]->SetRGB(1.0,0.0,0.0);
  colJul[30]->SetRGB(0.92,0.0,0.0);
  colJul[31]->SetRGB(0.85,0.0,0.0);
  colJul[32]->SetRGB(0.74,0.0,0.0);
  colJul[33]->SetRGB(0.62,0.0,0.0);
  colJul[34]->SetRGB(0.5,0.0,0.0);
  colJul[35]->SetRGB(0.0,0.0,0.0);
  
  int tabColJul[36];
  for (iJul=0;iJul<36;iJul++) tabColJul[iJul]=999-iJul;
  gStyle->SetPalette(nbCol,tabColJul+offset);
}

void help(){

  cout << "Usage:"<<endl;
  cout << "    oaSlowControlDatabase_toolTpcTemperature.exe -s YYYY MM DD HH MM SS -e YYYY MM DD HH MM SS [-m -1]"<< endl;
  cout << "      -s specify year month day minute and second of start period"<< endl;
  cout << "      -e specify year month day minute and second of start period"<< endl;
  cout << "      -m specify maximum number of samples to use (default=-1 for all samples)"<<endl;
  return;


}


/// Program to make temperature plots for a time
/// range.
int main(int argc,char **argv){
  gROOT->SetStyle("Pub");
  mypalette();
  gStyle->SetOptTitle(1);

  int syear=0;
  int smonth=0;
  int sday=0;
  int shour=0;
  int smin=0;
  int ssec=0;

  int eyear=0;
  int emonth=0;
  int eday=0;
  int ehour=0;
  int emin=0;
  int esec=0;

  int maxsamples=-1;

  // ***** process command line arguments ... 
  Int_t i=1;
  while ( i < argc ) {
    if ( strcmp(argv[i],"-h") == 0 || argc < 14 ) {
      help();
      exit(1);
    }
    else if ( strcmp(argv[i],"-m") == 0 ) {
      maxsamples = atoi( argv[++i] );
    }
    else if ( strcmp(argv[i],"-e") == 0 ) {
      eyear  = atoi( argv[++i] );
      emonth = atoi( argv[++i] );
      eday   = atoi( argv[++i] );
      ehour   = atoi( argv[++i] );
      emin   = atoi( argv[++i] );
      esec   = atoi( argv[++i] );
    }
    else if ( strcmp(argv[i],"-s") == 0 ) {
      syear  = atoi( argv[++i] );
      smonth = atoi( argv[++i] );
      sday   = atoi( argv[++i] );
      shour = atoi( argv[++i] );
      smin   = atoi( argv[++i] );
      ssec   = atoi( argv[++i] );
    }
    i++;
  }
  
  if ( syear==0 || eyear==0 ){
    std::cout<<"ERROR. One of date parameters missing."<<std::endl;
    help();
    exit(1);
  }
       
  TBenchmark timer;
  timer.Start("all");

  std::cout<<"toolTpcTemperature requesting "<<maxsamples<<" temperatures"<<std::endl;

  // plot data over range requested
  ITPCTempAvg TPCTPAvg( maxsamples,  
			syear, smonth, sday, shour, smin, ssec,
			eyear, emonth, eday, ehour, emin, esec );

  // print out about results
  TPCTPAvg.Print();

  // plot histogram of mean and rms temperature
  TCanvas* tc=new TCanvas("canvas2","canvas2",1400,700);
  tc->Clear();
  gStyle->SetOptTitle(1);
  char apname[256];
  sprintf(apname, "TPC Tavg (degC) from %d/%d/%d %d:%d:%d to %d/%d/%d %d:%d:%d",
	  syear, smonth, sday, shour, smin, ssec,
	  eyear, emonth, eday, ehour, emin, esec );
  
  TPCTPAvg.GetAvgTH2F()->SetTitle(apname);  
  TPCTPAvg.GetAvgTH2F()->Draw("colz");
  sprintf(apname, "TpcTavg_%d%d%d%d%d%d_%d%d%d%d%d%d.eps",
	  syear, smonth, sday, shour, smin, ssec,
	  eyear, emonth, eday, ehour, emin, esec );
  tc->Print(apname);

  tc->Clear();
  sprintf(apname, "TPC Trms (degC) from %d/%d/%d %d:%d:%d to %d/%d/%d %d:%d:%d",
	  syear, smonth, sday, shour, smin, ssec,
	  eyear, emonth, eday, ehour, emin, esec );
  TPCTPAvg.GetRMSTH2F()->SetTitle(apname);
  TPCTPAvg.GetRMSTH2F()->Draw("colz");
  sprintf(apname, "TpcTrms_%d%d%d%d%d%d_%d%d%d%d%d%d.eps",
	  syear, smonth, sday, shour, smin, ssec,
	  eyear, emonth, eday, ehour, emin, esec );
  tc->Print(apname);

  // make gifsicle of temperature readings
  TPCTPAvg.MakeGifSicle();


  timer.Stop("all");
  float cputime = timer.GetCpuTime("all");
  float realtime = timer.GetRealTime("all");
  std::cout<<"Execution took "<<cputime<<" CPU seconds, and "
	   <<realtime<<" Real seconds.\nDone!"<<std::endl;
}