zpizmak = zpizmak or {}
zpizmak.f = zpizmak.f or {}

if SERVER then
	concommand.Add("zpizmak_debug", function(ply, cmd, args)
		local tr = ply:GetEyeTrace()
		local trEnt = tr.Entity

		if (IsValid(trEnt)) then
			if (trEnt:GetClass() == "zpizmak_customertable") then
				PrintTable(trEnt.Customers)
			elseif (trEnt:GetClass() == "zpizmak_opensign") then
				print("ActiveCustomerCount: " .. trEnt.ActiveCustomerCount)
				print("SignState: " .. tostring(trEnt:GetSignState()))
			end
		end
	end)

	function zpizmak.f.Notify(ply, msg, ntfType)
		if zpizmak.config.selectedGamemode == "DarkRP" then
			DarkRP.notify(ply, ntfType, 8, msg)
		else
			ply:ChatPrint(msg)
		end
	end

	function zpizmak.f.CalcPizzaByChance()
		local PizzaPool = {}

		for k, v in pairs(zpizmak.config.Pizzas) do
			for i = 1, v.SpawnChance do
				table.insert(PizzaPool, v.Name)
			end
		end

		local pizza = PizzaPool[math.random(#PizzaPool)]

		return pizza
	end

	-- This checks if the player is a admin
	function zpizmak.f.IsAdmin(ply)
		local isAdmin = false

		if (table.HasValue(zpizmak.config.allowedRanks, ply:GetUserGroup())) then
			isAdmin = true
		end

		return isAdmin
	end

	-- This saves the owners SteamID
	function zpizmak.f.SetOwnerID(ent, ply)
		if (IsValid(ent) and IsValid(ply)) then
			ent:SetNWString("ZPIZMAK_Owner", ply:SteamID())

			if CPPI then
				ent:CPPISetOwner(ply)
			end
		end
	end
end

function zpizmak.f.GetPizzaByName(name)
	local pizza = nil

	for k, v in pairs(zpizmak.config.Pizzas) do
		if (v.Name == name) then
			pizza = v
			break
		end
	end

	return pizza
end

function zpizmak.f.GetIngredientByName(name)
	local ing = nil

	for k, v in pairs(zpizmak.config.Ingredients) do
		if (v.Name == name) then
			ing = v
			break
		end
	end

	return ing
end

function zpizmak.f.GetPizzaFullIngredientPrice(name)
	local pizza = zpizmak.f.GetPizzaByName(name)
	local pizzaprice = 0

	for k, v in pairs(pizza.Recipe) do
		if (v > 0) then
			pizzaprice = pizzaprice + (zpizmak.f.GetIngredientByName(k).Price * v) + zpizmak.config.Ingredients[1].Price
		end
	end

	return pizzaprice
end

function zpizmak.f.LerpColor(t, c1, c2)
	local c3 = Color(0, 0, 0)
	c3.r = Lerp(t, c1.r, c2.r)
	c3.g = Lerp(t, c1.g, c2.g)
	c3.b = Lerp(t, c1.b, c2.b)
	c3.a = Lerp(t, c1.a, c2.a)

	return c3
end

-- This returns the entites owner SteamID
function zpizmak.f.GetOwnerID(ent)
	return ent:GetNWString("ZPIZMAK_Owner", "nil")
end

function zpizmak.f.GetOwner(ent)
	if (IsValid(ent)) then
		local id = ent:GetNWString("ZPIZMAK_Owner", "nil")
		local ply = player.GetBySteamID(id)

		if (IsValid(ply)) then
			return ply
		else
			return false
		end
	else
		return false
	end
end

-- This returns the entites owner SteamID
function zpizmak.f.IsOwner(ply, ent)
	if (ply:SteamID() == ent:GetNWString("ZPIZMAK_Owner", "nil")) then
		return true
	else
		return false
	end
end

-- Does the player have the correctjob
function zpizmak.f.JobRestriction(ply)
	local hasJob = false

	if (table.Count(zpizmak.config.JobRestriction) > 0) then
		if (table.HasValue(zpizmak.config.JobRestriction, team.GetName(ply:Team()))) then
			hasJob = true
		end
	else
		hasJob = true
	end

	return hasJob
end
