/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */
/*
 * Source for loading/drawing square
 */

#include "stonage.h"
#include "square.h"
#include "image.h"
#include "trace.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <gtk/gtkwidget.h>

struct enum_list square_properties_text2enum[] =
{
   {"CAN_MOVE_TO_NORTH", CAN_MOVE_TO_NORTH},
   {"CAN_MOVE_TO_SOUTH", CAN_MOVE_TO_SOUTH},
   {"CAN_MOVE_TO_EAST", CAN_MOVE_TO_EAST},
   {"CAN_MOVE_TO_WEST", CAN_MOVE_TO_WEST},
   {"AFTER_MOVE_CRASH", AFTER_MOVE_CRASH},
   {"START", START},
   {"FINISH", FINISH},
   {"CAN_GO_ABOVE", CAN_GO_ABOVE},
   {"KEY_RED", KEY_RED},
   {"KEY_BLUE", KEY_BLUE},
   {"KEY_GREEN", KEY_GREEN},
   {"NEED_KEY_RED", NEED_KEY_RED},
   {"NEED_KEY_BLUE", NEED_KEY_BLUE},
   {"NEED_KEY_GREEN", NEED_KEY_GREEN},
   {"HOURGLASS", HOURGLASS},
   {"TELEPORT1", TELEPORT1},
   {"TELEPORT2", TELEPORT2},
   {"TELEPORT3", TELEPORT3},
   {0,0 }
};


#if 0
/*
 * Extract the basename on the full filename
 */
const char *basename(const char *filename)
{
  const char *p;

  /* FIXME: change / by \\ on a msdos filesystem */
  p=strrchr(filename,'/');
  if (p==NULL)
   return filename;
  p++;
  return p;
}

int Square_Count_Dir(const char *directory)
{
  int n;
  DIR *d;
  struct dirent *dir;
#if ENABLE_DEBUG_TRACE
  char path[1024];
#endif

  n=0;
  d=opendir(directory);
  if (d==NULL)
   {
     trace("Can't open directory %s (%s)",directory,strerror(errno));
     return 0;
   }

  while ((dir=readdir(d))!=NULL)
   {
     if (dir->d_name[0]=='.')
       continue;
#ifdef ENABLE_DEBUG_TRACE
     snprintf(path,1023,"%s/%s",directory,dir->d_name);
     trace("%s",path);
#endif
     n++;
   }

  closedir(d);
  return n;
}


int Square_Load_Dir(GList **squares,const char *directory)
{
  int n;
  DIR *d;
  struct dirent *dir;
  char path[1024];
  GtkWidget *pixmap;
  struct t_square *square;

  n=0;
  d=opendir(directory);
  if (d==NULL)
   {
     trace("Can't open directory %s (%s)",directory,strerror(errno));
     return 0;
   }

  while ((dir=readdir(d))!=NULL)
   {
     if (*(dir->d_name)=='.')
       continue;
     snprintf(path,1023,"%s/%s",directory,dir->d_name);
     trace("Trying to load %s",path);
     pixmap = gtk_pixmap_new_from_file(path);
     if (pixmap)
      {
	square = g_new0(struct t_square,1);
	if (square==NULL)
	 {
	   trace("Failed to alloc %d bytes",sizeof(struct t_square));
	   return 0;
	 }
	trace("Loading %s: success",path);
	square->pixmaps = pixmap;
	*squares = g_list_append(*squares,square);
	n++;
      }
     else
      {
	trace("Loading %s: failed",path);
      }
   }

  closedir(d);
  return n;
}

/*
 * Count the number of graphics squares available
 */
int Square_Count(void)
{
  int i,n;
  char tmp[1024];
  char *datadir_path[]=
   {
     STONAGE_DATA_DIR,
     ".",
     "../data",
     NULL
   };
  i=0; n=0;
  while (datadir_path[i])
   {
     snprintf(tmp,1024,"%s/squares/",datadir_path[i]);
     trace("Trying to parse %s",tmp);
     n += Square_Count_Dir(tmp);
     i++;
   }
  return n;
}

/*
 * Load all the square
 */
int Square_Load_All(GList **squares)
{
  int i,n;
  char tmp[1024];
  char *datadir_path[]=
   {
     STONAGE_DATA_DIR,
     ".",
     "../data",
     NULL
   };
  i=0; n=0;
  while (datadir_path[i])
   {
     snprintf(tmp,1024,"%s/squares/",datadir_path[i]);
     trace("Trying to parse %s",tmp);
     n += Square_Load_Dir(squares,tmp);
     i++;
   }
  return n;
}

#endif

struct t_square_default
{
  unsigned int properties;
  const char *pixmap;
} static default_square[] =
{
   {0,"wall.png"},
   {CAN_GO_ABOVE,"normal.png"},
   {CAN_GO_ABOVE|CAN_MOVE_TO_WEST,"arrow_left.png"},
   {CAN_GO_ABOVE|CAN_MOVE_TO_EAST,"arrow_right.png"},
   {CAN_GO_ABOVE|CAN_MOVE_TO_NORTH,"arrow_up.png"},
   {CAN_GO_ABOVE|CAN_MOVE_TO_SOUTH,"arrow_down.png"},
   {CAN_GO_ABOVE|START,"stair.png"},
   {CAN_GO_ABOVE|FINISH,"stair.png"},
   {CAN_GO_ABOVE|AFTER_MOVE_CRASH,"break.png"},
   {CAN_GO_ABOVE|CAN_MOVE_TO_EAST|CAN_MOVE_TO_WEST|CAN_MOVE_TO_SOUTH|CAN_MOVE_TO_NORTH,"arrow_all.png"},
   {CAN_GO_ABOVE|CAN_MOVE_TO_EAST|CAN_MOVE_TO_WEST,"arrow_left_right.png"},
   {CAN_GO_ABOVE|CAN_MOVE_TO_NORTH|CAN_MOVE_TO_SOUTH,"arrow_up_down.png"},
   {CAN_GO_ABOVE|AFTER_MOVE_CRASH,"break.png"},
   {CAN_GO_ABOVE|KEY_RED,"key_red.png"},
   {CAN_GO_ABOVE|NEED_KEY_RED,"need_key_red.png"},
   {CAN_GO_ABOVE|TELEPORT1,"teleport.png"},
   {0,NULL}
};

/**
 *
 * Load default square
 *
 */
GList *
Square_Load_Default(void)
{
  int i,j;
  GList *squares=NULL;
  GtkWidget *pixmap;
  GdkPixmap *pixmap_lowlevel;
  struct t_square *square;
  char tmp[1024];
  char *datadir_path[]=
   {
     STONAGE_DATA_DIR,
     ".",
     "../data",
     NULL
   };

  i=0;
  while (default_square[i].pixmap!=NULL)
   {
     j=0;
     pixmap=NULL;
     while (datadir_path[j])
      {
	snprintf(tmp,1024,"%s/squares/%s",datadir_path[j],default_square[i].pixmap);
	trace("Trying to load %s",tmp);
	pixmap = gtk_pixmap_new_from_file(tmp,&pixmap_lowlevel);
	if (pixmap)
	  break;
	j++;
      }
     if (pixmap)
      {
	square = g_new0(struct t_square,1);
	square->pixmaps = g_new0(GtkWidget *,1);
	square->pixmaps[0] = pixmap;
	square->npixmaps = 1;
	square->pixmap = pixmap_lowlevel;
	square->delay = 10;
	square->properties = default_square[i].properties;
	square->name = g_strdup(default_square[i].pixmap);
	squares = g_list_append(squares,square);
      }
     i++;
   }

  return squares;
}

/**
 *
 * We don't free the structure itself
 *
 */
void
Square_Free(struct t_square *s)
{
  int i;
  if (s!=NULL)
   {
     if (s->pixmaps!=NULL)
      {
        for (i=0;i<s->npixmaps;i++)
          gtk_widget_destroy(s->pixmaps[i]);
        g_free(s->pixmaps);
      }
   }
}


