/* vim: set sw=3 ts=3 sts=3 expandtab: */
#include "ecdb.h"

typedef struct Erase_Data Erase_Data;
struct Erase_Data
{
   Ecdb_Project *proj;
   Ecdb_Page *page;
};

static void ecdb_erase_progress_handler(void *data, void *buffer,
                                                         unsigned int nbyte);
int ecdb_erase_finished(void *data, int type, void *event);

// FIXME Make this return an error code
int
ecdb_erase_disc(Ecdb_Erase_Project *proj, Ecdb_Page *page)
{
   BurnDriveStatus disc_state;
   pthread_t progress_update;
   Erase_Data *data;

   data = calloc(1, sizeof(Erase_Data));
   if (!data)
   {
      EINA_ERROR_PWARN("ecdb_erase_disc: NULL data!\n");
      return FALSE;
   }
   data->proj = ECDB_PROJECT(proj);
   data->page = page;

   disc_state = burn_disc_get_status(ECDB_PROJECT(proj)->drive->
                                                         tangible[0].drive);
   if (((disc_state == BURN_DISC_FULL)
         || (disc_state == BURN_DISC_APPENDABLE))
         && (burn_disc_erasable(ECDB_PROJECT(proj)->drive->tangible[0].drive)))
   {
      EINA_ERROR_PINFO("Beginning to erase disc!\n");
      ECDB_PROJECT(proj)->pipe = ecore_pipe_add(ecdb_erase_progress_handler,
                                                            data);
      burn_disc_erase(ECDB_PROJECT(proj)->drive->tangible[0].drive,
                                                            proj->quick);
      pthread_create(&progress_update, NULL, ecdb_drive_progress_update, proj);
      pthread_detach(progress_update);
      ECDB_PROJECT(proj)->ev_handler = ecore_event_handler_add
            (ECDB_DRIVE_ACTION_FINISHED, ecdb_erase_finished, data);
      return TRUE;
   }
   else
   {
      EINA_ERROR_PINFO("Disc not suitable for erasing\n");
      return FALSE;
   }
}

static void
ecdb_erase_progress_handler(void *data, void *buffer, unsigned int nbyte)
{
   BurnProgress *p;
   Evas_Object *swallow;
   Erase_Data *edata;
   static int last_sector = 0;
   Edje_Message_Int_Set *progress_msg;

   EINA_ERROR_PDBG("In progress handler, %d, %d\n", sizeof(BurnProgress),
                                                                        nbyte);
   if (nbyte != sizeof(BurnProgress))
   {
      ecore_event_add(ECDB_DRIVE_ACTION_FINISHED, NULL, NULL, NULL);
      EINA_ERROR_PDBG("Adding event to queue.\n");
      last_sector = 0;
      return;
   }
   else
   {
      p = buffer;
   }


   /* Libburn reports p->sector as being 0 right at the end of the job,
    * so store the last sector and use that to determine the proper
    * percentage/sector to set
    */
   if (last_sector <= p->sector)
   {
      last_sector = p->sector;
   }
   else
   {
      last_sector = p->sectors;
   }

   edata = data;
   if (!edata)
   {
      EINA_ERROR_PWARN("ecdb_erase_progress_handler: NULL edata!\n");
      return;
   }

   if ((!edata->page) || (!edata->proj))
   {
      EINA_ERROR_PWARN("ecdb_erase_progress_handler: NULL page or proj!\n");
      return;
   }

   switch (edata->proj->type)
   {
      case ECDB_ERASE_PROJECT:
         swallow = edata->page->erase;
         break;

      default:
         EINA_ERROR_PWARN("ecdb_erase_progress_handler: "
                                          "Unrecognized project type!\n");
         return;
   }

   progress_msg = alloca(sizeof(Edje_Message_Int_Set) + (2 * sizeof(int)));
   progress_msg->count = 3;
   progress_msg->val[0] = (int)((double)(last_sector + 1) /
                  (double)p->sectors * 100.0);
   progress_msg->val[1] = last_sector;
   progress_msg->val[2] = p->sectors;
   edje_object_message_send(swallow, EDJE_MESSAGE_INT_SET, 0, progress_msg);
}

int
ecdb_erase_finished(void *data, int type, void *event)
{
   Erase_Data *ed;
   Ecdb_Erase_Project *proj;

   ed = data;
   proj = ECDB_ERASE(ed->proj);

   burn_drive_release(ECDB_PROJECT(proj)->drive->tangible[0].drive, 0);
   burn_drive_info_free(ECDB_PROJECT(proj)->drive->tangible);
   ecore_event_handler_del(ECDB_PROJECT(proj)->ev_handler);
   ecore_pipe_del(ECDB_PROJECT(proj)->pipe);
   ecdb_erase_cleanup(ed->page);
   FREE(ed);

   return TRUE;
}

