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()

	local attach01 = self:GetAttachment(3)
	local ang01 = attach01.Ang

	ang01:RotateAroundAxis(self:GetUp(),90)

	local door01 = self:CreateProp("zpizmak_animbase",attach01.Pos+self:GetRight()*2.35,ang01)

	local attach02 = self:GetAttachment(4)
	local ang02 = attach02.Ang

	ang02:RotateAroundAxis(self:GetUp(),90)

	local door02 = self:CreateProp("zpizmak_animbase",attach02.Pos+self:GetRight()*2.35,ang02)

	self.PizzaSpots = {}
	self.PizzaSpots[1] = {ent = nil,door = door01}
	self.PizzaSpots[2] = {ent = nil,door = door02}
end
function ENT:CreateProp(class,pos,ang)
	local ent = ents.Create(class)
	ent:SetModel("models/zerochain/props_pizza/zpizmak_ovendoor.mdl")
	ent:SetAngles( ang )
	ent:SetPos( pos )
	ent:SetParent(self)
	ent:Spawn()
	ent:Activate()
	ent.PhysgunDisabled = true
	return ent
end


//This creates a Trace for determining if the Screen got hit
function ENT:Use(activator, caller)

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

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

	local localTrace
	localTrace = caller:GetEyeTrace()
	if(localTrace && caller:GetPos():Distance(localTrace.HitPos) < 300)then
		if (localTrace.Entity && localTrace.Entity == self) then
			self:UseLogic(localTrace,caller)
		end
	end
end

//Here do we check what Button the trace is hitting
function ENT:UseLogic(trace,caller)
	local lTrace = self:WorldToLocal( trace.HitPos )

	if(self:CalcWorldElementPos(lTrace,27,-27,70,55))then
		// Top
		if(IsValid(self.PizzaSpots[2].ent))then
			self:RemovePizza(self.PizzaSpots[2].ent,2)
		end
	end
	if(self:CalcWorldElementPos(lTrace,27,-27,55,38))then
		// Bottom
		if(IsValid(self.PizzaSpots[1].ent))then
			self:RemovePizza(self.PizzaSpots[1].ent,1)
		end
	end
end
//Check if we are inside a 2D area relativ from the Root of the Entity
function ENT:CalcWorldElementPos(trace,xStart,xEnd,yStart,yEnd)
	if trace.y < xStart && trace.y > xEnd && trace.z < yStart && trace.z > yEnd then
		return true
	else
		return false
	end
end

util.AddNetworkString( "zpizmak_Oven_PizzaTransfer" )
function ENT:PlacePizza(pizza,w)
	zpizmak.f.CreateAnimTable(self.PizzaSpots[w].door,"close",1)

	pizza:SetPos(self:GetAttachment(w).Pos)
	pizza:SetParent(self,w)
	pizza:SetAngles(self:GetAttachment(w).Ang)
	self.PizzaSpots[w].ent = pizza
	pizza.InOven = true

	self:SendPizzaInfo()
end

function ENT:SendPizzaInfo()
	local PizzaInfo = {}
	for k, v in pairs(self.PizzaSpots) do
		if(IsValid(v.ent))then
			PizzaInfo[k] = v.ent
		end
	end

	net.Start("zpizmak_Oven_PizzaTransfer")
	net.WriteEntity(self)
	net.WriteTable(PizzaInfo)
	net.Broadcast()
end

function ENT:RemovePizza(pizza,pos)
	zpizmak.f.CreateAnimTable(self.PizzaSpots[pos].door,"open",1)
	self.PizzaSpots[pos].ent = nil

	self:SendPizzaInfo()

	timer.Simple(0.5,function()
		if(IsValid(self))then
			pizza:SetParent(nil)
			pizza:SetPos(self:GetPos()+self:GetUp()*35+self:GetForward()*50)
			pizza:PhysicsInit( SOLID_VPHYSICS )
			pizza:SetMoveType( MOVETYPE_VPHYSICS )
			pizza:SetSolid( SOLID_VPHYSICS )

			local phys = pizza:GetPhysicsObject()
			if (phys:IsValid()) then
				phys:Wake()
				phys:EnableMotion(true)
			end

			pizza.InOven = false
		end
	end)
end


function ENT:Has_FreeSpot()
	local hasFreeSpot
	for k, v in pairs(self.PizzaSpots) do
		if(!IsValid(v.ent))then
			hasFreeSpot = k
			break
		end
	end
	return hasFreeSpot
end

function ENT:BakePizzas()

	if(table.Count(self.PizzaSpots) > 0)then

		local pos = self:GetPos() + self:GetUp()*50

		//debugoverlay.Sphere( pos,30, 1, Color( 0, 255, 0,100 ), false )

		for k, v in pairs(self.PizzaSpots) do
			ent = v.ent
	        if(IsValid(ent) && ent:GetClass() == "zpizmak_pizza")then

				if(ent:GetCurrentState_Index() == 2 || ent:GetCurrentState_Index() == 3)then

					zpizmak.f.CreateEffectTable("zpizmak_oven_main",nil,self,ent:GetAngles(),ent:GetPos() )

		        	ent:Pizza_Bake()
				end
	        end
	    end
	end
end

function ENT:Think()
	if ( CurTime() > (self.lastBake or 1) ) then
		self.lastBake = CurTime() + 1
		self:BakePizzas()
	end
end
