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:SetPos(self:GetPos()+Vector(0,0,25))

	self.SpawnedIngredients = {}
end


// Logic
util.AddNetworkString( "zpizmak_fridge_net" )
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 == false and zpizmak.f.IsOwner(activator, self) == false) then return end

		net.Start("zpizmak_fridge_net")
		net.WriteEntity(self)
		net.Send( activator )
	end
end


util.AddNetworkString("zpizmak_fridge_buy")
net.Receive("zpizmak_fridge_buy",function(len,ply)
	local val = net.ReadInt(16)
	local zpizmak_ConnectedFridge = net.ReadEntity()

	if ( !IsValid(zpizmak_ConnectedFridge) || zpizmak_ConnectedFridge:GetClass() != "zpizmak_fridge" ) then
		return
	end

	if ply:GetPos():Distance(zpizmak_ConnectedFridge:GetPos()) > 300 then
		return
	end

    if !ply:Alive() then
		return
	end

	local item = zpizmak.config.Ingredients[val]

	if(zpizmak.config.selectedGamemode == "DarkRP")then
		if(!ply:canAfford(item.Price))then
			zpizmak.f.Notify(ply, zpizmak.language.FridgeShop_NoMoney, 1 )
			return
		end
	elseif(zpizmak.config.selectedGamemode == "BaseWars")then
		if(ply:GetMoney() < item.Price)then
			zpizmak.f.Notify(ply, zpizmak.language.FridgeShop_NoMoney, 1 )
			return
		end
	end

	// Add check for amount
	local currentIngredientCount = 0
	if(table.Count(zpizmak_ConnectedFridge.SpawnedIngredients) > 0)then
		for k,v in pairs(zpizmak_ConnectedFridge.SpawnedIngredients) do
			if(IsValid(v))then
				currentIngredientCount = currentIngredientCount + 1
			end
		end
		if(currentIngredientCount >zpizmak.config.Ingredient_Limit)then
			zpizmak.f.Notify(ply, zpizmak.language.FridgeShop_LimitReached, 1 )
			return
		end
	end

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

		ply:addMoney(-item.Price)

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

		ply:GiveMoney(-item.Price)

	end

	zpizmak_ConnectedFridge:BuyIngredient(item)

	local message = string.Replace( zpizmak.language.FridgeShop_BoughtItem, "$ItemName", tostring(item.Name) )
	message = string.Replace( message, "$ItemPrice", tostring(item.Price)..zpizmak.config.Currency )
	zpizmak.f.Notify(ply, message, 2 )
end)

function ENT:BuyIngredient(ingredientData)
	local class = "zpizmak_ingredient"
	if(ingredientData.Name == zpizmak.config.Ingredients[1].Name)then
		class = "zpizmak_pizza"
	else
		class = "zpizmak_ingredient"
	end

	local ent = ents.Create(class)
	ent:SetAngles( self:GetAngles() )
	ent:SetPos( self:GetPos() + self:GetForward()*50 + Vector(0,0,40) )
	ent:Spawn()
	ent:Activate()

	if(ingredientData.Name ~= zpizmak.config.Ingredients[1].Name)then
		ent:SetModel(ingredientData.Model)
		ent:SetIngredient_Type(ingredientData.Name)
	end

	ent:PhysicsInit( SOLID_VPHYSICS )
	ent:SetMoveType( MOVETYPE_VPHYSICS )
	ent:SetSolid( SOLID_VPHYSICS )
	ent:SetUseType(SIMPLE_USE)
	local phys = ent:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableMotion(true)
		phys:EnableDrag( true )
		phys:SetDragCoefficient( 1 )
	end


	zpizmak.f.SetOwnerID(ent, zpizmak.f.GetOwner(self))
	table.insert(self.SpawnedIngredients,ent)

	ent.SpawnTime = CurTime() + zpizmak.config.Ingredient_Despawn
end
