----------------------------
-- CakeScript (March 29, 2008)
-- by LuaBanana
--
-- Alpha Version
-- cl_createcharacter.lua
----------------------------

ChosenModel = "";

function AddModel(data)

	table.insert(models, data:ReadString())
	
end
usermessage.Hook("addmodel", AddModel);

function SetChosenModel( mdl )

	if(table.HasValue(models, mdl)) then	
	
		ChosenModel = mdl
		
	else
	
		LocalPlayer():PrintMessage(3, ChosenModel .. " is not a valid model!");
		
	end
	
end

ExistingChars = {}

function ReceiveChar( data )
	local n = data:ReadLong();
	ExistingChars[n] = {}
	ExistingChars[n]['name'] = data:ReadString();
	ExistingChars[n]['model'] = data:ReadString();
	
end
usermessage.Hook("ReceiveChar", ReceiveChar);

local function NewLoadingMenu()

Msg("making load menu\n");

	if( CAKE.LoadMenu != nil ) then
	
		CAKE.LoadMenu:Remove();
		CAKE.LoadMenu = nil;
	
	end

	CAKE.LoadMenu = vgui.Create( "DFrame" )
	CAKE.LoadMenu:SetPos( ScrW() / 2 - 320, ScrH() / 2 - 240 )
	CAKE.LoadMenu:SetSize( 640, 480 )
	CAKE.LoadMenu:SetTitle( "Player Menu" )
	CAKE.LoadMenu:SetVisible( true )
	CAKE.LoadMenu:SetDraggable( false )
	CAKE.LoadMenu:ShowCloseButton( false )
	
	CAKE.LoadPropertySheet = vgui.Create( "DPropertySheet" )
	CAKE.LoadPropertySheet:SetParent(CAKE.LoadMenu)
	CAKE.LoadPropertySheet:SetPos( 2, 30 )
	CAKE.LoadPropertySheet:SetSize( 636, 448 )

	local CharPanel = vgui.Create( "DPanelList" )
	CharPanel:SetPadding(20);
	CharPanel:SetSpacing(20);
	CharPanel:EnableHorizontal(false);
	
	local newcharform = vgui.Create( "DForm" );
	newcharform:SetPadding(4);
	newcharform:SetName("New Character");
	newcharform:SetAutoSize(true);
	
	local CharMenu = vgui.Create( "DPanelList" )
	newcharform:AddItem(CharMenu);
	CharMenu:SetSize( 316, 386 )
	CharMenu:SetPadding(10);
	CharMenu:SetSpacing(20);
	CharMenu:EnableVerticalScrollbar();
	CharMenu:EnableHorizontal(false);
	
	local label = vgui.Create("DLabel");
	CharMenu:AddItem(label);
	label:SetSize(400, 25);
	label:SetPos(5, 25);
	label:SetText("Why hallo thar.");

	local info = vgui.Create( "DForm" );
	info:SetName("Personal Information");
	CharMenu:AddItem(info);

	local label = vgui.Create("DLabel");
	info:AddItem(label);
	label:SetSize(30,25);
	label:SetPos(150, 50);
	label:SetText("First: ");

	local firstname = vgui.Create("DTextEntry");
	info:AddItem(firstname);
	firstname:SetSize(100,25);
	firstname:SetPos(185, 50);
	firstname:SetText("");

	local label = vgui.Create("DLabel");
	info:AddItem(label);
	label:SetSize(30,25);
	label:SetPos(5, 50);
	label:SetText("Last: ");

	local lastname = vgui.Create("DTextEntry");
	info:AddItem(lastname);
	lastname:SetSize(100,25);
	lastname:SetPos(40, 50);
	lastname:SetText("");

	local label = vgui.Create("DLabel");
	info:AddItem(label);
	label:SetSize(100,25);
	label:SetPos(5, 80);
	label:SetText("Title: ");

	local occupation = vgui.Create("DTextEntry");
	info:AddItem(occupation);
	occupation:SetSize(205, 25);
	occupation:SetPos(80, 80);
	occupation:SetText("Unemployed");

	local spawnicon = nil;

	local modelform = vgui.Create( "DForm" );
	modelform:SetPadding(4);
	modelform:SetName("Appearance");
	CharMenu:AddItem(modelform);

	local modellist = vgui.Create( "DPanelList" );
	modellist:EnableHorizontal(true);
	modellist:EnableVerticalScrollbar();
	modellist:SetSize(256, 128);
	modelform:AddItem(modellist);

	for n = 1, table.getn(models) do -- Yes, this is Rick's code.
		
		local spawnicon = vgui.Create( "SpawnIcon");
		spawnicon:SetPos( x, y );
		spawnicon:SetSize( 64, 64 );
		spawnicon:SetModel( models[n] );
		--spawnicon:SetToolTip(false);
		spawnicon:SetToolTip( models[n] );

		spawnicon.DoClick = function() SetChosenModel( models[n] ) end
		
		modellist:AddItem(spawnicon);
		
	end

	local apply = vgui.Create("DButton");
	apply:SetSize(100, 25);
	apply:SetText("Apply");
	apply.DoClick = function ( btn )

		
		if(firstname:GetValue() == "" or lastname:GetValue() == "") then
			LocalPlayer():PrintMessage(3, "You must enter a first and last name!");
			return;
		end
		
		if(!table.HasValue(models, ChosenModel)) then
			LocalPlayer():PrintMessage(3, ChosenModel .. " is not a valid model!");
			return;
		end
		
		LocalPlayer():ConCommand("rp_startcreate");
		LocalPlayer():ConCommand("rp_setmodel " .. ChosenModel);
		LocalPlayer():ConCommand("rp_defaultmodel");
		LocalPlayer():ConCommand("rp_setstats"); -- Set spawning stats
		LocalPlayer():ConCommand("rp_changename " .. firstname:GetValue() .. " " .. lastname:GetValue());
		LocalPlayer():ConCommand("rp_job " .. string.sub(occupation:GetValue(), 1, 32));
		LocalPlayer():PrintMessage(3, "Welcome home, " .. firstname:GetValue() .. " " .. lastname:GetValue());
		LocalPlayer().MyModel = ""
		LocalPlayer():ConCommand("rp_finishcreate");
		
		--DrawNewChat();
		
		CAKE.LoadMenu:Remove();
		CAKE.LoadMenu = nil;
		
	end
	CharMenu:AddItem(apply);

	local selectcharform = vgui.Create( "DForm" );
	selectcharform:SetPadding(4);
	selectcharform:SetName("Select Character");
	selectcharform:SetSize(316, 386);

	local charlist = vgui.Create( "DPanelList" );
	selectcharform:AddItem(charlist)
	charlist:SetSize( 316, 386 )
	charlist:SetPadding(10);
	charlist:SetSpacing(20);
	charlist:EnableVerticalScrollbar();
	charlist:EnableHorizontal(true);

	for n = 1, table.getn(ExistingChars) do

		local spawnicon = vgui.Create( "SpawnIcon");
		spawnicon:SetSize( 128, 128 );
		spawnicon:SetIconSize( 128 )
		spawnicon:SetModel(ExistingChars[n]['model']);
		spawnicon:SetToolTip(false);
		spawnicon.DoClick = function ( btn )
		
			LocalPlayer():ConCommand("rp_selectchar " .. n);
			LocalPlayer().MyModel = ""
			
			DrawNewChat();
			
			CAKE.LoadMenu:Remove();
			CAKE.LoadMenu = nil;
			
		end
		charlist:AddItem(spawnicon);
		
		spawnicon.PaintOver = function()
			surface.SetTextColor(Color(255,255,255,255));
			surface.SetFont("Trebuchet18");
			surface.SetTextPos(64 - surface.GetTextSize(ExistingChars[n]['name']) / 2, 5);
			surface.DrawText(ExistingChars[n]['name'])
		end
		
		spawnicon.PaintOverHovered = function()
			surface.SetTextColor(Color(255,255,255,255));
			surface.SetFont("Trebuchet18");
			surface.SetTextPos(64 - surface.GetTextSize(ExistingChars[n]['name']) / 2, 5);
			surface.DrawText(ExistingChars[n]['name'])
		end
		
	end

	
	local divider = vgui.Create("DHorizontalDivider");
	divider:SetLeft(newcharform);
	divider:SetRight(selectcharform);
	divider:SetLeftWidth(316); 

	CharPanel:AddItem(newcharform);
	CharPanel:AddItem(selectcharform);
	CharPanel:AddItem(divider);
	
	CAKE.LoadPropertySheet:AddSheet( "Character Menu", CharPanel, "gui/silkicons/group", false, false, false);
	
	-- this will attach to CAKE.PlayerMenu and will be removed on its next open, hidden when the menu is
	local emergencyFrame = vgui.Create( "DFrame", CAKE.LoadMenu );
	emergencyFrame:SetPos( 540, 400 );
	emergencyFrame:SetSize( 100, 80 );
	emergencyFrame:SetTitle( "Reload Data" );
	emergencyFrame:SetVisible( true );
	emergencyFrame:SetDraggable( false );
	emergencyFrame:ShowCloseButton( false );
	local emergencyButton = vgui.Create( "DButton", emergencyFrame );
	emergencyButton:SetPos( 10, 30 );
	emergencyButton:SetSize(80, 40);
	emergencyButton:SetText("Please wait\nafter pressing");
	emergencyButton.DoClick = function ( btn )
	
		RunConsoleCommand("emergencyFullReloadCommandIsLong", "\n");
	
	end
	
	CAKE.LoadMenu:MakePopup();

end

local function CharacterCreatePanel()

	--CreatePlayerMenu()
	--CAKE.PlayerMenu:ShowCloseButton( false );
	--CAKE.PropertySheet:SetActiveTab( CAKE.PropertySheet.Items[2].Tab );
	--CAKE.PropertySheet.SetActiveTab = function() end;
	
	NewLoadingMenu();
	
	--InitHUDMenu();
	InitHiddenButton();

end
usermessage.Hook("charactercreate", CharacterCreatePanel);