void FireworkShowMenuComponent::fillObjectMenuResponse(SceneObject* sceneObject,
		ObjectMenuResponse* menuResponse, CreatureObject* player) const {

	if (!sceneObject->isFireworkObject())
		return;

	FireworkObject* fireworkShow = cast<FireworkObject*>(sceneObject);

	if (fireworkShow == NULL)
		return;

	DataObjectComponent* data = fireworkShow->getDataObjectComponent()->get();

	if(data == NULL || !data->isFireworkShowData())
		return;

	FireworkShowDataComponent* fireworkShowData = cast<FireworkShowDataComponent*>(data);
	int totalFireworks = fireworkShowData->getTotalFireworkCount();

	menuResponse->addRadialMenuItem(68, 3, "@firework:mnu_show_data");
	menuResponse->addRadialMenuItemToRadialID(68, 69, 3, "@firework:mnu_add_event");
	if (totalFireworks > 0) {
		menuResponse->addRadialMenuItemToRadialID(68, 70, 3, "@firework:mnu_remove_event");
		menuResponse->addRadialMenuItemToRadialID(68, 71, 3, "@firework:mnu_modify_event");
	}
	if (totalFireworks > 1) {
		menuResponse->addRadialMenuItemToRadialID(68, 72, 3, "@firework:mnu_reorder_show");
	}

	TangibleObjectMenuComponent::fillObjectMenuResponse(sceneObject,
			menuResponse, player);

}
void FireworkShowMenuComponent::showData(CreatureObject* player, FireworkObject* fireworkShow) const {
	ManagedReference<PlayerObject*> ghost = player->getPlayerObject();
	if (ghost == NULL || ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_ADDEVENT) || ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_REMOVEEVENT)
			|| ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_REORDERSHOW) || ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_MODIFYEVENT)
				|| ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_DELAYSELECTION)) {
		return;
	}

	DataObjectComponent* data = fireworkShow->getDataObjectComponent()->get();

	if (data == NULL || !data->isFireworkShowData())
		return;

	FireworkShowDataComponent* fireworkShowData = cast<FireworkShowDataComponent*>(data);

	Locker plocker(player);

	ManagedReference<SuiListBox*> suiBox = new SuiListBox(player, SuiWindowType::FIREWORK_SHOW_ADDEVENT, SuiListBox::HANDLESINGLEBUTTON);
	suiBox->setPromptTitle("@firework:data_title");
	suiBox->setPromptText("@firework:data_prompt");
	suiBox->setOkButton(true, "@ok");
	suiBox->setUsingObject(fireworkShow);

	int totalFireworks = fireworkShowData->getTotalFireworkCount();

	for (int i = 0; i < totalFireworks; i++) {
		ManagedReference<FireworkObject*> firework = fireworkShowData->getFirework(i);
		String fireworkDelay = String::valueOf(float(fireworkShowData->getFireworkDelay(i)) / 1000.f);
		String menuItem = "(" + String::valueOf(i) + ":" + fireworkDelay + "s) " + firework->getDisplayedName();
		suiBox->addMenuItem(menuItem);
	}

	ghost->addSuiBox(suiBox);
	player->sendMessage(suiBox->generateMessage());
}
void FireworkObjectImplementation::beginShowLaunch(CreatureObject* player) {
	if (player == NULL || player->getParent() != NULL)
		return;

	ManagedReference<FireworkObject*> fireworkShow = _this.getReferenceUnsafeStaticCast();

	if (fireworkShow == NULL)
		return;

	DataObjectComponent* data = fireworkShow->getDataObjectComponent()->get();

	if(data == NULL || !data->isFireworkShowData())
		return;

	FireworkShowDataComponent* fireworkShowData = cast<FireworkShowDataComponent*>(data);

	if (fireworkShowData->getTotalFireworkCount() == 0)
		return;

	ManagedReference<StaticObject*> showLauncherObject = (getZoneServer()->createObject(fireworkObject.hashCode(), 0)).castTo<StaticObject*>();

	if (showLauncherObject == NULL)
		return;

	player->setPosture(CreaturePosture::CROUCHED);
	player->doAnimation("manipulate_low");

	float angle = player->getDirectionAngle();

	if (angle > 360)
		angle = angle - 360;

	float distance = 2.0;

	angle = 2 * M_PI * angle / 360;

	int x = player->getPositionX() + sin(angle) * (distance);
	int y = player->getPositionY() + cos(angle) * (distance);
	int z = player->getZone()->getHeight(x, y);
	Locker locker(showLauncherObject);
	showLauncherObject->initializePosition(x, z, y);
	player->getZone()->transferObject(showLauncherObject, -1, true);

	fireworkShow->destroyObjectFromWorld(true);

	int launchDelay = fireworkShowData->getFireworkDelay(0);
	Reference<FireworkShowLaunchFireworkEvent*> fireworkShowLaunchFireworkEvent = new FireworkShowLaunchFireworkEvent(player, fireworkShow, fireworkShowData, showLauncherObject);
	fireworkShowLaunchFireworkEvent->schedule(launchDelay);

}
void FireworkObjectImplementation::destroyObjectFromDatabase(bool destroyContainedObjects) {
	ManagedReference<FireworkObject*> firework = _this.getReferenceUnsafeStaticCast();

	if (firework != NULL && firework->isFireworkObject()) {

		DataObjectComponent* data = firework->getDataObjectComponent()->get();

		if(data != NULL && data->isFireworkShowData()) {
			FireworkShowDataComponent* fireworkShowData = cast<FireworkShowDataComponent*>(data);
			if (fireworkShowData->getTotalFireworkCount() > 0) {
				for (int i=0; i< fireworkShowData->getTotalFireworkCount(); i++) {
					ManagedReference<FireworkObject*> firework = fireworkShowData->getFirework(0);
					fireworkShowData->removeFirework(0);
					if (firework != NULL)
						firework->destroyObjectFromDatabase(false);
				}
			}

		}

	}

	TangibleObjectImplementation::destroyObjectFromDatabase(destroyContainedObjects);
}
int FireworkObjectImplementation::getDisplayedUseCount() {

	ManagedReference<FireworkObject*> firework = _this.getReferenceUnsafeStaticCast();

	if (firework == NULL)
		return 0;

	DataObjectComponent* data = firework->getDataObjectComponent()->get();

	if (data != NULL && data->isFireworkShowData()) {
		FireworkShowDataComponent* fireworkShowData = cast<FireworkShowDataComponent*>(data);

		if (fireworkShowData == NULL)
			return 0;

		return fireworkShowData->getTotalFireworkCount();
	}

	return useCount;
}
void FireworkShowMenuComponent::reorderShow(CreatureObject* player, FireworkObject* fireworkShow) const {
	ManagedReference<PlayerObject*> ghost = player->getPlayerObject();
	if (ghost == NULL || ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_ADDEVENT) || ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_REMOVEEVENT)
			|| ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_REORDERSHOW) || ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_MODIFYEVENT)
				|| ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_DELAYSELECTION)) {
		return;
	}

	DataObjectComponent* data = fireworkShow->getDataObjectComponent()->get();

	if (data == NULL || !data->isFireworkShowData())
		return;

	FireworkShowDataComponent* fireworkShowData = cast<FireworkShowDataComponent*>(data);

	Locker plocker(player);

	ManagedReference<SuiListBox*> suiBox = new SuiListBox(player, SuiWindowType::FIREWORK_SHOW_REORDERSHOW, SuiListBox::HANDLETHREEBUTTON);
	suiBox->setPromptTitle("@firework:modify_index_title"); // Remove Show Event
	suiBox->setPromptText("@firework:modify_index_prompt"); // Select the event index for which you would like to modify the delay time.
	suiBox->setOkButton(true, "@ui:done");
	suiBox->setCancelButton(true, "@ui:moveup");
	suiBox->setOtherButton(true, "@ui:movedown");
	suiBox->setUsingObject(fireworkShow);

	int totalFireworks = fireworkShowData->getTotalFireworkCount();

	for (int i = 0; i < totalFireworks; i++) {
		ManagedReference<FireworkObject*> firework = fireworkShowData->getFirework(i);
		String fireworkDelay = String::valueOf(float(fireworkShowData->getFireworkDelay(i)) / 1000.f);
		String menuItem = "(" + String::valueOf(i) + ":" + fireworkDelay + "s) " + firework->getDisplayedName();
		suiBox->addMenuItem(menuItem, firework->getObjectID());
	}

	suiBox->setCallback(new FireworkShowReorderShowSuiCallback(player->getZoneServer()));
	ghost->addSuiBox(suiBox);
	player->sendMessage(suiBox->generateMessage());
}
void FireworkObjectImplementation::fillAttributeList(AttributeListMessage* alm, CreatureObject* object) {
	TangibleObjectImplementation::fillAttributeList(alm, object);

	ManagedReference<FireworkObject*> firework = _this.getReferenceUnsafeStaticCast();

	if (firework == NULL)
		return;

	DataObjectComponent* data = firework->getDataObjectComponent()->get();

	if (data == NULL || !data->isFireworkShowData()) {
		String effect = "@firework_n:" + fireworkObject.subString(fireworkObject.lastIndexOf("/") + 1, fireworkObject.lastIndexOf("."));
		alm->insertAttribute("@obj_attr_n:pattern", effect);
	} else {
		FireworkShowDataComponent* fireworkShowData = cast<FireworkShowDataComponent*>(data);

		if (fireworkShowData == NULL)
			return;

		alm->insertAttribute("examine_hoppersize", firework->getCapacity());

		int totalFireworks = fireworkShowData->getTotalFireworkCount();
		alm->insertAttribute("blank_entry", "");
		alm->insertAttribute("length", "\\#pcontrast2 " + String::valueOf(totalFireworks) + " Fireworks");

		for (int i = 0; i < totalFireworks; i++) {
			ManagedReference<FireworkObject*> firework = fireworkShowData->getFirework(i);
			if (firework == NULL)
				continue;
			String fireworkObj = firework->getFireworkObjectPath();
			String effect = "@firework_n:" + fireworkObj.subString(fireworkObj.lastIndexOf("/") + 1, fireworkObj.lastIndexOf("."));
			alm->insertAttribute("pattern", effect);
		}

	}

}
void FireworkShowMenuComponent::addEvent(CreatureObject* player, FireworkObject* fireworkShow) const {

	DataObjectComponent* data = fireworkShow->getDataObjectComponent()->get();

	if (data == NULL || !data->isFireworkShowData())
		return;

	FireworkShowDataComponent* fireworkShowData = cast<FireworkShowDataComponent*>(data);

	int curFireworks = fireworkShowData->getTotalFireworkCount();
	int showCapacity = fireworkShow->getCapacity();

	ManagedReference<PlayerObject*> ghost = player->getPlayerObject();

	if (ghost == NULL)
		return;

	if (curFireworks >= showCapacity && !ghost->isPrivileged()) {
		player->sendSystemMessage("This firework show is at full capacity.");
		return;
	}

	if (ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_ADDEVENT) || ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_REMOVEEVENT)
			|| ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_REORDERSHOW) || ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_MODIFYEVENT)
				|| ghost->hasSuiBoxWindowType(SuiWindowType::FIREWORK_SHOW_DELAYSELECTION)) {
		return;
	}

	Locker plocker(player);

	ManagedReference<SuiListBox*> suiBox = new SuiListBox(player, SuiWindowType::FIREWORK_SHOW_ADDEVENT, SuiListBox::HANDLETWOBUTTON);
	suiBox->setPromptTitle("Select Show Addition");
	suiBox->setPromptText("Select the firework to append to the end of the show package.");
	suiBox->setOkButton(true, "@ok");
	suiBox->setCancelButton(true, "@cancel");
	suiBox->setUsingObject(fireworkShow);

	ManagedReference<SceneObject*> inventory = player->getSlottedObject("inventory");
	SceneObject* sceneObject = NULL;

	for (int i = 0; i < inventory->getContainerObjectsSize(); i++) {
		sceneObject = inventory->getContainerObject(i);
		if (sceneObject == NULL)
			continue;

		FireworkObject* firework = cast<FireworkObject*>(sceneObject);
		if (firework == NULL)
			continue;

		DataObjectComponent* data = firework->getDataObjectComponent()->get();

		if (data != NULL && data->isFireworkShowData())
			continue;

		if (sceneObject->getObjectID() != fireworkShow->getObjectID()) {
			String itemWithUseCount = sceneObject->getDisplayedName() + " (" + firework->getUseCount() + ")";
			suiBox->addMenuItem(itemWithUseCount, sceneObject->getObjectID());
		}
	}

	suiBox->setCallback(new FireworkShowAddEventSuiCallback(player->getZoneServer()));
	ghost->addSuiBox(suiBox);
	player->sendMessage(suiBox->generateMessage());
}