/* 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 _combo_cb_clicked(void *data, Evas_Object *obj, void *event_info);

static void
_button_cb_return(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *swallow, *gui;
   char *signal;

   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),
																			"burn_image_page");

   edje_object_part_text_set(swallow, "progress_text", "Begin Burn");
   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();

   /* Hide the filelist to be nice */
   edje_object_signal_emit(gui, "ecdb,filelist,hide", "ecdb");
}

static void
_button_cb_begin(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *swallow, *gui, *o;
   Ecdb_Burn_Project *proj;
   Ecdb_Source *iso_file;
   char *file, buf[1024];
   Ecdb_Burn_Result burn_result;
   int drive, speed;

   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),
																			"burn_image_page");
   o = evas_object_name_find(ecore_evas_get(em->main_win_ee),
																	"ecdb/burn_image/file");
   file = ecdb_entry_text_get(o);

   // Make sure that file actually exists
   if (!ecore_file_exists(file))
   {
      edje_object_part_text_set(swallow, "progress_text", "File doesn't "
                                                               "exists!");
      FREE(file);
      return;
   }

   proj = ecdb_burn_project_new();
   ecdb_project_type_set(ECDB_PROJECT(proj), ECDB_IMAGE_PROJECT);
   iso_file = ecdb_source_new();
   proj->files = ecdb_source_new();
   ecdb_source_data_set(iso_file, file);
   FREE(file);
   ecdb_source_child_append(proj->files, iso_file);

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

   o = evas_object_name_find(evas_object_evas_get(obj),
                                                      "ecdb/burn_image/speed");
   speed = ecdb_speed_convert(eina_list_nth(em->drives, drive),
                                             ecdb_combo_selected_get(o));
   if (speed < 0)
      proj->speed = 0;
   else
      proj->speed = speed;

   snprintf(buf, sizeof(buf), "Commencing...");
   edje_object_part_text_set(swallow, "progress_text", buf);
   if (!ecdb_aquire_drive(ECDB_PROJECT(proj), drive))
   {
      printf("Couldn't grab drive!\n");
      ecdb_burn_project_destroy(proj);
      edje_object_part_text_set(swallow, "progress_text",
																	"Couldn't grab the drive!");
      return;
   }

   burn_result = ecdb_burn_project(proj);
   switch (burn_result)
   {
      case ECDB_ERROR_NONE:
         edje_object_signal_emit(gui, "ecdb,filelist,hide", "ecdb");
         edje_object_signal_emit(swallow, "ecdb,burn_image,start", "ecdb");
         const char *ids[] = {"ecdb/burn_image/return", "ecdb/burn_image/begin",
										"ecdb/burn_image/file", "ecdb/burn_image/drive",
                              "ecdb/burn_image/speed"};
         ecdb_gui_controls_disable(ids, 5);
         return;

      case ECDB_ERROR_IMAGE_CREATE:
         snprintf(buf, sizeof(buf), "Invalid file!");
         break;

      case ECDB_ERROR_SOURCE_ATTACH:
         snprintf(buf, sizeof(buf), "Couldn't attach source data!");
         break;

      case ECDB_ERROR_WRITE_MODE:
         snprintf(buf, sizeof(buf), "No suitable burn mode!");
         break;

      default:
         snprintf(buf, sizeof(buf), "Unknown error :-(");
   }     
      
   edje_object_part_text_set(swallow, "progress_text", buf);
   Ecdb_Project *p = ECDB_PROJECT(proj);
   burn_drive_release(p->drive->tangible[0].drive, 1);
   burn_drive_info_free(p->drive->tangible);
   ecdb_burn_project_destroy(proj);
}

static void
_combo_cb_clicked(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *c1, *c2, *b;
   Ecdb_Drive_Info *drive;
   int sel, i, num_speeds, speed;
   char buf[1024];

   c2 = data;
   c1 = evas_object_name_find(evas_object_evas_get(obj),
                                                   "ecdb/burn_image/drive");
   sel = ecdb_combo_selected_get(c1);
   if (sel < 0)
      return;

   drive = eina_list_nth(em->drives, sel);
   num_speeds = drive->write_speeds[0];
   ecdb_combo_data_set(c2, drive);
   for (i = 1; i < num_speeds; i++)
   {
      if ((drive->write_speeds[i]) <= 0)
         continue;

      b = ecdb_combo_item_add(c2, NULL);

      if ((drive->profile_name) && (drive->profile_name[0] != '\0'))
      {
         if (drive->profile_name[0] == 'C')
            speed = drive->write_speeds[i] / 150;
         else if (drive->profile_name[0] == 'D')
            speed = drive->write_speeds[i] / 1352;
         else
            speed = drive->write_speeds[i] / 4500;
      }
      else
      {
         speed = 0;
      }

      snprintf(buf, sizeof(buf), "%dX (%dkb/s)", speed, drive->write_speeds[i]);
      ecdb_combo_item_label_set(b, buf);
      ecdb_combo_append(c2, b);
      evas_object_show(b);
   }
}

void
ecdb_burn_image_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),
         "burn_image_page");

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

      swallow = edje_object_add(ecore_evas_get(em->main_win_ee));
      edje_object_file_set(swallow, em->theme_path, "ecdb/burn_image_page");
      evas_object_name_set(swallow, "burn_image_page");
      evas_object_data_set(swallow, "hide_signal", "ecdb,burn_image_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",
													"burn_image_page",
													ecdb_page_hide_finished, NULL);
      evas_object_show(swallow);

      b = ecdb_button_add(swallow, "ecdb/burn_image/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/burn_image/return");

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

      b = ecdb_entry_add(swallow, "ecdb/burn_image/file");
      ecdb_entry_text_set(b, "Burn File");
      evas_object_show(b);

      c1 = ecdb_combo_add(swallow, "ecdb/burn_image/drive");
      ecdb_combo_header_set(c1, "Drive");
      ecdb_combo_header_create_set(c1, ecdb_gui_combo_header_from_drive);
      evas_object_show(c1);

      c2 = ecdb_combo_add(swallow, "ecdb/burn_image/speed");
      ecdb_combo_header_create_set(c2, ecdb_gui_combo_header_from_speeds);
      ecdb_combo_header_set(c2, "Max Speed");
      evas_object_show(c2);

      EINA_LIST_FOREACH(em->drives, l, drive)
      {
         b = ecdb_combo_item_add(c1, NULL);
         ecdb_combo_item_label_set(b, drive->product);
         evas_object_smart_callback_add(b, "clicked", _combo_cb_clicked, c2);
         ecdb_combo_append(c1, b);
         evas_object_show(b);
      }
   }

   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,burn_image_page,visible", "ecdb");
   edje_object_signal_emit(swallow, "ecdb,burn_image_page,visible", "ecdb");
}

void
ecdb_burn_image_cleanup(void)
{
   Evas_Object *swallow;
   const char *ids[] = {"ecdb/burn_image/return", "ecdb/burn_image/begin",
								"ecdb/burn_image/file", "ecdb/burn_image/drive"
                        "ecdb/burn_image/speed"};
   swallow = evas_object_name_find(ecore_evas_get(em->main_win_ee),
																				"burn_image_page");
   edje_object_part_text_set(swallow, "progress_text", "Burn Complete!");
   ecdb_gui_controls_enable(ids, 3);
   edje_object_signal_emit(swallow, "ecdb,burn_image,done", "ecdb");
}

