#include <Evas.h>
#include <stdio.h>

#include "config.h"
#include "theme.h"
#include "evas_macros.h"
#include "icon_list.h"



/******************************************************************************/
/* icon-factory */
/****************/



icon_el *icon_els_create(struct _noir_theme *theme,icon_list *il,char *file,char *text,icon *icon,char *font,int fs,char *thumb) {
  icon_el     *el=NULL,*ic;
  Evas        *evas=get_evas_from_iconlist(il);
  Evas_Object *o=NULL;
  Evas_Coord   _w,_h,_x,_y;
  int          w_,h_,wt=0,ht=0,tx=2,tb,ty=0,iw,ih,err;
  char        *fm,*to;
  file_info   *fi=(file_info *)(icon?icon->user2:NULL);

  icon->state&=~IE_THMB;

  if((o=evas_thumbnail(theme,evas,fi))) {
    icon->state|=IE_THMB;
    evas_object_geometry_get(o,&_x,&_y,&_w,&_h);
    w_=_w; h_=_h; }

  if(!o) {
    if(!file||!*file)
      E(0,"cannot thumb-nail, and no fallback icon given...\n");
    else {
      o=evas_object_image_add(evas);
      evas_object_image_file_set(o,file,NULL);
      if((err=evas_object_image_load_error_get(o))!=EVAS_LOAD_ERROR_NONE) {
        E(0,"error #%d loading \"%s\"...\n",err,file);
        evas_object_del(o);
        return o=NULL; }
      if(o) {
        evas_object_image_size_get(o,&w_,&h_);
        evas_object_resize(o,w_,h_);
        evas_object_image_fill_set(o,0,0,w_,h_); }}}

  iw=w_;
  ih=h_;

  ICON_EL_PRE((IE_DFL|IE_SEL),0,0,(theme->icon_height-h_)/2.0);
  ic=el;

  if(!theme->icon_height) {
#ifdef NOIR_DEBUG
    fprintf(stderr,"got height (%d) from %s\n",ih,thumb?thumb:file);
#endif
    theme->icon_height=ih; }
  if(!theme->icon_width) {
#ifdef NOIR_DEBUG
    fprintf(stderr,"got width  (%d) from %s\n",iw,thumb?thumb:file);
#endif
    theme->icon_width=iw; }

  tb=ty=theme->icon_height;

  { int fs_=fs/6;  /* re-scale font */
    fs_=fs_?fs_:1;
    fm=text;
    while((fm=strchr(fm,'\n'))) {
      fs-=fs_;
      fm++; }}

  fm=text;        /* support text-wrap */
  while(fm) {
    if((to=strchr(fm,'\n')))
      *to='\0';

    o=evas_object_text_add(evas);
    evas_object_text_font_set(o,font,fs);
    evas_object_text_text_set(o,fm);
    evas_object_geometry_get(o,&_x,&_y,&_w,&_h);
    if(w_>_w)
      tx=((w_-_w)/2.0);
    evas_object_color_set(o,0,0,0, 255);
    ICON_EL_PRE((IE_DFL|IE_KBD),20,tx,ty);

    o=evas_object_text_add(evas);
    evas_object_text_font_set(o,font,fs);
    evas_object_text_text_set(o,fm);
    evas_object_color_set(o,255,255,255, 129);
    ICON_EL_PRE((IE_DFL|IE_KBD),18,tx+1,ty+1);

    o=evas_object_text_add(evas);
    evas_object_text_font_set(o,font,fs);
    evas_object_text_text_set(o,fm);
    evas_object_color_set(o,255,255,255, 255);
    ICON_EL_PRE(IE_SEL,20,tx,ty);

    o=evas_object_text_add(evas);
    evas_object_text_font_set(o,font,fs);
    evas_object_text_text_set(o,fm);
    evas_object_color_set(o,0,0,0, 159);
    ICON_EL_PRE(IE_SEL,19,tx+1,ty+1);

    if(to) {
      *(to++)='\n';
      fm=to;
      ty+=fs; }
    else
      fm=NULL; }

  icon->w=wt+4;
  icon->h=ht;

  ic->xo=(int)((wt-ic->w)/2);  /* center image */

  o=evas_object_rectangle_add(evas);
  evas_object_resize(o,wt,ht-tb); /* wt-2 */
  evas_object_color_set(o,55,55,55,255);
  ICON_EL_PRE(IE_SEL,10,1,tb);

  o=evas_object_rectangle_add(evas);
  evas_object_resize(o,wt+1,ht-tb-2);
  evas_object_color_set(o,96,96,96, 255);
  ICON_EL_PRE(IE_SEL,10,0,tb+1);

#if 0
  o=evas_object_image_add(evas);
  evas_object_image_file_set(o,file,NULL);
  evas_object_resize(o,iw,ih);
  evas_object_image_fill_set(o,0,0,iw,ih);
  evas_object_color_set(o,128,0,96, 255);
  ICON_EL_PRE(IE_KBD,10,ic->xo,ic->yo);
#else
  o=evas_object_gradient_add(evas);
  ICON_EL_PRE(IE_KBD,-10,0,0);
  evas_object_resize(o,icon->w,ht);
  evas_object_gradient_color_add(o,  0,103,127,  0,  8);
  evas_object_gradient_color_add(o,  0,206,255, 63, 16);
  evas_object_gradient_angle_set(o,150);
#endif

  return el; }
