AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')
//----------------------------//

// Spawn
function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 30
	local ent = ents.Create(self.ClassName)
	local angle = ply:GetAimVector():Angle()
	angle = Angle(0,angle.yaw,0)
	//angle:RotateAroundAxis(angle:Up(), 180)
	ent:SetAngles( angle )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	zpizmak.f.SetOwnerID(ent, ply)
	return ent
end
function ENT:Initialize()
	self:SetModel(self.Model)
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	self:SetUseType(SIMPLE_USE)
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableMotion(true)
	end
	self:UseClientSideAnimation()

	self.ActiveCustomerCount = 0


	timer.Simple(0.1,function()
		if(IsValid(self))then
			self:FindTables()
			self:ReCalc_CustomerCount()
		end
	end)
end

// This Connects the Table to its Parent OpenSign
function ENT:FindTables()
	for k,v in pairs(ents.FindByClass("zpizmak_customertable")) do
		if(IsValid(v) && zpizmak.f.GetOwnerID(v) == zpizmak.f.GetOwnerID(self))then
			v.OpenSign = self
			break
		end
	end
end


function ENT:Use( activator, caller )

	if IsValid( caller ) and caller:IsPlayer() then

		if (zpizmak.f.JobRestriction(activator) == false) then return end

		if(!zpizmak.config.EquipmentSharing)then
			if(!zpizmak.f.IsOwner(activator, self))then
				return
			end
		end

		self:SetSignState(!self:GetSignState())

		if(self:GetSignState() == false)then
			local earnedMoney = self:GetSessionEarnings()
			if(earnedMoney > 0)then
				zpizmak.f.Notify(activator, zpizmak.language.OpenSign_RevenueMessage..earnedMoney..zpizmak.config.Currency , 0 )



				if(zpizmak.config.selectedGamemode == "DarkRP")then

					if(zpizmak.config.RevenueSpawn || zpizmak.config.EquipmentSharing)then
						DarkRP.createMoneyBag(self:GetPos()+self:GetUp()*20, earnedMoney)
					else
						activator:addMoney(earnedMoney)
					end

				elseif(zpizmak.config.selectedGamemode == "BaseWars")then

					activator:GiveMoney(earnedMoney)

				end
			end
			self:SetSessionEarnings(0)

			for k,v in pairs(ents.FindByClass("zpizmak_customertable")) do
				if(IsValid(v) && zpizmak.f.GetOwnerID(v) == zpizmak.f.GetOwnerID(self))then
					v:DeSpawnCustomers()
				end
			end
		end
	end
end

// This checks if we are allowed do spawn another customer
function ENT:Calc_NextCustomer()
	if(!self:GetSignState())then return end

	if(self.ActiveCustomerCount < zpizmak.config.Customer_Limit)then

		// Here we get all the customer tables that the player owns
		local PlayerCFreeTables = {}
		for k,v in pairs(ents.FindByClass("zpizmak_customertable")) do
			if(IsValid(v) && zpizmak.f.GetOwnerID(v) == zpizmak.f.GetOwnerID(self))then
				table.insert(PlayerCFreeTables,v)
			end
		end

		// Here we get the first free seat on the table we find
		local freeSeats = {}
		for k,v in pairs(PlayerCFreeTables) do
			if(IsValid(v))then
				local aFreeSeat = v:Return_FreeSeat()
				if(aFreeSeat ~= nil)then
					table.insert(freeSeats,aFreeSeat)
				end
			end
		end

		local freeSeat = freeSeats[ math.random( #freeSeats ) ]

		// Here we spawn the customer on the table
		if(freeSeat ~= nil)then
			local cTable = freeSeat.agent:GetParent()
			cTable:SpawnCustomer(freeSeat)
			self.ActiveCustomerCount = self.ActiveCustomerCount + 1
		end
	end
end

// This recounts all of our customers if needed
function ENT:ReCalc_CustomerCount()
	local count = 0
	for k,v in pairs(ents.FindByClass("zpizmak_customertable")) do
		if(IsValid(v) && zpizmak.f.GetOwnerID(v) == zpizmak.f.GetOwnerID(self))then
			count = count + v:Return_ActiveCustomerCount()
		end
	end
	self.ActiveCustomerCount = count
end

// This removes one customer from our count, gets called by the tables
function ENT:CustomerDespawned(earning)
	self.ActiveCustomerCount = self.ActiveCustomerCount - 1
	self:SetSessionEarnings(self:GetSessionEarnings() + earning)
end
