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_NONE )
	self:SetSolid( SOLID_VPHYSICS )
	self:SetUseType(SIMPLE_USE)
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableMotion(false)
	end
	self:UseClientSideAnimation()
	self:DropToFloor()


	// Customer Setup
	self.Customers = {}

	self:SetupCustomers(1,self:GetAngles(),self:GetPos()+self:GetForward()*-33 + self:GetUp()*2,self:GetPos()+self:GetForward()*-30 + self:GetUp()*-18,self:GetPos()+self:GetForward()*-15 + self:GetUp()*18)

	local ang02 = self:GetAngles()
	ang02:RotateAroundAxis(self:GetUp(),90)
	self:SetupCustomers(2,ang02,self:GetPos()+self:GetRight()*30 + self:GetUp()*2,self:GetPos()+self:GetRight()*27 + self:GetUp()*-18,self:GetPos()+self:GetRight()*15 + self:GetUp()*18)

	local ang03 = self:GetAngles()
	ang03:RotateAroundAxis(self:GetUp(),-90)
	self:SetupCustomers(3,ang03,self:GetPos()+self:GetRight()*-33 + self:GetUp()*2,self:GetPos()+self:GetRight()*-30 + self:GetUp()*-18,self:GetPos()+self:GetRight()*-15 + self:GetUp()*18)

	local ang04 = self:GetAngles()
	ang04:RotateAroundAxis(self:GetUp(),180)
	self:SetupCustomers(4,ang04,self:GetPos()+self:GetForward()*33 + self:GetUp()*2,self:GetPos()+self:GetForward()*30 + self:GetUp()*-18,self:GetPos()+self:GetForward()*15 + self:GetUp()*18)

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

// This is used for spawning the cusomer, plate and chair
function ENT:SetupCustomers(num,ang,ChairPos,GuyPos,PlatePos)
	self:CreateProp("zpizmak_animbase",ChairPos,ang,"models/props_interiors/furniture_chair01a.mdl")

	local  customer = self:CreateProp("zpizmak_animbase",GuyPos,ang,"models/alyx.mdl")
	customer:SetNoDraw(true)

	zpizmak.f.CreateAnimTable(customer,"d1_t03_sit_bed",1)

	local plate = self:CreateProp("zpizmak_plate",PlatePos,ang,"models/maxofs2d/hover_plate.mdl")
	plate:SetNoDraw(true)

	self.Customers[num] = {agent = customer, customerID = 1, agentPlate = plate, agentState = "DISABLED"}
end

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

	end
end

//NOT USED
function ENT:Return_FreeSeatID()
	local seatID = nil

	for k,v in pairs(self.Customers) do
		if(v.agentState == "DISABLED")then
			seatID = k
			break
		end
	end
	return seatID
end

// This returns the first free seat it can finds
function ENT:Return_FreeSeat()
	local seat

	for k,v in pairs(self.Customers) do
		if(v.agentState == "DISABLED")then
			seat = v
			break
		end
	end
	return seat
end

// This returns the count of active Customer
function ENT:Return_ActiveCustomerCount()
	local customerCount = 0
	for k,v in pairs(self.Customers) do
		if(v.agentState == "WAITING")then
			customerCount = customerCount + 1
		end
	end
	return customerCount
end

// This Spawn/Enables the Customer
function ENT:SpawnCustomer(TheCustomer)
	//local TheCustomer = self.Customers[ seatID ]

	local customerDataID = math.random( #zpizmak.config.Customers )
	local customerData = zpizmak.config.Customers[ customerDataID ]
	TheCustomer.agent:SetModel(customerData.Model)
	TheCustomer.customerID = customerDataID

	local sitAnim = customerData.SitAnim[ math.random( #customerData.SitAnim ) ]
	zpizmak.f.CreateAnimTable(TheCustomer.agent,sitAnim,1)

	TheCustomer.agent:SetNoDraw(false)
	TheCustomer.agentPlate:SetNoDraw(false)

	local wish = zpizmak.f.GetPizzaByName(zpizmak.f.CalcPizzaByChance())

	local waitTime = wish.BakeTime + zpizmak.config.Customer_ExtraWaitTime
	TheCustomer.agentPlate:SetPizzaType(wish.Name)
	TheCustomer.agentPlate:SetPizzaWaitTime(CurTime()+waitTime)
	TheCustomer.agentState = "WAITING"

	timer.Create( "CustomerTable_"..self:EntIndex().."_Customer_"..TheCustomer.agent:EntIndex(), waitTime, 1, function()
		if(IsValid(TheCustomer.agent))then
			self:DeSpawnCustomer(TheCustomer,0)
		end
	end )
end

// This DeSpawn the Customer
function ENT:DeSpawnCustomer(customer,income)
	timer.Remove( "CustomerTable_"..self:EntIndex().."_Customer_"..customer.agent:EntIndex() )
	customer.agent:SetNoDraw(true)
	customer.agentPlate:SetNoDraw(true)
	customer.agentState = "DISABLED"
	customer.agentPlate:SetPizzaWaitTime(-1)
	customer.agentPlate:SetPizzaType("nil")

	if(IsValid(self.OpenSign))then
		self.OpenSign:CustomerDespawned(income)
	end
end

// This Despawns all customers
function ENT:DeSpawnCustomers()
	for k, v in pairs(self.Customers) do
		if(v.agentState ~= "DISABLED")then
			timer.Remove( "CustomerTable_"..self:EntIndex().."_Customer_"..v.agent:EntIndex() )
			v.agent:SetNoDraw(true)
			v.agentPlate:SetNoDraw(true)
			v.agentState = "DISABLED"
			v.agentPlate:SetPizzaWaitTime(-1)
			v.agentPlate:SetPizzaType("nil")

			if(IsValid(self.OpenSign))then
				self.OpenSign:CustomerDespawned(0)
			end
		end
	end
end

// Here we make sure the Customer Count gets recalculated if a Table gets removed
function ENT:OnRemove()
	if(IsValid(self.OpenSign))then
		self.OpenSign:ReCalc_CustomerCount()
	end
end

// Here we serv the Pizza
function ENT:ServPizza(pizza,ply)
	local pizzaType = pizza:GetPizzaType()

	local ValidCustomer
	local ValidCustomerTime = 999999
	for k, v in pairs(self.Customers) do
		if( v.agentPlate:GetPizzaType() == pizzaType )then
			if(v.agentPlate:GetPizzaWaitTime() < ValidCustomerTime)then
				ValidCustomer = v
				ValidCustomerTime = v.agentPlate:GetPizzaWaitTime()
			end
		end
	end

	if(ValidCustomer ~= nil)then
		local pizzaData = zpizmak.f.GetPizzaByName(pizzaType)
		local pizzaPrice = zpizmak.f.GetPizzaFullIngredientPrice(pizzaType)
		pizzaPrice = pizzaPrice + pizzaData.Price

		local earnings = pizzaPrice

		if(pizza:GetCurrentState_Index() == 3)then

			earnings = pizzaPrice
			zpizmak.f.Notify(ply, zpizmak.language.Customer_ServPizza_good, 0 )
		elseif(pizza:GetCurrentState_Index() == 4)then

			earnings = pizzaPrice*zpizmak.config.Customer_BurnedPizzaPenalty
			zpizmak.f.Notify(ply, zpizmak.language.Customer_ServPizza_bad, 1 )
		end

		timer.Remove( "CustomerTable_"..self:EntIndex().."_Customer_"..ValidCustomer.agent:EntIndex() )
		pizza:Remove()

		local customerData = zpizmak.config.Customers[ ValidCustomer.customerID ]
		local ServAnim = customerData.ServAnim[ math.random( #customerData.ServAnim ) ]

		zpizmak.f.CreateAnimTable(ValidCustomer.agent,ServAnim,1)

		local dur = ValidCustomer.agent:SequenceDuration( ValidCustomer.agent:LookupSequence(ServAnim))
		timer.Simple(dur ,function()
			if(IsValid(self) && IsValid(ValidCustomer.agent))then
				zpizmak.f.Notify(ply, zpizmak.language.Customer_Pays.."  +"..tostring(earnings)..zpizmak.config.Currency, 0 )
				self:DeSpawnCustomer(ValidCustomer,earnings)
			end
		end)
	end
end
//////


function ENT:CreateProp(class,pos,ang,model)
	local ent = ents.Create(class)
	ent:SetModel(model)
	ent:SetAngles( ang )
	ent:SetPos( pos )
	ent:SetParent(self)
	ent:Spawn()
	ent:Activate()
	ent.PhysgunDisabled = true
	ent:SetCollisionGroup(COLLISION_GROUP_WEAPON )

	return ent
end
