FactoryErrorDialog::FactoryErrorDialog(
    /* [in] */ IContext* context,
    /* [in] */ ICharSequence* msg)
    : BaseErrorDialog(context)
{
    mHandler = new MyHandler();

    SetCancelable(FALSE);
    AutoPtr<ICharSequence> title;
    context->GetText(R::string::factorytest_failed,
            (ICharSequence**)&title);
    SetTitle(title);
    SetMessage(msg);

    AutoPtr<ICharSequence> text;
    context->GetText(R::string::factorytest_reboot, (ICharSequence**)&text);
    AutoPtr<IMessage> msg2;
    mHandler->ObtainMessage(0, (IMessage**)&msg2);
    AlertDialog::SetButton(IDialogInterface::BUTTON_POSITIVE, text, msg2);

    AutoPtr<IWindow> window;
    GetWindow((IWindow**)&window);
    AutoPtr<IWindowManagerLayoutParams> attrs;
    window->GetAttributes((IWindowManagerLayoutParams**)&attrs);
    AutoPtr<ICharSequence> fcs;
    CString::New(String("Factory Error"), (ICharSequence**)&fcs);
    attrs->SetTitle(fcs);
    window->SetAttributes(attrs);
}
void SpellCreationSystem::ProcessMessage(Message* data) {
	/*
		Check spellcastingcomponent
			If a spell is being cast
				Is it done casting?
					Yes -> create spell
					No -> Finish processing
			If spellname contains a string/int not equal to NOCAST
				If valid spell from spellbook
					Yes -> Check if entity has appropriate resources
						Yes -> Subtract resources and update spellcasting component
						No -> Finish processing
					No -> Finish processing; throw warning
			If spellname contains NOCAST
				Finish processing
	*/
	LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
	LARGE_INTEGER Frequency;

	QueryPerformanceFrequency(&Frequency);
	QueryPerformanceCounter(&StartingTime);

	// Check for valid spell message
	if (data->GetID() == SPELLMESSAGEID)// auto msg = dynamic_cast<SpellMessage*>(data))
	{
		auto msg = static_cast<SpellMessage*>(data);
		QueryPerformanceCounter(&EndingTime);
		ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
		auto spellcastingcomponent = GetEntityComponent<SpellCastingComponent*>(msg->entity, SpellCastingComponentID);
		auto spellbookcomponent = GetEntityComponent<SpellbookComponent*>(msg->entity, SpellbookComponentID);
		auto equipmentcomponent = GetEntityComponent<EquipmentComponent*>(msg->entity, EquipmentComponentID);

		Spell* queuedspell = spellbookcomponent->GetSpell(msg->spellId);

		if (spellcastingcomponent->SpellToCast() == NO_CAST) {
			// Check if spell is still coolingdown
			if (((TimeRunning() - queuedspell->lastcast) >= (queuedspell->cooldown + queuedspell->duration))) {
				// Check any other spell casting requirements
				spellcastingcomponent->SetSpellToCast(msg->spellId);
				spellcastingcomponent->SetCastTime(queuedspell->casttime);
				spellcastingcomponent->SetStartTimeOfCast(TimeRunning());
				spellcastingcomponent->SetCancelable(queuedspell->cancelable);
				castspells_.push_back(msg->entity);

			}
		}

	}
	else {
		std::cout << "Invalid message type! Expected: SpellMessage Receieved: " << typeid(*data).name() << std::endl;
	}

	ElapsedMicroseconds.QuadPart *= 1000000;
	ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;

	//std::cout << ElapsedMicroseconds.QuadPart << std::endl;

}