util.AddNetworkString('PlayerSelectClass')

net.Receive('PlayerSelectClass', function(len, ply)
  local ID = tonumber(net.ReadString())
  ply:JoinClass(ID)
end)

local PLAYER = FindMetaTable("Player")
CLASSES = {}
TEAMS = {}

function AddClass(name, color, ply_team, spawnfunction)
  local num = #CLASSES + 1
  CLASSES[num] = {}
  CLASSES[num]["spawnfunction"] = spawnfunction or function(ply) end
  TEAMS[num] = {}
  TEAMS[num].name = name
  TEAMS[num].color = color
  TEAMS[num].team = ply_team
end

function PLAYER:RunSpawnfunction()
  local player_team = self:GetNWInt("ClassID")
  if(not CLASSES[player_team]) then return end
  if(not CLASSES[player_team]["spawnfunction"]) then return end
  CLASSES[player_team]["spawnfunction"](self)
end

function PLAYER:JoinClass(class_num)
  if(not TEAMS[class_num]) then return end
  if(TEAMS[class_num].team) then
    if(TEAMS[class_num].team != self:Team()) then return end
  end
  self:SetNWInt("ClassID", class_num)
end

AddClass("Jason_1", Color(0,0,0), nil, function(ply) 

    ply:SetHealth( 200 )
    ply:SetMaxHealth( 200 )
    ply:SetWalkSpeed(  250 )
    ply:SetRunSpeed( 350 )
    ply:SetModel( "models/models/konnie/jasonpart3/jasonpart3.mdl" )
    ply:Give("tfa_nmrih_fireaxe")

end)

AddClass("A.J. Mason", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 80 )
    ply:SetMaxHealth( 80 )
    ply:SetWalkSpeed(  200 )
    ply:SetRunSpeed( 300 )
    ply:SetModel( "models/models/konnie/punkgirl/punkgirl.mdl" )

end)

AddClass("Adam Palomino", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 100 )
    ply:SetMaxHealth( 90 )
    ply:SetWalkSpeed(  220 )
    ply:SetRunSpeed( 320 )
    ply:SetModel( "models/models/konnie/tough/tough.mdl" )

end)

AddClass("Brandon 'Buggzy' Wilson", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 100 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  240 )
    ply:SetRunSpeed( 340 )
    ply:SetModel( "models/models/konnie/buggzy/buggzy.mdl" )

end)

AddClass("Chad Kensington", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 100 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  230 )
    ply:SetRunSpeed( 330 )
    ply:SetModel( "models/models/konnie/chad/chad.mdl" )

end)

AddClass("Deborah Kim", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 80 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  200 )
    ply:SetRunSpeed( 300 )
    ply:SetModel( "models/models/konnie/deborah/deborah.mdl" )

end)

AddClass("Eric 'J.R.' LaChappa", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 100 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  200 )
    ply:SetRunSpeed( 300 )
    ply:SetModel( "models/models/konnie/eric/eric.mdl" )

end)

AddClass("Jenny Myers", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 80 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  200 )
    ply:SetRunSpeed( 300 )
    ply:SetModel( "models/models/konnie/jenny/jenny.mdl" )

end)

AddClass("Kenny Riedell", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 100 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  220 )
    ply:SetRunSpeed( 320 )
    ply:SetModel( "models/models/konnie/kenny/kenny.mdl" )

end)

AddClass("Tiffany Cox", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 80 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  220 )
    ply:SetRunSpeed( 320 )
    ply:SetModel( "models/models/konnie/tiffany/tiffany.mdl" )

end)

AddClass("Vanessa Jones", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 80 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  240 )
    ply:SetRunSpeed( 340 )
    ply:SetModel( "models/models/konnie/vanessa/vanessa.mdl" )

end)

AddClass("Rob Dier", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 100 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  230 )
    ply:SetRunSpeed( 330 )
    ply:SetModel( "models/models/konnie/hunter/hunter.mdl" )

end)

AddClass("Tommy Jarvis", Color(0,0,0), nil, function(ply)

    ply:SetHealth( 100 )
    ply:SetMaxHealth( 100 )
    ply:SetWalkSpeed(  230 )
    ply:SetRunSpeed( 330 )
    ply:SetModel( "models/models/konnie/tommyjarvis/tommyjarvis.mdl" )

end)


hook.Remove("PlayerSpawn", "SpawnFunction")
hook.Add("PlayerSpawn", "SpawnFunction", function(ply)
  timer.Simple(0.15, function()
    if !IsValid(ply) then return end
    ply:RunSpawnfunction()
  end)
end)

-- Class Setup

--function ClassSetupHook( ply )

 --   if ply:Team() == 0 then 

--        ply:JoinClass(1)

--    else

--        ply:JoinClass(1)

--    end
--end
-- hook.Add("PlayerInitialSpawn", "ClassSetup", ClassSetupHook)