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

typedef void (*edje_cb_type)(void *, Evas_Object *, const char *, const char *);

static void ecdb_cb_burn_data_clicked(void *data, Evas_Object *o,
                                    const char *emission, const char *source);
static void ecdb_cb_burn_audio_clicked(void *data, Evas_Object *o,
                                    const char *emission, const char *source);
static void ecdb_cb_burn_image_clicked(void *data, Evas_Object *o,
                                    const char *emission, const char *source);
static void ecdb_cb_erase_disc_clicked(void *data, Evas_Object *o,
                                    const char *emission, const char *source);

static void ecdb_handle_typebuf(Evas_Object *gui, Ewl_Widget *fl);
static void ecdb_filelist_show(Ecdb_Page *page);
static void ecdb_welcome_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 _filelist_del(Ewl_Widget *w, void *ev_data, void *data);

static void ecdb_welcome_page_show_about(void *data, Evas_Object *o,
                                    const char *emission, const char *source);
static void ecdb_welcome_page_show_config(void *data, Evas_Object *o,
                                    const char *emission, const char *source);

static void
ecdb_welcome_page_show_about(void *data __UNUSED__, Evas_Object *o __UNUSED__,
                                             const char *emission __UNUSED__,
                                             const char *source __UNUSED__)
{
   ecdb_about_show();
}

static void
ecdb_welcome_page_show_config(void *data __UNUSED__, Evas_Object *o __UNUSED__,
                                             const char *emission __UNUSED__,
                                             const char *source __UNUSED__)
{
   ecdb_config_dialog_show();
}

static void
_filelist_del(Ewl_Widget *w, void *ev_data __UNUSED__, void *data __UNUSED__)
{
   Ewl_Filelist_Filter *f;

   f = ewl_filelist_filter_get(EWL_FILELIST(w));
   if (f)
   {
      if (f->extension)
         free(f->extension);
      free(f);
   }
}

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

   page = data;
   if (!page)
      return;

   free(page);
}

/* XXX These need to be fixed when Ecdb_Page become the swallow,
 * whereas it is the actual gui as well
 */
static void
ecdb_cb_enter(Ecore_Evas *ee)
{
   edje_object_signal_emit(em->page->gui, "ecdb,window,enter", "ecdb");
}

static void
ecdb_cb_leave(Ecore_Evas *ee)
{
   edje_object_signal_emit(em->page->gui, "ecdb,window,exit", "ecdb");
}

static void
ecdb_cb_resize(Ecore_Evas *ee)
{
   int w, h;
   ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
   evas_object_resize(em->page->gui, w, h);
}

static void
_cb_filelist_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *eo,
																	void *ev_data __UNUSED__)
{
   Ecdb_Page *page = data;
   evas_object_focus_set(eo, 1);
   edje_object_signal_emit(page->gui, "ecdb,filelist_overlay,deactivate",
                                                                     "ecdb");
   edje_object_part_text_set(page->gui, "filelist_overlay_text", NULL);

}

static void
_cb_filelist_key_down(void *data, Evas *e __UNUSED__,
            Evas_Object *eo __UNUSED__, void *ev_data)
{
   Evas_Event_Key_Down *ek = ev_data;
   Ecdb_Page *page = data;

   if (!strcmp(ek->key, "Escape"))
   {
      edje_object_signal_emit(page->gui, "ecdb,filelist_overlay,deactivate",
                                                                     "ecdb");
      edje_object_part_text_set(page->gui, "filelist_overlay_text", NULL);
   }

   else if (!strcmp(ek->key, "Return"))
   {
      ecdb_handle_typebuf(page->gui, evas_object_data_get(page->filelist,
                                                               "filelist"));
   }

   else if (!strcmp(ek->key, "BackSpace"))
   {
      int len;
      const char *t;
      char *text;

      t = edje_object_part_text_get(page->gui, "filelist_overlay_text");
      text = (t) ? (strdup(t)) : NULL;

      if ((!text) || (!*text)) return;

      len = strlen(text);
      text = realloc(text, len);
      text[len - 1] = '\0';

      if (len == 1)
      {
         edje_object_signal_emit(page->gui, "ecdb,filelist_overlay,deactivate",
																							"ecdb");
      }
      edje_object_part_text_set(page->gui, "filelist_overlay_text",  text);
      FREE(text);
   }

   else 
   {
      int len;
      const char *t, *append;
      char *text;

      t = edje_object_part_text_get(page->gui, "filelist_overlay_text");

      text = (t) ? (strdup(t)) : NULL;

      if ((text) && (strlen(text) >= PATH_MAX))
         return;

      if ((ek->string) && (*ek->string) && (!ek->string[1]))
      {
         append = ek->string;
      }
      else if ((ek->keyname) && (*ek->keyname) && (!ek->key[1]))
      {
         append = ek->keyname;
      }
      else
      {
         if (text)
            free(text);
         return;
      }

      if (!text)
      {
         text = strdup(append);
      }
      else
      {
         len = strlen(text) + 2;
         text = realloc(text, len);
         strncat(text, append, len);
      }
      
      /* Avoid sending the same signal twice... seems to skip the
       * transition in that case
       */
      if (strlen(text) == 1)
      {
         edje_object_signal_emit(page->gui, "ecdb,filelist_overlay,activate",
                                                                     "ecdb");
      }
      edje_object_part_text_set(page->gui, "filelist_overlay_text", text);
      FREE(text);
   }
}

int
ecdb_create_main_gui(void)
{
   Evas_Coord mw, mh;

   if (!strcmp(em->engine, "opengl_x11"))
   {
      em->main_win_ee = ecore_evas_gl_x11_new(0, 0, 0, 0, 255, 255);
      em->xwin = ecore_evas_gl_x11_window_get(em->main_win_ee);
   }
   else if (!strcmp(em->engine, "xrender_x11"))
   {
      em->main_win_ee = ecore_evas_xrender_x11_new(0, 0, 0, 0, 255, 255);
      em->xwin = ecore_evas_xrender_x11_window_get(em->main_win_ee);
   }
   else if (!strcmp(em->engine, "software_x11"))
   {
      em->main_win_ee = ecore_evas_software_x11_new(0, 0, 0, 0, 255, 255);
      em->xwin = ecore_evas_software_x11_window_get(em->main_win_ee);
   }

   if (!em->main_win_ee)
   {
      EINA_ERROR_PWARN("Cannot create main window!\n");
      return FALSE;
   }

   ecore_evas_title_set(em->main_win_ee, "ECDB");
   ecore_evas_name_class_set(em->main_win_ee, "ECDB", "ECDB");
   ecore_evas_avoid_damage_set(em->main_win_ee, 1);
   ecore_x_dnd_aware_set(em->xwin, 1);
   ecore_x_dnd_type_set(em->xwin, "*", 1);

   ecore_evas_callback_delete_request_set(em->main_win_ee, ecdb_shutdown);
   ecore_evas_callback_destroy_set(em->main_win_ee, ecdb_shutdown);

   ecore_evas_callback_mouse_in_set(em->main_win_ee, ecdb_cb_enter);
   ecore_evas_callback_mouse_out_set(em->main_win_ee, ecdb_cb_leave);
   ecore_evas_callback_resize_set(em->main_win_ee, ecdb_cb_resize);

   edje_frametime_set(1.0 / (double)em->fps);
   ecore_evas_show(em->main_win_ee);

   if (ecore_config_boolean_get("use_scale"))
      edje_scale_set(em->scalef);

   em->page = calloc(1, sizeof(Ecdb_Page));
   em->page->gui = edje_object_add(ecore_evas_get(em->main_win_ee));
   edje_object_file_set(em->page->gui, em->theme_path, "ecdb/window");
   edje_object_signal_callback_add(em->page->gui, "ecdb/about/show",
                        "ecdb", ecdb_welcome_page_show_about, NULL);
   edje_object_signal_callback_add(em->page->gui, "ecdb/config/show",
                        "ecdb", ecdb_welcome_page_show_config, NULL);
   edje_object_size_min_get(em->page->gui, &mw, &mh);
   if (mw <= 0) mw = 400;
   if (mh <= 0) mh = 300;
   if (ecore_config_boolean_get("use_scale"))
   {
      mw = (int)((float)mw * em->scalef);
      mh = (int)((float)mh * em->scalef);
   }
   evas_object_resize(em->page->gui, mw, mh);
   ecore_evas_resize(em->main_win_ee, mw, mh);
   ecore_evas_size_min_set(em->main_win_ee, mw, mh);
   evas_object_event_callback_add(em->page->gui, EVAS_CALLBACK_DEL, _page_del,
                                                                  em->page);
   evas_object_move(em->page->gui, 0, 0);
   evas_object_show(em->page->gui);

   ecdb_welcome_page_show(em->page);
   return TRUE;
}

void
ecdb_handle_typebuf(Evas_Object *gui, Ewl_Widget *fl)
{
   Ewl_Filelist_Filter *filter;
   const char *ext;

   if (!(ext = edje_object_part_text_get(gui, "filelist_overlay_text")))
   {
      return;
   }
   
   /* Check for a 'cd' first */
   if (!ecdb_match_keyword(ext, "cd", 2))
   {
      char *dir;

      /* What to do about multi-word directories? */
      dir = ecdb_strip_string(ext);

      if (dir)
      {
         if (dir[0] == '/')
         {
            if (ecore_file_exists(dir))
			   {
               ewl_filelist_directory_set(EWL_FILELIST(fl), dir);
			   }
         }
         else if (dir[0] == '~')
         {
            char path[PATH_MAX];

            snprintf(path, PATH_MAX, "%s%s", getenv("HOME"), &dir[1]);

            if (ecore_file_exists(path))
			   {
               ewl_filelist_directory_set(EWL_FILELIST(fl), path);
			   }
         }
         else
         {
            /* Try to concate it to our current directory */
            char path[PATH_MAX];

            snprintf(path, PATH_MAX, "%s/%s", ewl_filelist_directory_get
																		(EWL_FILELIST(fl)), dir);

            if (ecore_file_exists(path))
			   {
               ewl_filelist_directory_set(EWL_FILELIST(fl), path);
			   }
         }

         FREE(dir);
      }

      edje_object_signal_emit(gui, "ecdb,filelist_overlay,deactivate", "ecdb");
      edje_object_part_text_set(gui, "filelist_overlay_text", NULL);
      return;
   }
   else if (!ecdb_match_keyword(ext, "exit", 4))
   {
      /* This creates an ewl error... */
      ecore_event_add(ECORE_EVENT_SIGNAL_EXIT, NULL, NULL, NULL);
      return;
   }
   
   filter = ewl_filelist_filter_get(EWL_FILELIST(fl));
   if (!filter)
   {
      filter = calloc(sizeof(Ewl_Filelist_Filter), 1);
   }
   else
   {
      if (filter->extension)
      {
         if (strcmp(filter->extension, ext))
         {
            FREE(filter->extension);
         }
         else
         {
            return;
         }
      }
   }
   
   filter->extension = strdup(ext);
   ewl_filelist_filter_set(EWL_FILELIST(fl), filter);
   ewl_filelist_refresh(EWL_FILELIST(fl));
   edje_object_signal_emit(gui, "ecdb,filelist_overlay,deactivate", "ecdb");
   edje_object_part_text_set(gui, "filelist_overlay_text", NULL);
}

void
ecdb_welcome_page_show(Ecdb_Page *page)
{
   if (!page->welcome)
   {
      Evas_Object *b;
      Evas_Coord x, y, w, h;

      page->welcome = edje_object_add(ecore_evas_get(em->main_win_ee));
      edje_object_file_set(page->welcome, em->theme_path, "ecdb/welcome_page");
      edje_object_part_geometry_get(page->gui, "active_area", &x, &y, &w, &h);
      evas_object_move(page->welcome, x, y);
      evas_object_resize(page->welcome, w, h);
      edje_object_part_swallow(page->gui, "action_area", page->welcome);
      edje_object_signal_callback_add(page->welcome, "hide,finished",
                        "welcome_page", ecdb_welcome_page_hide_finished,
                                                                  page->gui);
      evas_object_show(page->welcome);

      int i;
      edje_cb_type cbs[] =
                        {
                           ecdb_cb_burn_data_clicked,
                           ecdb_cb_burn_audio_clicked,
                           ecdb_cb_burn_image_clicked,
                           ecdb_cb_erase_disc_clicked
                        };
      char *labels[] = 
                        {
                           "Burn Data CD",
                           "Burn Audio CD",
                           "Burn Image",
								   "Erase Re-writable Disc"
                        };
      char *ids[] = 
                        {
                           "ecdb/burn_data",
                           "ecdb/burn_audio",
                           "ecdb/burn_image",
							      "ecdb/erase"
                        };
      for (i = 0; i < 4; i++)
      {
         b = ecdb_button_add(page->welcome, ids[i]);
         ecdb_button_label_set(b, labels[i]);
         evas_object_show(b);
         edje_object_signal_callback_add(b, "ecdb,clicked", "ecdb",
                                                               cbs[i], page);
         ecdb_button_icon_set(b, ids[i]);
      }
   }

   else if (edje_object_part_swallow_get(page->gui, "action_area") !=
                                                               page->welcome)
   {
      /* All that is needed here? */
      edje_object_part_swallow(page->gui, "action_area", page->welcome);
      evas_object_show(page->welcome);
   }

   edje_object_signal_emit(page->gui, "ecdb,welcome_page,show", "ecdb");
   edje_object_signal_emit(page->welcome, "ecdb,welcome_page,show", "ecdb");
}

static void
ecdb_filelist_show(Ecdb_Page *page)
{
   if (!page->filelist)
   {
      Evas_Coord x, y, w, h;
      Ewl_Widget *embed, *filelist;

      embed = ewl_embed_new();
      ewl_object_fill_policy_set(EWL_OBJECT(embed), EWL_FLAG_FILL_ALL);
      page->filelist = ewl_embed_canvas_set(EWL_EMBED(embed),
		 ecore_evas_get(em->main_win_ee),
            (void *)(long)ecore_evas_software_x11_window_get(em->main_win_ee));
      ewl_embed_focus_set(EWL_EMBED(embed), TRUE);
      ewl_widget_show(embed);

      filelist = ewl_filelist_new();
      ewl_container_child_append(EWL_CONTAINER(embed), filelist);
      ewl_callback_prepend(EWL_WIDGET(filelist), EWL_CALLBACK_DESTROY,
                                                         _filelist_del, NULL);
      ewl_filelist_directory_set(EWL_FILELIST(filelist), getenv("HOME"));
      ewl_widget_show(filelist);
   
      edje_object_part_geometry_get(page->gui, "filelist", &x, &y, &w, &h);
      evas_object_move(page->filelist, x, y);
      evas_object_resize(page->filelist, w, h);
      edje_object_part_swallow(page->gui, "filelist", page->filelist);
      evas_object_event_callback_add(page->filelist, EVAS_CALLBACK_MOUSE_DOWN,
															_cb_filelist_mouse_down, page);
      evas_object_event_callback_add(page->filelist, EVAS_CALLBACK_KEY_DOWN,
															_cb_filelist_key_down, page);
      evas_object_data_set(page->filelist, "filelist", filelist);
      evas_object_show(page->filelist);
   }

   /* Assume here that it is the active projects job to hide itself and
    * call this show function
    */
   else if (edje_object_part_swallow_get(page->gui, "filelist") !=
                                                               page->filelist)
   {
      /* Is this all that is neccessary? */
      edje_object_part_swallow(page->gui, "filelist", page->filelist);
      evas_object_show(page->filelist);
   }

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


/* Hurrah! Fancyness */
static void
ecdb_welcome_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);
}

void
ecdb_gui_combo_header_from_drive(Evas_Object *c, const char *name, void *data,
                                                   Evas_Object *obj, int sel)
{
   Evas_Object *ret;
   Ecdb_Drive_Info *drive;

   ret = ecdb_label_add(c, name);
   drive = eina_list_nth(em->drives, sel);
   ecdb_label_text_set(ret, drive->product);
   evas_object_show(ret);
}

void
ecdb_gui_combo_header_from_speeds(Evas_Object *c, const char *name, void *data,
                                                Evas_Object *obj, int sel)
{
   Evas_Object *ret;
   Ecdb_Drive_Info *drive;
   char buf[1024];

   drive = data;

   if (!drive)
      return;

   ret = ecdb_label_add(c, name);
   snprintf(buf, sizeof(buf), "%d", ecdb_speed_convert(drive, sel));
   ecdb_label_text_set(ret, buf);
   evas_object_show(ret);
}

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

   edje_object_signal_emit(page->welcome, "ecdb,welcome_page,hide", "ecdb");
   ecdb_burn_data_page_show(page);
   ecdb_filelist_show(page);
}

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

   edje_object_signal_emit(page->welcome, "ecdb,filelist,hide", "ecdb");
   EINA_ERROR_PDBG("Burn Audio Disc\n");
}

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

   ecdb_filelist_show(page);
   edje_object_signal_emit(page->welcome, "ecdb,welcome_page,hide", "ecdb");
   ecdb_burn_image_page_show(page);
}
static void
ecdb_cb_erase_disc_clicked(void *data, Evas_Object *o,
                                    const char *emission __UNUSED__,
                                    const char *source __UNUSED__)
{
   Ecdb_Page *page = data;

   edje_object_signal_emit(page->welcome, "ecdb,welcome_page,hide", "ecdb");
   ecdb_erase_page_show(page);
}
