UniqueID = nil;

local function setUniqueID( msg )

	UniqueID = msg:ReadShort();
	
end
usermessage.Hook( "setUniqueID", setUniqueID );

function EndFancyGayIntro()

		TS.FancyGayIntro = false;
		TS.MapView = false;

		HideMouse();

end
usermessage.Hook( "EFGI", EndFancyGayIntro );

function msgCreateOkPanel( msg )

	local title = msg:ReadString()
	local message = msg:ReadString()
	
	CreateOkPanel( title, message )
	
end
usermessage.Hook( "COK", msgCreateOkPanel )	

--Receive item data, cept for description
function RecItemData( msg )

	local name = msg:ReadString();
	local model = msg:ReadString();
	local lookat = msg:ReadVector();
	local campos = msg:ReadVector();
	local fov = msg:ReadShort();
	local w = msg:ReadShort();
	local h = msg:ReadShort();
	local flags = msg:ReadString();
	local price = msg:ReadShort();
	local contains = msg:ReadString();
	
	local id = string.sub( name, 1, string.find( name, ":" ) - 1 );
	name = string.sub( name, string.find( name, ":" ) + 1 );
	
	TS.ItemsData[id] = { Name = name, Model = model, Desc = "", LookAt = lookat, CamPos = campos, Fov = fov, Width = w, Height = h, Flags = flags, Price = price, ContainsModel = contains };

end
usermessage.Hook( "RID", RecItemData );

function RecDescPiece( msg )

	local id = msg:ReadString();
	local desc = msg:ReadString();
	
	TS.ItemsData[id].Desc = TS.ItemsData[id].Desc .. desc;

end
usermessage.Hook( "RDP", RecDescPiece );

function RecItemHUDInfo( msg )

	local id = msg:ReadShort();
	local name = msg:ReadString();
	
	local ent = ents.GetByIndex( id );
	
	ent:GetTable().ItemName = name;

end
usermessage.Hook( "RIHI", RecItemHUDInfo );

LetterContent = "";

if( WritingVGUI ) then

	if( WritingVGUI.Menu ) then 
		WritingVGUI.Menu:Remove();
		WritingVGUI.Menu = nil;
	end	

	WritingVGUI:Remove();

end

WritingVGUI = nil;
PreviewLetterOn = false;
LetterCache = "";
UnfLetterContent = "";
LetterContent = "";

function LetterWritingPrompt( msg )

	local content = msg:ReadString();
	
	LetterCache = LetterCache .. content;
	
	WritingVGUI = vgui.Create( "Panel" );
	if( ScrH() > 600 ) then
		WritingVGUI:SetPos( ScrW() / 2 - 300, 100 );
	else
		WritingVGUI:SetPos( ScrW() / 2 - 300, 0 );
	end
	WritingVGUI:SetSize( 600, 600 );
	WritingVGUI.Paint = function()
	
		draw.RoundedBox( 0, 0, 0, WritingVGUI:GetWide(), WritingVGUI:GetTall(), Color( 255, 255, 255, 200 ) );
		
	end
	
	local x, y = WritingVGUI:GetPos();
	
	WritingVGUI.TextEntry = vgui.Create( "DTextEntry", WritingVGUI );
	WritingVGUI.TextEntry:SetPos( x + 5, y + 5 );
	WritingVGUI.TextEntry:SetSize( 590, 590 );
	WritingVGUI.TextEntry:SetText( WritingVGUI.TextEntry:GetValue() .. LetterCache );
	WritingVGUI.TextEntry:SetMultiline( true );
	WritingVGUI.TextEntry:MakePopup();

	local function Finish()
	
		WritingVGUI:SetVisible( false ); 
		WritingVGUI.TextEntry:SetVisible( false );
		WritingVGUI.Menu:SetVisible( false );
		PreviewLetterOn = false;
	
		local parts = math.ceil( string.len( WritingVGUI.TextEntry:GetValue() ) / 200 );

		RunConsoleCommand( "eng_sl", "" );
		 
		local delay = 0;
		 
		for n = 1, parts do
		
			timer.Simple( delay, RunConsoleCommand, "eng_slp", string.gsub( string.sub( WritingVGUI.TextEntry:GetValue(), ( n - 1 ) * 200 + 1, n * 200 ), "\n", "@n" ) );
			delay = delay + .3;
			
		end
		
		timer.Simple( delay, RunConsoleCommand, "eng_el", "" );
	
	end

	local _, y = WritingVGUI:GetPos();

	WritingVGUI.Menu = CreateBPanel( "Menu", ScrW() / 2 + 300, y, 100, 110 );
	WritingVGUI.Menu:CanSeeTitle( false );
	WritingVGUI.Menu:AddButton( "Edit", 5, 5, function() WritingVGUI:SetVisible( true ); WritingVGUI.TextEntry:SetVisible( true ); PreviewLetterOn = false; end );
	WritingVGUI.Menu:AddButton( "Preview", 5, 30, function() WritingVGUI:SetVisible( false ); WritingVGUI.TextEntry:SetVisible( false ); PreviewLetterOn = true; gui.EnableScreenClicker( true ); LetterContent = FormatLine( WritingVGUI.TextEntry:GetValue(), "LetterFont", 580 ); end );
	WritingVGUI.Menu:AddButton( "Close", 5, 55, function() WritingVGUI:SetVisible( false ); WritingVGUI.TextEntry:SetVisible( false ); WritingVGUI.Menu:SetVisible( false ); PreviewLetterOn = false; HideMouse(); end );
	WritingVGUI.Menu:AddButton( "Finish", 5, 80, Finish );
	
end
usermessage.Hook( "LWP", LetterWritingPrompt );

function StartLetter( msg )

	UnfLetterContent = "";
	LetterContent = "";

end
usermessage.Hook( "SL", StartLetter );

if( LetterVGUI ) then
	
	if( LetterVGUI.Menu ) then
		LetterVGUI.Menu:Remove();
	end

	LetterVGUI:Remove();
end
LetterVGUI = nil;
function DisplayLetter( msg )
	
	if( LetterVGUI ) then
		LetterVGUI:Remove();
	end
	
	local y = 0;

	if( ScrH() > 600 ) then
		y = 100;
	end

	LetterVGUI = vgui.Create( "Panel" );
	LetterVGUI:SetPos( ScrW() / 2 - 300, y );
	LetterVGUI:SetSize( 600, 600 );
	LetterVGUI.Paint = function()
	
		draw.RoundedBox( 0, 0, 0, LetterVGUI:GetWide(), LetterVGUI:GetTall(), Color( 255, 255, 255, 200 ) );
		draw.DrawText( LetterContent, "LetterFont", 5, 5, Color( 0, 0, 0, 255 ) );
	
	end
	
	LetterVGUI.Menu = CreateBPanel( "Menu", ScrW() / 2 + 300, y, 100, 30 );
	LetterVGUI.Menu:CanSeeTitle( false );
	LetterVGUI.Menu:AddButton( "Close", 5, 5, function() LetterVGUI:SetVisible( false ); LetterVGUI.Menu:SetVisible( false ); HideMouse(); end );
	
	gui.EnableScreenClicker( true );
	
end
usermessage.Hook( "DL", DisplayLetter );

function SendLetterPiece( msg )

	UnfLetterContent = UnfLetterContent .. string.gsub( msg:ReadString(), "@n", "\n" );
	
	LetterContent = FormatLine( UnfLetterContent, "LetterFont", 580 );

end
usermessage.Hook( "SLP", SendLetterPiece );

function CitizenGuide( msg )
	
	if( LetterVGUI ) then
		LetterVGUI:Remove();
	end
	
	local y = 0;

	if( ScrH() > 600 ) then
		y = 100;
	end

	LetterVGUI = vgui.Create( "Panel" );
	LetterVGUI:SetPos( ScrW() / 2 - 300, y );
	LetterVGUI:SetSize( 600, 600 );
	LetterVGUI.Paint = function()
	
		draw.RoundedBox( 0, 0, 0, LetterVGUI:GetWide(), LetterVGUI:GetTall(), Color( 255, 255, 255, 200 ) );
		draw.DrawText( "Citizen's Handbook" , "ChatFont", LetterVGUI:GetWide() / 2 - 95 , 5, Color( 0, 0, 0, 255 ) );
		draw.DrawText( "This book is property of Civil Protection\n Do not destroy or damage in anyway.\n\n Property Ownership:\n Owning an apartment or storefront is a privilage.\n Remember to always keep your property kept in good condition.\n\n Always treat Protection Teams with kindness and follow directions.\n To inquire of a job as a CP, first prove yourself a loyal citizen." , "LetterFont", 10 , 35, Color( 0, 0, 0, 255 ) );
	
	end
	
	LetterVGUI.Menu = CreateBPanel( "Menu", ScrW() / 2 + 300, y, 100, 30 );
	LetterVGUI.Menu:CanSeeTitle( false );
	LetterVGUI.Menu:AddButton( "Close", 5, 5, function() LetterVGUI:SetVisible( false ); LetterVGUI.Menu:SetVisible( false ); HideMouse(); end );
	
	gui.EnableScreenClicker( true );
	
end
usermessage.Hook( "CG", CitizenGuide );

function CPGuide( msg )
	
	if( LetterVGUI ) then
		LetterVGUI:Remove();
	end
	
	local y = 0;

	if( ScrH() > 600 ) then
		y = 100;
	end

	LetterVGUI = vgui.Create( "Panel" );
	LetterVGUI:SetPos( ScrW() / 2 - 300, y );
	LetterVGUI:SetSize( 600, 800 );
	LetterVGUI.Paint = function()
	
		draw.RoundedBox( 0, 0, 0, LetterVGUI:GetWide(), LetterVGUI:GetTall(), Color( 255, 255, 255, 200 ) );
		draw.DrawText( "Civil Protection Handbook" , "ChatFont", LetterVGUI:GetWide() / 2 - 125 , 5, Color( 0, 0, 100, 255 ) );
		draw.DrawText( "This book is property of Civil Protection\n Do not destroy or damage in any way.\n\n Classifications\n\n Anti-Citizen: Criminals\n Malignant: Highly dangerous and contagious.\n Infected: Unfriendly ( Refers to Humanoid. )\n Biotic: Antlion or Myrmidont.\n Infestation: High parasite activity\n Parasite: Headcrab\n Necrotic: Zombie\n Outland: Foreign\n Virome: Vortigaunt\n\n Status\n Deserviced: Unit down, Officer unable to perform duties.\n In Position: Ready to perform orders.\n Standing by: Awaiting orders.\n Outbreak: Last remaining unit in group.\n Holding: Maintainging and guarding position.\n Malcompliant: Unable to carry out Civil Obligations.\n\n Actions\n Clamp: Restrict passage, block all Civil Passage through area.\n Anti-Civil: Anything that negatively effects the citizen population.\n Breach: Remove obstacle or defences. \n Stabilize: Restore order through any means required verbally or otherwise.\n Amputate: Execute, destroy.\n Sterilize: Free area or situation of.\n Isolate: Seclude a citizenor a group of citizens from the rest of the population.\n Jurisdiction: Choice of reasoning.\n Stabilization: Stimulation of the Citizen Population during a time of unrest.\n Contain: Prevent forces or situation from spreading beyond its current location.\n Extract: Evacuate\n Pacify: Detain or Neutralize through any means necessary. ( Nonlethal if possible.)\n" , "LetterFont", 10 , 35, Color( 0, 0, 0, 255 ) );
	
	end
	
	LetterVGUI.Menu = CreateBPanel( "Menu", ScrW() / 2 + 300, y, 100, 30 );
	LetterVGUI.Menu:CanSeeTitle( false );
	LetterVGUI.Menu:AddButton( "Close", 5, 5, function() LetterVGUI:SetVisible( false ); LetterVGUI.Menu:SetVisible( false ); HideMouse(); end );
	
	gui.EnableScreenClicker( true );
	
end
usermessage.Hook( "CPG", CPGuide );


ActionMenuOn = false;
ActionMenuTitle = "";
ActionMenuOptions = { }
ActionMenuPos = Vector( 0, 0, 0 );
ActionMenuLongest = 0;
ActionMenuChoice = 0;

function ActionMenu( msg )

	ActionMenuOn = true;
	ActionMenuLongest = 0;

	ActionMenuFade = 1;

	ActionMenuTitle = msg:ReadString();
	ActionMenuPos = msg:ReadVector();
	local n = msg:ReadShort();
	local options = { }
	
	for j = 1, n do
	
		table.insert( options, { Name = msg:ReadString(), Command = msg:ReadString(), ID = msg:ReadShort() } );
		
		surface.SetFont( "NewChatFont" );
		local w = surface.GetTextSize( options[j].Name );
		
		if( w > ActionMenuLongest ) then
			ActionMenuLongest = w;
		end
	
	end
	
	ActionMenuLongest = math.Clamp( ActionMenuLongest, 100, 9999 );
	
	if( ActionMenuLongest > 100 ) then
		ActionMenuLongest = ActionMenuLongest + 6;
	end
	
	ActionMenuOptions = options;
	
end
usermessage.Hook( "AM", ActionMenu );

ProcessBars = { }

function CreateCustomProcessBar( msg )

	local bar = { }
	
	local id = msg:ReadString();

	bar.Title = msg:ReadString();
	bar.Time = msg:ReadFloat();
	bar.StartTime = CurTime();
	bar.EndTime = CurTime() + bar.Time;
	bar.Color = Color( msg:ReadShort(), msg:ReadShort(), msg:ReadShort(), msg:ReadShort() );
	
	ProcessBars[id] = bar;


end
usermessage.Hook( "CCPB", CreateCustomProcessBar );

function CreateProcessBar( msg )

	local bar = { }
	
	local id = msg:ReadString();

	bar.Title = msg:ReadString();
	bar.Time = msg:ReadFloat();
	bar.StartTime = CurTime();
	bar.EndTime = CurTime() + bar.Time;
	bar.Color = Color( 70, 70, 200, 90 );
	
	ProcessBars[id] = bar;

end
usermessage.Hook( "CPB", CreateProcessBar );

function RemoveProcessBar( msg )

	local id = msg:ReadString();
	
	ProcessBars[id] = nil;

end
usermessage.Hook( "RPB", RemoveProcessBar );

function RemoveAllProcessBars( msg )

	for k, v in pairs( ProcessBars ) do
	
		ProcessBars[k] = nil;
	
	end

end
usermessage.Hook( "RAPB", RemoveAllProcessBars );

FadeStart = 0;
FadeEnd = 0;
FIFOAlpha = 0;
FIFOWait = 0;

--Use only for intro
function FadeInFadeOut( msg )

	FadeStart = CurTime();
	FadeWait = msg:ReadFloat();
	FadeEnd = FadeWait / 2 + CurTime();
	FIFOAlpha = 0;

end
usermessage.Hook( "FIFO", FadeInFadeOut );

function TurnOffOWSight()

	OWSight = false;

end
usermessage.Hook( "TOFFOWS", TurnOffOWSight );

function ToggleOWSight()

	OWSight = !OWSight;

end
usermessage.Hook( "TOWS", ToggleOWSight );

AdminSeeAll = false;

function SeeAll()

	AdminSeeAll = !AdminSeeAll;

end
usermessage.Hook( "SeeAll", SeeAll );

function UpdateDoor( msg )

	local ent = msg:ReadEntity();
	local property = msg:ReadString();
	local name = msg:ReadString();
	
	ent:GetTable().PropertyName = property;
	ent:GetTable().DoorName = name;

end
usermessage.Hook( "UDD", UpdateDoor );

function UpdatePlayerTitle( msg )

	local ply = msg:ReadEntity();
	local title = msg:ReadString();

	ply:GetTable().PlayerTitle = title;

end
usermessage.Hook( "UDPT", UpdatePlayerTitle );

StunStickSlapTime = 0;
StunStickWhiteAlpha = 0;
StunStickBlurMul = 0;

function DoStunStickSlap()

	StunStickSlapTime = CurTime();
	StunStickWhiteAlpha = 255;
	StunStickBlurMul = 1;

end
usermessage.Hook( "DSSS", DoStunStickSlap );

function HolsterPos()

	local weap = LocalPlayer():GetActiveWeapon();
	
	if( weap:IsValid() and weap:GetTable() and weap:GetTable().Primary ) then

		weap:GetTable().Primary.PositionMode = 3;
		weap:GetTable().Primary.PositionMul = 1;
		
	end
	

end
usermessage.Hook( "HPOS", HolsterPos );

--Too lazy to convert old shit to events.  Using events from now on.
function event.MapViewOn()

	TS.MapView = true;

end

function event.MapViewOff()

	TS.MapView = false;

end

WaterBlur =
{

	LastUpdate = 0,
	Add = 0,
	Draw = 0,
	Delay = 0,
	MaxAdd = .07,
	MaxDraw = .39,
	MaxDelay = 0.03,

}

--water bottle effect
function event.WaterBlur()

	WaterBlur.Add = math.Clamp( WaterBlur.Add + 0.014, 0, WaterBlur.MaxAdd );
	WaterBlur.Draw = math.Clamp( WaterBlur.Draw + .078, 0, WaterBlur.MaxDraw );
	WaterBlur.Delay = math.Clamp( WaterBlur.Delay + .006, 0, WaterBlur.MaxDelay );
	WaterBlur.LastUpdate = CurTime();

end

AlcoholBlur =
{

	LastUpdate = 0,
	Add = 0,
	Draw = 0,
	Delay = 0,
	HeadBob = 0,
	HeadBobPitchMul = 1,
	HeadBobYawMul = 1,
	HeadBobCosine = 0,
	MaxHeadBob = 1,
	MaxAdd = .21,
	MaxDraw = 1,
	MaxDelay = 0.09,
	ShiftedPitchYaw = false,

}


function event.HAlcoholBlur()

	AlcoholBlur.Add = math.Clamp( AlcoholBlur.Add + AlcoholBlur.MaxAdd * .14, 0, AlcoholBlur.MaxAdd );
	AlcoholBlur.Draw = math.Clamp( AlcoholBlur.Draw + AlcoholBlur.MaxDraw * .14, 0, AlcoholBlur.MaxDraw );
	AlcoholBlur.Delay = math.Clamp( AlcoholBlur.Delay + AlcoholBlur.MaxDelay * .14, 0, AlcoholBlur.MaxDelay );
	AlcoholBlur.LastUpdate = CurTime();

end

function event.HangOver()

	AlcoholBlur.Add = math.Clamp( AlcoholBlur.MaxAdd * .5, 0, AlcoholBlur.MaxAdd );
	AlcoholBlur.Draw = math.Clamp( AlcoholBlur.MaxDraw * .5, 0, AlcoholBlur.MaxDraw );
	AlcoholBlur.Delay = math.Clamp( AlcoholBlur.MaxDelay * .5, 0, AlcoholBlur.MaxDelay );
		

end

function event.NoDrunk()

	AlcoholBlur.Add = 0;
	AlcoholBlur.Draw = 0;
	AlcoholBlur.Delay = 0;

end

---------------------------------------
-- MOTD CREATION 
---------------------------------------

MOTDText = "";

if( MOTD ) then
	
	if( MOTD.TitleBar ) then

		MOTD.TitleBar:Remove();
		MOTD.TitleBar = nil;
		
	end
		
	MOTD:Remove();
	MOTD = nil;
end
function CreateMOTD()

	if( MOTD ) then
		MOTD:Remove();
		MOTD = nil;
	end

	MOTD = CreateBPanel( "Read This", ScrW() / 2 - 250, math.Clamp( ScrH() * .1 + 40, 180, 9999 ), 500, 400 );
	MOTD:CanClose( false );
	MOTD:CanDrag( false );

	FillMOTD();

	gui.EnableScreenClicker( true );

end
usermessage.Hook( "MOTD", CreateMOTD );

function FillMOTD()

	if( MOTD.Text ) then
		MOTD.Text:Remove();
	end

	MOTD.Text = MOTD:AddLabel( MOTDText, "NewChatFont", 5, 3, Color( 255, 255, 255, 255 ) );

	MOTD:EnableScrolling( true );
	
	local function CloseMOTD()
	
		RunConsoleCommand( "rp_closemotd", "" );
		
		timer.Simple( 2, function()
			TS.FancyGayIntro = false;
			TS.MapView = false;
		end );
		
		MOTD.TitleBar:Remove();
		MOTD:Remove();
	
		HideMouse();
	
	end
	
	if( MOTD.ExitButton ) then
		MOTD.ExitButton:Remove();
	end
	
	MOTD.ExitButton = MOTD:AddButton( "Done", 225, MOTD.Text:GetTall() + 50, CloseMOTD );


end

function RestartMOTD()

	MOTDText = "";

end
usermessage.Hook( "RMOTD", RestartMOTD );

function SendMOTDPiece( msg )
	
	MOTDText = MOTDText .. msg:ReadString();
	
	MOTDText = FormatLine( MOTDText, "NewChatFont", 480 );
	
	if( MOTD and MOTD:IsVisible() ) then
		FillMOTD();
	end

end
usermessage.Hook( "SMP", SendMOTDPiece );



