/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.

	This file is part of Sortix.

	Sortix 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.

	Sortix 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 Sortix. If not, see <http://www.gnu.org/licenses/>.

	panic.cpp
	Displays an error whenever something critical happens.

******************************************************************************/

#include "platform.h"
#include "memory.h"
#include "globals.h"
#include "iprintable.h"
#include "log.h"
#include "panic.h"

namespace Sortix
{
	DEFINE_GLOBAL_OBJECT(PanicClass, Panic);

	void PanicClass::Init()
	{
// TODO: Remove this.
#if 0
		Log::Init();

		Colors[Line] = (COLOR8_GREEN << 0) + (COLOR8_BLACK << 4);
		Print("\n>: 4 8 15 16 23 42\n");
		Halt();
#endif

		Colors[Line] = (COLOR8_RED << 0) + (COLOR8_BLACK << 4);
		Print("                                                       _                        ");
		Print("                                                      / \\                       ");
		Print("                  /\\    /\\                           /   \\                      ");
		Print("                 /  \\  /  \\                          |   |                      ");
		Print("                /    \\/    \\                         |   |                      ");
		Print("               |  X    X    \\_______________________ /   |                      ");
		Print("               |                                         |                      ");
		Print("               | _________                               /                      ");
		Print("                \\                                       /                       ");
		Print("                  ------       ---------------      ---/                        ");
		Print("                       /       \\             /      \\                           ");
		Print("                      /         \\           /        \\                          ");
		Print("                     /           \\         /          \\                         ");
		Print("                    /_____________\\       /____________\\                        ");
		Print("                                                                                ");
		Print("                                                                                ");
		Colors[Line] = (COLOR8_RED << 0) + (COLOR8_DARK_GREY << 4);
		Print("                              RED MAXSI OF DEATH                                ");
		Colors[Line] = (COLOR8_RED << 0) + (COLOR8_BLACK << 4);
		Print("                                                                                ");
	}

	void PanicClass::Halt()
	{
		UpdateScreen();
		HaltKernel();
	}

	extern "C" void Panic(const char* Error)
	{
		GPanic->Init();
		GPanic->Print(Error);
		GPanic->Halt();	
	}

	extern "C" void PanicF(const char* Format, ...)
	{
		GPanic->Init();
		GPanic->PrintFV(Format, (void*) (&Format + 1));
		GPanic->Halt();	
	}
}
