#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include"object.h"
#include"io.h"
#include"fejs.h"
#include"objects.c"

char *obj_cat[] = {"Money", "Missiles", "Food", "Headcovers"};
int obj_cats = 4;

void
object_clone(unsigned long i, tobj *o) {
  tobjtmp *tmp = &pre_obj[i];
  
  o->t = tmp->t;
  o->w = tmp->w;
  o->c = tmp->c;
  o->i = i;
  o->f = 0;
}

void
money_make(unsigned int m, tobj *o) {
  object_clone(0, o);
  
  o->w = o->c = m;
}

void
object_paint(tobj *o, int x, int y) {
  pputch(x + sx, y + sy, pre_obj[o->i].tr, pre_obj[o->i].cl);
}

void
object_notice(tobj *o) {
  char s[256];
  
  if (o->t == 0 && o->c > 1)
    sprintf(s, "%d %s are", o->c, pre_obj[o->i].names);
  else
    sprintf(s, "%s %s is", pre_obj[o->i].an? "an": "a", pre_obj[o->i].name);
  
  s[0] = toupper(s[0]);
  strcat(s, " lying here.");
  mwrite(s);
}

void
object_get(tlevel *level, tmonster *monster) {
  char str[256];
  tobj *obj;
  
  obj = level->m[monster->y][monster->x].o;
  monster_iget(monster, obj);
  
  level->m[monster->y][monster->x].o = NULL;
  
  sprintf(str, "You took");
  
  if (obj->t == 0 && obj->c > 1)
    sprintf(str, "%s %d %s.", str, obj->c, pre_obj[obj->i].names); 
  else
    sprintf(str, "%s the %s.", str, pre_obj[obj->i].name);
  
  mwrite(str);
}

void
object_drop(tlevel *l, tmonster *m, tobj *o) {
  char s[256];
  
  if (l->m[m->y][m->x].o) {
    mwrite("Sorry, but there is actually lying something.");
    return;
  }
  
  monster_idrop(m, o);
  
  l->m[m->y][m->x].o = o;
  
  sprintf(s, "You dropped");
  
  if (o->t == 0 && o->c > 1)
    sprintf(s, "%s %d %s.", s, o->c, pre_obj[o->i].names);
  else
    sprintf(s, "%s the %s.", s, pre_obj[o->i].name);
  
  mwrite(s);
}

void
object_menu(tobj **i, int l, unsigned long *c, int f) {
  int s = 0, *tr = malloc(sizeof(long) * l), ct, it, tt = 0;
  
  tr[0] = 0;
  for (ct = 0; ct < obj_cats; ct++)
    if (f < 0 || ! (f ^ ct))
      for (it = 0; it < l; it++)
	if (i[it]->t == ct) {
	  tr[tt] = it;
	  tt++;
	}
  
  l = tt;
  
  while (1) {
    pclear(2,23);
    
    if (! l)
      pputstr(2, 2, "No items at all", C_YELLOW);
    
    else {
      ct = -1;
      tt = 2;
      
      for (it = s; it < l; it++) {
	if (tt > 22)
	  break;
	
	if (i[tr[it]]->t != ct) {
	  if (tt < 20) {
	    ct = i[tr[it]]->t;
	    tt++;
	    pputstr(0, tt, obj_cat[ct], C_BROWN);
	    tt++;
	  } else
	    break;
	}
	
        pputch(2, tt, it - s + 'A', C_YELLOW);
        pputch(4, tt, '-', 0);
	pputstr(6, tt, pre_obj[i[tr[it]]->i].name, 0);
	tt++;
      }
    }
    
    refresh();
    pputstr(6, 24, "Choose letter [A-V]  --  Type Z for quit  --  PgUp/PgDn for scrolling", C_YELLOW);
    refresh();
    
    tt = toupper(getch());
    if (l && tt >= 'A' && tt <= 'A' + (l - s < 20? l - s - 1: 20)) {
      *c = tr[tt - 'A' + s];
      goto e;
    }
    
    switch (tt) {
      case 'Z':
      case '\033': *c = -1;
		   goto e;
		   
      case KEY_PPAGE: if (s > 0)
			s -= 20;
		      if (s < 0)
			s = 0;
		      break;
		      
      case KEY_NPAGE: if (s < l && l - s > 20)
			s += 20;
		      break;
    }
  }
  
e:
  free(tr);
}
