zrmine = zrmine or {}
zrmine.f = zrmine.f or {}

if SERVER then
	function zrmine.f.Notify(ply, msg, ntfType)
		if (not IsValid(ply)) then return end

		if DarkRP then
			DarkRP.notify(ply, ntfType, 8, msg)
		else
			ply:ChatPrint(msg)
		end
	end

	-- Is the Player a Customer of the Metal Buyer?
	function zrmine.f.IsMetalBuyerCustomer(ply)
		local rightJob = false

		if (table.Count(zrmine.config.BuyerNPC_customers) > 0) then
			for k, v in pairs(zrmine.config.BuyerNPC_customers) do
				if (v == team.GetName(ply:Team())) then
					rightJob = true
					break
				end
			end
		else
			rightJob = true
		end

		-- If we run a nutscript or BaseWars server then there are no Jobs just Fractions
		if (zrmine.config.selectedGameMode == "NutScript" or zrmine.config.selectedGameMode == "BaseWars") then
			rightJob = true
		end

		return rightJob
	end

	-- This function tells us if the player has metal
	function zrmine.f.HasPlayerMetal(ply)
		local metalCount = 0

		for k, v in pairs(ply:zrms_GetMetalBars()) do
			if (v > 0) then
				metalCount = metalCount + v
			end
		end

		if (metalCount > 0) then
			return true
		else
			return false
		end
	end

	-- This function is used do sell Metal Bars
	function zrmine.f.SellBars(ply, buyer)
		-- Calculate the Earnings
		local ply_MetalBars = ply:zrms_GetMetalBars()
		local Earning = 0
		local sellProfit = buyer:GetBuyRate() / 100
		local SoldBars = {}

		for k, v in pairs(ply_MetalBars) do
			if (v > 0) then
				Earning = Earning + (v * zrmine.config.BarValue[k]) * sellProfit
				ply:zrms_AddMetalBarsSold(k, v)
				table.insert(SoldBars, k .. "Bar:" .. v)
			end
		end

		-- Give the player the Cash
		if (zrmine.config.selectedGameMode == "DarkRP") then
			ply:addMoney(Earning)
		elseif (zrmine.config.selectedGameMode == "NutScript") then
			ply:getChar():giveMoney(Earning)
		elseif (zrmine.config.selectedGameMode == "BaseWars") then
			ply:GiveMoney(Earning)
		end

		zrmine.f.Notify(ply, string.Implode(" ", SoldBars), 0)
		zrmine.f.Notify(ply, "+ " .. math.Round(Earning) .. zrmine.config.BuyerNPC_Currency, 0)
		-- Resets Players bar  amount
		ply:zrms_ResetMetalBars()
	end

	--This function is used do calculate the max Capacity amount we can use
	function zrmine.f.Calculate_AmountCap(hAmount, cap)
		local sAmount

		if (hAmount > cap) then
			sAmount = cap
		else
			sAmount = hAmount
		end

		return sAmount
	end

	-- This saves the owners SteamID
	function zrmine.f.SetOwnerID(ent, ply)
		if (IsValid(ply)) then
			ent:SetNWString("ZRMS_Owner", ply:SteamID())
			if CPPI then
				ent:CPPISetOwner(ply)
			end
		else
			ent:SetNWString("ZRMS_Owner", "world")
		end
	end
end

-- This function tells us if the player is an Admin
function zrmine.f.IsAdmin(ply)
	local isAdmin = false

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

	return isAdmin
end

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

-- This returns the owner
function zrmine.f.GetOwner(ent)
	if (IsValid(ent)) then
		local id = ent:GetNWString("ZRMS_Owner", "nil")
		local ply = player.GetBySteamID(id)

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

-- This returns true if the input is the owner
function zrmine.f.IsOwner(ply, ent)
	local isOwner = false

	if (zrmine.config.SharedOwnership) then
		isOwner = true
	else
		if (IsValid(ent)) then
			local id = ent:GetNWString("ZRMS_Owner", "nil")
			local ply_id = ply:SteamID()

			if (IsValid(ply) and id == ply_id) then
				isOwner = true
			end
		end
	end

	return isOwner
end
