/*  gfocustimer
 *  Copyright (C) 1999 James Cameron, <quozl@us.netrek.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "data.h"
#include "entry.h"
#include "total.h"
#include "charge.h"
#include "observation.h"


/* main window */
extern struct mw mw;

/* preferences data */
extern struct pd pd;

/* tag for timeout context */
int charge_timeout_tag  = 0;


/* choose an update timeout based on the accuracy chosen by user */
int charge_rate ()
{
  switch (pd.format) {
  case FORMAT_HH_MM:
    return 1000*60;		/* one minute	*/
  case FORMAT_HH_MM_SS:
    return 1000;		/* one second	*/
  case FORMAT_H_HH:
    return 1000*60*60/100;	/* hundredth of an hour (36 seconds)	*/
  case FORMAT_H_HHHH:
    return 3600;		/* thousandth of an hour (3.6 seconds)	*/
  default:
    return 1000;
  }
}


/* update the current entry by stopping and starting charging */
void charge_update()
{
  /* check to see if we are charging */
  if (mw.charge) {

    time_t now = time(NULL);
    observation_update (now);
  }
}


/* called frequently to update the display */
gint charge_timeout (gpointer data)
{
  {
    static int day =-1;
    time_t now = time ( NULL );
    struct tm *tm = localtime ( &now );
    if (day==-1) {
      day=tm->tm_mday;
    }
    if (day!=tm->tm_mday) {
      /*It's a whole new day, reset counters.*/
      entry_reset();
      entry_save();
      entry_refresh();
      day=tm->tm_mday;
    }
  }

  /* ???: temporary automatic save, remove when preferences done */
  {
    static int saver = 60;

    if (saver-- <= 0) {
      saver = 60;
      entry_save();
    }
  }

  charge_update();

  /* repeat the timer */
  return TRUE;
}


/* start running the timeout process */
void charge_schedule()
{
  charge_timeout_tag = gtk_timeout_add (charge_rate(), 
					charge_timeout, NULL);
}


/* stop running the timeout process */
void charge_deschedule()
{
  if (charge_timeout_tag == 0) return;
  gtk_timeout_remove (charge_timeout_tag);
  charge_timeout_tag = 0;
}


/* if the charging timeout is active, reschedule it */
void charge_reschedule()
{
  if (charge_timeout_tag != 0) {
    gtk_timeout_remove (charge_timeout_tag);
    charge_schedule();
  }
}


/* called when the Charge Time toggle button is toggled */
void
on_charging_toggled                    (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
  time_t now = time(NULL);

  /* is the toggle button being turned on or off? */
  if (gtk_toggle_button_get_active(togglebutton)) {

    /* button is turning on, make sure a row was selected too */
    if (mw.row == -1) {
      /* select the top row */
      gtk_clist_select_row (mw.list, 0, 0);
    } else {
      /* re-highlight the current row */
      entry_highlight_active (mw.row);
    }

    /* start observation */
    observation_end (now);
    observation_start_manual (now, mw.ed);

    /* start running the timeout process */
    charge_schedule();

    mw.charge = 1;
    pd.charging = TRUE;
  } else {

    /* button is turning off, end the charging interval */
    observation_end (now);
    observation_start_manual (now, NULL);

    /* stop running the timeout process */
    charge_deschedule();

    /* deselect the current row */
    if (mw.row != -1) {
      entry_highlight_inactive (mw.row);
    }

    mw.charge = 0;
    pd.charging = FALSE;
  }
}
