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

static void _button_cb_return(void *data, Evas_Object *obj, void *event_info);
static void _button_cb_begin(void *data, Evas_Object *obj, void *event_info);

static void
_button_cb_return(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *swallow;
   char *signal;
   
   swallow = evas_object_name_find(ecore_evas_get(em->main_win_ee),
                                                               "erase_page");

   edje_object_part_text_set(swallow, "progress_text", "Begin Erase");
   edje_object_part_text_set(swallow, "progress_percent", "0%");
   signal = evas_object_data_get(swallow, "hide_signal");
   edje_object_signal_emit(swallow, signal, "ecdb");
   ecdb_welcome_page_show();
}

static void
_button_cb_begin(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *swallow, *o;
   Ecdb_Erase_Project *proj;
   char buf[1024];
   int drive;
   
   swallow = evas_object_name_find(ecore_evas_get(em->main_win_ee),
                                                               "erase_page");

   proj = ecdb_erase_project_new();
   snprintf(buf, sizeof(buf), "Commencing...");
   edje_object_part_text_set(swallow, "progress_text", buf);

   o = evas_object_name_find(evas_object_evas_get(obj), "ecdb/erase/drive");
   drive = ecdb_combo_selected_get(o);
   if (drive < 0)
   {
      printf("Choose a drive!\n");
      ecdb_erase_project_destroy(proj);
      edje_object_part_text_set(swallow, "progress_text", "Choose a Drive!");
      return;
   }

   if (!ecdb_aquire_drive(ECDB_PROJECT(proj), drive))
   {
      printf("Couldn't grab drive!\n");
      ecdb_erase_project_destroy(proj);
      edje_object_part_text_set(swallow, "progress_text",
																	"Couldn't grab the drive!");
      return;
   }

   o = evas_object_name_find(evas_object_evas_get(obj), "ecdb/erase/speed");
   proj->quick = ecdb_check_checked_get(o);
   if (!ecdb_erase_disc(proj))
   {
      Ecdb_Project *p = ECDB_PROJECT(proj);

      printf("Disc not erasable!\n");
      snprintf(buf, sizeof(buf), "Disc not erasable!");
      edje_object_part_text_set(swallow, "progress_text", buf);
      burn_drive_release(p->drive->tangible[0].drive, 0);
      burn_drive_info_free(p->drive->tangible);
      ecdb_erase_project_destroy(proj);
      return;
   }

   edje_object_signal_emit(swallow, "ecdb,erase,start", "ecdb");
   const char *ids[] = {"ecdb/erase/return", "ecdb/erase/begin",
                        "ecdb/erase/speed", "ecdb/erase/drive"};
   ecdb_gui_controls_disable(ids, 4);
}

void
ecdb_erase_page_show(void)
{
   Evas_Object *swallow, *gui;

   gui = evas_object_name_find(ecore_evas_get(em->main_win_ee), "gui");
   swallow = evas_object_name_find(ecore_evas_get(em->main_win_ee),
                        "erase_page");

   /* Hide the filelist as it isn't needed to erase a disc */
   edje_object_signal_emit(gui, "ecdb,filelist,hide", "ecdb");

   if (!swallow)
   {
      Evas_Object *b, *d;
      Evas_Coord x, y, w, h;
      Eina_List *l;
      Ecdb_Drive_Info *drive;

      swallow = edje_object_add(ecore_evas_get(em->main_win_ee));
      edje_object_file_set(swallow, em->theme_path, "ecdb/erase_page");
      evas_object_name_set(swallow, "erase_page");
      evas_object_data_set(swallow, "hide_signal", "ecdb,erase_page,hide");
      edje_object_part_geometry_get(gui, "active_area", &x, &y, &w, &h);
      evas_object_move(swallow, x, y);
      evas_object_resize(swallow, w, h);
      edje_object_part_swallow(gui, "action_area", swallow);
      evas_object_show(swallow);

      edje_object_signal_callback_add(swallow, "hide,finished", "erase_page",
                                    ecdb_page_hide_finished, NULL);

      b = ecdb_button_add(swallow, "ecdb/erase/return");
      ecdb_button_label_set(b, "Return to Main Page");
      evas_object_smart_callback_add(b, "clicked", _button_cb_return, NULL);
      evas_object_show(b);
      ecdb_button_icon_set(b, "ecdb/erase/return");

      b = ecdb_button_add(swallow, "ecdb/erase/begin");
      ecdb_button_label_set(b, "Start Erase");
      evas_object_smart_callback_add(b, "clicked", _button_cb_begin, NULL);
      evas_object_show(b);
      ecdb_button_icon_set(b, "ecdb/erase/begin");

      b = ecdb_check_add(swallow, "ecdb/erase/speed");
      ecdb_check_label_set(b, "Quick Erase");
      evas_object_show(b);

      b = ecdb_combo_add(swallow, "ecdb/erase/drive");
      ecdb_combo_header_set(b, "Drive");
      ecdb_combo_header_create_set(b, ecdb_gui_combo_header_from_drive);

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

   else if (edje_object_part_swallow_get(gui, "action_area") != swallow)
   {
      edje_object_part_swallow(gui, "action_area", swallow);
      evas_object_show(swallow);
   }

   edje_object_signal_emit(gui, "ecdb,erase_page,visible", "ecdb");
   edje_object_signal_emit(swallow, "ecdb,erase_page,visible", "ecdb");
}

void
ecdb_erase_cleanup(void)
{
   Evas_Object *swallow;
   const char *ids[] = {"ecdb/erase/return", "ecdb/erase/begin",
                        "ecdb/erase/speed", "ecdb/erase/drive"};

   swallow = evas_object_name_find(ecore_evas_get(em->main_win_ee), 
                                                         "erase_page");
   edje_object_part_text_set(swallow, "progress_text", "Erase Complete!");
   ecdb_gui_controls_enable(ids, 4);
   edje_object_signal_emit(swallow, "ecdb,erase,done", "ecdb");

   swallow = evas_object_name_find(ecore_evas_get(em->main_win_ee),
                                                         "ecdb/erase/drive");
   ecdb_check_checked_set(swallow, 0);
}

