/**************************************************************
 * FFTacticsMUD : shop.cpp                                    *
 **************************************************************
 * (c) 2002 Damien Dailidenas (Trenton). All rights reserved. *
 **************************************************************/

#include "main.h"
#include <strstream>
#include <iomanip>

void SHOP::add(OBJ *obj) {
  for(short x = 0; x < MAX_SHOP_OBJECTS; ++x) {
    if(!objects[x]) {
      objects[x] = obj;
      return;
    }
  }

  return;
}

void do_buy(CH *ch, string argument = "") {
  OBJ *obj;

  if(!ch->room->shop) {
    ch->printf("buy: ERROR: Can only be used in a shop.\n\r");
    return;
  }

  if(argument.empty() || !(obj = ch->room->shop->objects[atoi(argument.c_str())])) {
    ch->printf("Usage: buy [id]\n\r");
    return;
  }

  if(ch->money < obj->cost() && !ch->admin) {
    ch->printf("buy: ERROR: Not enough money.\n\r");
    return;
  }

  ostrstream ost;
  ost << "You buy " << obj->name() << " for $" << obj->cost() << ".\n\r" << ends;
  ch->printf(ost.str());
  ost.freeze(false);
  ost.seekp(0, ios::beg);
  ost << ch->name << " buys " << obj->name() << " for $" << obj->cost() << ".\n\r" << ends;
  ch->room->echo(ost.str(), ch);

  if(!ch->admin)
    ch->money -= obj->cost();

  create_obj(obj->id)->to(ch);
  return;
}

void do_sell(CH *ch, string argument = "") {
  OBJ *obj;

  if(!ch->room->shop) {
    ch->printf("sell: ERROR: Can only be used in a shop.\n\r");
    return;
  }

  if(argument.empty() || !(obj = ch->obj(atoi(argument.c_str())))) {
    ch->printf("Usage: sell [id]\n\r");
    return;
  }

  if(!obj->value()) {
    ch->printf("sell: ERROR: That item has no resell value.\n\r");
    return;
  }

  ostrstream ost;
  ost << "You sell " << obj->name() << " for $" << obj->value() << ".\n\r" << ends;
  ch->printf(ost.str());
  ost.freeze(false);
  ost.seekp(0, ios::beg);
  ost << ch->name << " sells " << obj->name() << " for $" << obj->value() << ".\n\r" << ends;
  ch->room->echo(ost.str(), ch);
  ch->money += obj->value();
  zap(obj);
  return;
}

void SHOP::list(CH *ch) {
  bool can_wear;
  OBJ *obj;

  ch->printf("{3+--{0Type{3---------------{0Name{3------------------------{0Price{3--+\n\r");
  ch->printf("+--------------------------------------------------------+{0\n\r");

  for(short x = 0; (obj = objects[x]); ++x) {
    can_wear = (obj->type() == TYPE_POTION || ch->can_equip(obj, (obj_table[obj->id].obj_type == OBJ_WEAPON || obj_table[obj->id].obj_type == OBJ_WEAPON ? LOC_RHAND : obj_table[obj->id].obj_type == OBJ_HEAD ? LOC_HEAD : obj_table[obj->id].obj_type == OBJ_BODY ? LOC_BODY : LOC_ACC)));
    ch->printf(" {3|{0 %s%-16s {3|{0 %s%-26s  %s%05d{0\n\r", can_wear ? "{0" : "{8", obj->str_type().c_str(), can_wear ? "{0" : "{8", obj->name().c_str(), ch->money < obj->cost() ? "{8" : "{0", obj->cost());
  }

  ch->printf("{3+--------------------------------------------------------+\n\r");
  ch->printf("+--------------------------------------------------------+{0\n\r");
  ch->printf("                          Shopkeeper        War Funds\n\r");
  ch->printf("                           {&%-16s  %08ld{0\n\r", "The shopkeeper", ch->money);
  return;
}