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

static void _button_cb_return(void *data, Evas_Object *o, const char *emission,
                                                         const char *source);
static void _button_cb_begin(void *data, Evas_Object *o, const char *emission,
                                                         const char *source);
static void _page_hide_finished(void *data, Evas_Object *o,
                                                         const char *emission,
                                                         const char *source);
static void _page_del(void *data, Evas *e, Evas_Object *obj, void *ev);

static void _ecdb_erase_do_erase(Ecdb_Page *page, Ecdb_Erase_Project *proj);
void _ecdb_erase_unmount_cb(void *data, void *reply_data, DBusError *err);

static void
_page_del(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj,
                                                      void *ev __UNUSED__)
{
   Ecdb_Erase_Project *proj;

   proj = evas_object_data_get(obj, "proj");
   if (proj)
      ecdb_erase_project_destroy(proj);
}

static void
_page_hide_finished(void *data, Evas_Object *o,
                                          const char *emission __UNUSED__,
                                          const char *source __UNUSED__)
{
   Evas_Object *gui;

   gui = data;
   edje_object_part_unswallow(gui, o);
   evas_object_hide(o);
}

static void
_button_cb_return(void *data, Evas_Object *o __UNUSED__,
                                             const char *emission __UNUSED__,
                                             const char *source __UNUSED__)
{
   Ecdb_Page *page;
   page = data;

   edje_object_part_text_set(page->erase, "progress_text", "Begin Erase");
   edje_object_part_text_set(page->erase, "progress_percent", "0%");
   edje_object_signal_emit(page->erase, "ecdb,erase_page,hide", "ecdb");
   ecdb_welcome_page_show(page);
}

static void
_button_cb_begin(void *data, Evas_Object *o __UNUSED__,
                                             const char *emission __UNUSED__,
                                             const char *source __UNUSED__)
{
   Ecdb_Erase_Project *proj;
   Ecdb_Page *page;
   int idx;

   page = data;
   proj = evas_object_data_get(page->erase, "proj");

   idx = ecdb_combo_selected_get(ECDB_PROJECT(proj)->drive_combo);
   if (idx < 0)
   {
      EINA_ERROR_PDBG("Choose a drive!\n");
      edje_object_part_text_set(page->erase, "progress_text",
                                          "Choose a Drive!");
      return;
   }

   if (!ecdb_set_project_drive(ECDB_PROJECT(proj), idx))
   {
      EINA_ERROR_PWARN("Drive index doesn't exists!\n");
      edje_object_part_text_set(page->erase, "progress_text",
																	"Drive doesn't exist!");
      return;
   }

   if (!ECDB_PROJECT(proj)->drive->fresh_info)
   {
      EINA_ERROR_PDBG("Insert a disc into the drive!\n");
      edje_object_part_text_set(page->erase, "progress_text",
                                          "Insert a disc into the drive!");
      return;
   }

   if (ECDB_PROJECT(proj)->drive->status & ECDB_DISC_BLANK)
   {
      EINA_ERROR_PDBG("Disc is already blank!\n");
      edje_object_part_text_set(page->erase, "progress_text",
                                          "Disc is already blank!");
      return;
   }

   /* Apparently DBus doesn't tell you when a disc has been mounted
    * or unmounted, so do this every time regardless...
    */
   if (!(ECDB_PROJECT(proj)->drive->status & ECDB_DISC_BLANK))
   {
      ecdb_hal_request_unmount(ECDB_PROJECT(proj)->drive,
                                                   _ecdb_erase_unmount_cb,
                                                   page);
   }
   else
   {
      _ecdb_erase_do_erase(page, proj);
   }
}

void
ecdb_erase_page_show(Ecdb_Page *page)
{
   /* Hide the filelist as it isn't needed to erase a disc */
   edje_object_signal_emit(page->gui, "ecdb,filelist,hide", "ecdb");

   if (!page->erase)
   {
      Evas_Object *d;
      Evas_Coord x, y, w, h;
      Eina_List *l;
      Ecdb_Drive_Info *drive;
      Ecdb_Erase_Project *proj;
      Ecdb_Project *bp;

      page->erase = edje_object_add(ecore_evas_get(em->main_win_ee));
      edje_object_file_set(page->erase, em->theme_path, "ecdb/erase_page");
      edje_object_part_geometry_get(page->gui, "active_area", &x, &y, &w, &h);
      evas_object_move(page->erase, x, y);
      evas_object_resize(page->erase, w, h);
      edje_object_part_swallow(page->gui, "action_area", page->erase);
      evas_object_event_callback_add(page->erase, EVAS_CALLBACK_DEL, _page_del,
                                                               page);
      evas_object_show(page->erase);

      proj = ecdb_erase_project_new();
      bp = ECDB_PROJECT(proj);
      evas_object_data_set(page->erase, "proj", proj);

      edje_object_signal_callback_add(page->erase, "hide,finished",
                     "erase_page", _page_hide_finished, page->gui);

      bp->ret = ecdb_button_add(page->erase, "ecdb/erase/return");
      ecdb_button_label_set(bp->ret, "Return to Main Page");
      edje_object_signal_callback_add(page->erase, "ecdb,clicked",
                              "ecdb/erase/return", _button_cb_return, page);
      evas_object_show(bp->ret);
      ecdb_button_icon_set(bp->ret, "ecdb/erase/return");

      bp->begin = ecdb_button_add(page->erase, "ecdb/erase/begin");
      ecdb_button_label_set(bp->begin, "Start Erase");
      edje_object_signal_callback_add(page->erase, "ecdb,clicked",
                              "ecdb/erase/begin", _button_cb_begin, page);
      evas_object_show(bp->begin);
      ecdb_button_icon_set(bp->begin, "ecdb/erase/begin");

      proj->speed = ecdb_check_add(page->erase, "ecdb/erase/speed");
      ecdb_check_label_set(proj->speed, "Quick Erase");
      evas_object_show(proj->speed);

      bp->drive_combo = ecdb_combo_add(page->erase, "ecdb/erase/drive");
      ecdb_combo_header_set(bp->drive_combo, "Drive");
      ecdb_combo_header_create_set(bp->drive_combo,
                                             ecdb_gui_combo_header_from_drive);

      EINA_LIST_FOREACH(em->drives, l, drive)
      {
         d = ecdb_combo_item_add(bp->drive_combo, NULL);
         ecdb_combo_item_label_set(d, drive->product);
         ecdb_combo_append(bp->drive_combo, d);
         evas_object_show(d);
      }
   }

   else if (edje_object_part_swallow_get(page->gui, "action_area") !=
                                                                  page->erase)
   {
      edje_object_part_swallow(page->gui, "action_area", page->erase);
      evas_object_show(page->erase);
   }

   edje_object_signal_emit(page->gui, "ecdb,erase_page,visible", "ecdb");
   edje_object_signal_emit(page->erase, "ecdb,erase_page,visible", "ecdb");
}

void
ecdb_erase_cleanup(Ecdb_Page *page)
{
   Ecdb_Erase_Project *proj;
   int idx;

   if (!page)
   {
      EINA_ERROR_PWARN("ecdb_erase_cleanup: NULL page!\n");
      return;
   }

   proj = evas_object_data_get(page->erase, "proj");
   if (!proj)
   {
      EINA_ERROR_PWARN("ecdb_erase_cleanup: NULL proj!\n");
      return;
   }

   Evas_Object *objs[] = {proj->speed, ECDB_PROJECT(proj)->ret,
                          ECDB_PROJECT(proj)->begin,
                          ECDB_PROJECT(proj)->drive_combo, NULL};
   for (idx = 0; objs[idx] != NULL; idx++)
      edje_object_signal_emit(objs[idx], "ecdb,disable", "ecdb");

   edje_object_part_text_set(page->erase, "progress_text", "Erase Complete!");
   edje_object_signal_emit(page->erase, "ecdb,erase,done", "ecdb");

   // Set attributes back to normal
   ecdb_check_checked_set(proj->speed, 0);
   proj->speed = 0;
}


static void
_ecdb_erase_do_erase(Ecdb_Page *page, Ecdb_Erase_Project *proj)
{
   int idx;

   if (!proj)
   {
      EINA_ERROR_PWARN("NULL project!\n");
      return;
   }

   if (!page)
   {
      EINA_ERROR_PWARN("NULL page!\n");
      return;
   }

   edje_object_part_text_set(page->erase, "progress_text", "Commencing...");

   if (!ecdb_aquire_drive(ECDB_PROJECT(proj)))
   {
      EINA_ERROR_PWARN("Couldn't grab the drive!\n");
      edje_object_part_text_set(page->erase, "progress_text",
                                                "Couldn't grab the drive!");
   }

   proj->quick = ecdb_check_checked_get(proj->speed);
   if (!ecdb_erase_disc(proj, page))
   {
      EINA_ERROR_PINFO("Disc not erasable!\n");
      edje_object_part_text_set(page->erase, "progress_text",
                                                      "Disc not erasable!");
      burn_drive_release(ECDB_PROJECT(proj)->drive->tangible[0].drive, 0);
      burn_drive_info_free(ECDB_PROJECT(proj)->drive->tangible);
      return;
   }

   edje_object_signal_emit(page->erase, "ecdb,erase,start", "ecdb");
   Evas_Object *objs[] = {proj->speed, ECDB_PROJECT(proj)->ret,
                                 ECDB_PROJECT(proj)->begin,
                                 ECDB_PROJECT(proj)->drive_combo, NULL};
   for (idx = 0; objs[idx] != NULL; idx++)
      edje_object_signal_emit(objs[idx], "ecdb,disable", "ecdb");
}

void
_ecdb_erase_unmount_cb(void *data, void *reply_data __UNUSED__, DBusError *err)
{
   Ecdb_Erase_Project *proj;
   Ecdb_Page *page;

   page = data;

   if (!page)
   {
      EINA_ERROR_PWARN("NULL page!\n");
      return;
   }

   proj = evas_object_data_get(page->erase, "proj");
   if (!proj)
   {
      EINA_ERROR_PWARN("NULL project!\n");
      return;
   }

   if (dbus_error_is_set(err))
   {
      if (!strcmp(err->name, "org.freedesktop.Hal.Device.Volume.NotMounted"))
      {
         // This case is totally fine for our uses
         _ecdb_erase_do_erase(page, proj);
      }
      else
      {
         edje_object_part_text_set(page->erase, "progress_text",
                                          "Can not unmount drive!");
         EINA_ERROR_PWARN("Unmount error:\n%s\n%s\n", err->name, err->message);
      }
      dbus_error_free(err);
   }
   else
   {
      _ecdb_erase_do_erase(page, proj);
   }
}
