/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "bolt/carnival/carnival.h"

namespace Bolt {

namespace Carnival {

BarkerTable *CarnivalEngine::createBarker(int16 minIndex, int16 maxIndex) {
	bool success = false;

	BarkerTable *table = new BarkerTable();
	if (table) {
		table->minIndex = minIndex;
		table->maxIndex = maxIndex;
		table->count = maxIndex - minIndex + 1;

		table->handlers = new SideShowHandler[table->count];
		if (!table->handlers) {
			freeBarker(table);
		} else {
			success = true;
			for (int i = 0; i < table->count; i++) {
				table->handlers[i] = nullptr;
			}
		}
	}

	if (success) {
		_curErrorCode = 0;
		return table;
	} else {
		_curErrorCode = 1;
		return nullptr;
	}
}

void CarnivalEngine::freeBarker(BarkerTable *table) {
	if (!table)
		return;

	if (table->handlers)
		delete[] table->handlers;

	delete table;
}

bool CarnivalEngine::registerSideShow(BarkerTable *table, SideShowHandler handler, int16 boothId) {
	if (table->minIndex > boothId || table->maxIndex < boothId) {
		_curErrorCode = 5;
		return false;
	}

	table->handlers[boothId - table->minIndex] = handler;

	_curErrorCode = 0;
	return true;
}

int16 CarnivalEngine::barker(BarkerTable *table, int16 startBooth) {
	int16 prevBooth = 0;
	int16 currentBooth = startBooth;

	while (true) {
		SideShowHandler handler = table->handlers[currentBooth - table->minIndex];

		int16 nextBooth = (this->*handler)(prevBooth);

		if (nextBooth == 0)
			break;

		prevBooth = currentBooth;
		currentBooth = nextBooth;
	}

	return _currentBoothId;
}

bool CarnivalEngine::checkError() {
	return _curErrorCode != 0;
}

} // End of namespace Carnival

} // End of namespace Bolt
