/**
 * Sell a specific stack of items
 */
bool MenuInventory::sell(ItemStack stack) {
	if (stack.empty()) {
		return false;
	}

	// can't sell currency
	if (stack.item == CURRENCY_ID) return false;

	// items that have no price cannot be sold
	if (items->items[stack.item].getPrice() == 0) {
		items->playSound(stack.item);
		pc->logMsg(msg->get("This item can not be sold."), true);
		return false;
	}

	// quest items can not be sold
	if (items->items[stack.item].quest_item) {
		items->playSound(stack.item);
		pc->logMsg(msg->get("This item can not be sold."), true);
		return false;
	}

	int value_each = items->items[stack.item].getSellPrice();
	int value = value_each * stack.quantity;
	addCurrency(value);
	items->playSound(CURRENCY_ID);
	drag_prev_src = -1;
	return true;
}
QSqlError DBHelper::initDb() {
    if (!QSqlDatabase::drivers().contains("QSQLITE")) {
        QSqlError er("", "Unable to find database driver", QSqlError::ConnectionError, -1);
        return er;
    }

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(db_path);

    if (!db.open()) return db.lastError();

    QStringList tables = db.tables();
    if (tables.contains("assets", Qt::CaseInsensitive)) return QSqlError();

    QSqlQuery q;

    if (!q.exec(QString("create table currencies(id integer primary key, code varchar, name varchar, description varchar, btc_price_path varchar)")))
        return q.lastError();
    if (!q.exec(QString("create unique index idx_currencies_code ON currencies(code)")))
       return q.lastError();

    if (!q.prepare(QString("insert into currencies (code, name) values (?, ?)")))
        return q.lastError();
    addCurrency("", "");
    addCurrency("BTC", "Bitcoin");
    addCurrency("LTC", "Litecoin");

    if (!q.exec(QString("create table assets(id integer primary key, currency_id integer, quantity real, price_btc real, market_value_btc real, price_usd real, market_value_usd real)")))
        return q.lastError();

    if (!q.prepare(QString("insert into assets (currency_id, quantity) values (?, ?)")))
        return q.lastError();
    q.addBindValue(2);
    q.addBindValue(0.1);
    q.exec();
    q.addBindValue(3);
    q.addBindValue(0.2);
    q.exec();

    if (!q.exec(QString("create table pricecache(trading_pair varchar, price real, timestamp datetime)")))
        return q.lastError();
     if (!q.exec(QString("create unique index idx_trading_pair ON pricecache(trading_pair)")))
        return q.lastError();

    return QSqlError();
}
/**
 * Sell a specific stack of items
 */
bool MenuInventory::sell(ItemStack stack) {
	// can't sell currency
	if (stack.item == CURRENCY_ID) return false;

	// items that have no price cannot be sold
	if (items->items[stack.item].price == 0) return false;

	int value_each = items->items[stack.item].getSellPrice();
	int value = value_each * stack.quantity;
	addCurrency(value);
	items->playSound(CURRENCY_ID);
	drag_prev_src = -1;
	return true;
}
AffixPattern &
AffixPattern::append(const AffixPattern &other) {
    AffixPatternIterator iter;
    other.iterator(iter);
    UnicodeString literal;
    while (iter.nextToken()) {
        switch (iter.getTokenType()) {
        case kLiteral:
            iter.getLiteral(literal);
            addLiteral(literal.getBuffer(), 0, literal.length());
            break;
        case kCurrency:
            addCurrency(iter.getTokenLength());
            break;
        default:
            add(iter.getTokenType());
            break;
        }
    }
    return *this;
}