local Characters = {};

usermessage.Hook( "RecieveCharacters", function( um )
	local ID = um:ReadShort();
	
	Characters[ ID ] = { Name = um:ReadString(), Model = um:ReadString() };
end );

local function CharacterCreate()
	local PlayerModels = epsilonScript.Config:Get( "Models" )
	local SelectedModel = PlayerModels[ 1 ];
	local Frame = SimpleDFrame( 500, 490, nil, nil, true, "Create a character!" );
	Frame:ShowCloseButton( false );
	Frame:SetBackgroundBlur( true );
	
	local ModelPnl = vgui.Create( "DModelPanel", Frame );
	ModelPnl:SetSize( 250, 250 );
	ModelPnl:SetPos( 1, 20 );
	ModelPnl:SetModel( PlayerModels[ 1 ] );
	ModelPnl:SetAnimated( true );
	ModelPnl:SetAnimSpeed( 0 );
	
	local DForm1 = vgui.Create( "DForm", Frame );
	DForm1:SetPos( Frame:GetWide() - 255, 28 );
	DForm1:SetSize( 250, 202 );
	DForm1:SetPadding( 5 );
	DForm1:SetName( "Character Info" );
	DForm1.Paint = function()
		draw.DrawSimpleOutlined( 0, 0, DForm1:GetWide(), DForm1:GetTall(), Color( 0, 0, 0, 255 ) );
	end;

	local DForm2 = vgui.Create( "DForm" );
	DForm2:SetAutoSize( true );
	DForm2:SetName( "Name" );
	DForm2.Paint = function()
		draw.DrawSimpleOutlined( 0, 0, DForm2:GetWide(), DForm2:GetTall(), Color( 0, 0, 0, 255 ) );
	end;
	DForm1:AddItem( DForm2 );
	
	local ForeName = SimpleLabel( nil, "Forename", Color( 255, 255, 255, 255 ), "DefaultBold", nil, nil );
	local ForeNameEntry = vgui.Create( "DTextEntry" );
	ForeNameEntry:SetEditable( true );
	
	DForm2:AddItem( ForeName );
	DForm2:AddItem( ForeNameEntry );
	
	local SurName = SimpleLabel( nil, "Surname", Color( 255, 255, 255, 255 ), "DefaultBold", nil, nil );
	local SurNameEntry = vgui.Create( "DTextEntry" );
	SurNameEntry:SetEditable( true );
	
	DForm2:AddItem( SurName );
	DForm2:AddItem( SurNameEntry );
	
	local Title = SimpleLabel( nil, "Title", Color( 255, 255, 255, 255 ), "DefaultBold", nil, nil );
	local TitleEntry = vgui.Create( "DTextEntry" );
	TitleEntry:SetEditable( true );
	
	DForm2:AddItem( Title );
	DForm2:AddItem( TitleEntry );
	
	local DForm3 = vgui.Create( "DForm" );
	DForm3:SetAutoSize( true );
	DForm3:SetName( "Model" );
	DForm3.Paint = function()
		draw.DrawSimpleOutlined( 0, 0, DForm3:GetWide(), DForm3:GetTall(), Color( 0, 0, 0, 255 ) );
	end;
	DForm1:AddItem( DForm3 );
	
	local List = vgui.Create( "DPanelList" );
	List:SetSize( 0, 200 );
	List:SetPadding( 3 );
	List:SetSpacing( 3 );
	List:EnableHorizontal( true );
	List:EnableVerticalScrollbar();
	DForm3:AddItem( List );
	
	for i = 1, #PlayerModels do
		local SpawnIcon = vgui.Create( "SpawnIcon" );
		SpawnIcon:SetIconSize( 64 );
		SpawnIcon:SetModel( PlayerModels[ i ] );
		SpawnIcon:SetToolTip( "Click on me to select me as model!" );
		SpawnIcon.DoClick = function()
			SelectedModel = PlayerModels[ i ];
			ModelPnl:SetModel( PlayerModels[ i ] );
		end;
		
		List:AddItem( SpawnIcon );
	end;
	
	local Button = SimpleButton( nil, nil, nil, nil, nil, "Finish", function() LocalPlayer():ConCommand( "eps_createnewchar \"" .. ForeNameEntry:GetValue() .. " " .. SurNameEntry:GetValue() .. "\" \"" .. SelectedModel .. "\" \"" .. TitleEntry:GetValue() .. "\"" ) Frame:Remove() end );
	DForm1:AddItem( Button );
end;
concommand.Add( "eps_Createnewcharmenu", CharacterCreate );

concommand.Add( "eps_CharSelectMenu", function()
	local Frame = SimpleDFrame( 450, 360, nil, nil, true, "" );
	Frame:SetDraggable( false );
	Frame:ShowCloseButton( false );
	Frame:SetBackgroundBlur( true );
	
	local List = vgui.Create( "DPanelList", Frame );
	List:SetSize( Frame:GetWide(), Frame:GetTall() );
	List:SetPadding( 3 );
	List:SetSpacing( 3 );
	
	local SelectCharacterForm = vgui.Create( "DForm" );
	SelectCharacterForm:SetSize( Frame:GetWide() - 10, Frame:GetTall() - 58 );
	SelectCharacterForm:SetPadding( 4 );
	SelectCharacterForm:SetName( "Characters" );
	List:AddItem( SelectCharacterForm );
	
	local CharacterList = vgui.Create( "DPanelList" );
	CharacterList:SetSize( SelectCharacterForm:GetWide(), SelectCharacterForm:GetTall() );
	CharacterList:SetPadding( 3 );
	CharacterList:SetSpacing( 5 );
	CharacterList:EnableVerticalScrollbar();
	SelectCharacterForm:AddItem( CharacterList );
	
	for ID = 1, #Characters do
		local CharacterDPanel = vgui.Create( "DPanel" );
		CharacterDPanel:SetSize( 0, 75 );
		CharacterDPanel.Paint = function() draw.DrawSimpleRect( 0, 0, CharacterDPanel:GetWide(), CharacterDPanel:GetTall(), Color( 90, 90, 90, 255 ) ) draw.DrawSimpleOutlined( 0, 0, CharacterDPanel:GetWide(), CharacterDPanel:GetTall(), Colour_Black ) end; 
		
		local SpawnIcon = vgui.Create( "SpawnIcon", CharacterDPanel );
		SpawnIcon:SetPos( 5, 5 );
		SpawnIcon:SetSize( 64, 64 );
		SpawnIcon:SetModel( Characters[ ID ].Model );
		SpawnIcon:SetVisible( Frame:IsVisible() );
		SpawnIcon:SetMouseInputEnabled( false );
		
		local Label1 = SimpleLabel( CharacterDPanel, Characters[ ID ].Name, Color( 255, 255, 255, 255 ), "DefaultBold", 78, 10 );
		local Button1 = SimpleButton( CharacterDPanel, 78, 40, 100, 20, "Choose", function() LocalPlayer():ConCommand( "eps_selectcharacter " .. ID ) Frame:Remove() end );
		local Button2 = SimpleButton( CharacterDPanel, 188, 40, 100, 20, "Delete", function() local ContextMenu = DermaMenu() ContextMenu:AddOption( "Yes", function() LocalPlayer():ConCommand( "eps_removecharacter " .. ID ) Frame:Remove() Characters = {} end ) ContextMenu:AddOption( "No", function() return end ) ContextMenu:Open() end );
		
		CharacterList:AddItem( CharacterDPanel );
	end;
	
	local Button3 = SimpleButton( nil, nil, nil, nil, nil, "New Character", function() CharacterCreate() Frame:Remove() end );
	List:AddItem( Button3 );
end );
