if(!SERVER)then return end
zpizmak = zpizmak || {}
zpizmak.f = zpizmak.f || {}

function zpizmak.f.PublicEnts_Save(ply)

	local data = {}

    if not file.Exists("zpizmak" , "DATA") then
		file.CreateDir("zpizmak")
	end

    for u ,j in pairs(ents.FindByClass("zpizmak_oven")) do
		if zpizmak.f.IsOwner(ply, j)then
			table.insert(data, {class =j:GetClass(),pos = j:GetPos(), ang = j:GetAngles()})
			j:SetNWString("ZPIZMAK_Owner", "world")
		end
	end

    for u ,j in pairs(ents.FindByClass("zpizmak_customertable")) do
		if zpizmak.f.IsOwner(ply, j)then
			table.insert(data, {class =j:GetClass(),pos = j:GetPos(), ang = j:GetAngles()})
			j:SetNWString("ZPIZMAK_Owner", "world")
		end
	end

    for u ,j in pairs(ents.FindByClass("zpizmak_fridge")) do
		if zpizmak.f.IsOwner(ply, j)then
			table.insert(data, {class =j:GetClass(),pos = j:GetPos(), ang = j:GetAngles()})
			j:SetNWString("ZPIZMAK_Owner", "world")
		end
	end

    for u ,j in pairs(ents.FindByClass("zpizmak_opensign")) do
		if zpizmak.f.IsOwner(ply, j)then
			table.insert(data, {class =j:GetClass(),pos = j:GetPos(), ang = j:GetAngles()})
			j:SetNWString("ZPIZMAK_Owner", "world")
		end
	end

	file.Write("zpizmak/"..game.GetMap().."_PublicEnts"..".txt", util.TableToJSON(data))


    zpizmak.f.Notify( ply,"The Public PizzaMaker Entitis have been saved for the map "..game.GetMap().."!", 0 )
end



function zpizmak.f.PublicEnts_Load()

    local path = "zpizmak/"..game.GetMap().."_PublicEnts"..".txt"
    if file.Exists(path , "DATA") then

		local data = file.Read(path, "DATA")
		data = util.JSONToTable(data)

        for k, v in pairs(data) do
            local ent = ents.Create(v.class)
            ent:SetPos(v.pos)
            ent:SetAngles(v.ang)
            ent:Spawn()

            local phys = ent:GetPhysicsObject()
            if (phys:IsValid()) then
                phys:Wake()
                phys:EnableMotion(false)
            end

            if(v.class == "zpizmak_fridge")then
                ent:SetPos(v.pos)
            end
        end

		print("[Zeros PizzaMaker] Finished loading Public PizzaMaker entitis.")
	else

		print("[Zeros PizzaMaker] No map data found for Public PizzaMaker entitis.")
	end

end

hook.Add( "InitPostEntity", "zpizmak_PublicEnts_OnMapLoad", zpizmak.f.PublicEnts_Load)
hook.Add("PostCleanupMap", "zpizmak_PublicEnts_PostCleanUp", zpizmak.f.PublicEnts_Load)

concommand.Add( "zpizmak_savepublicentities", function( ply, cmd, args )
	if zpizmak.f.IsAdmin(ply) then
		zpizmak.f.PublicEnts_Save(ply)
	else
		zpizmak.f.Notify( ply,"You do not have permission to perform this action, please contact an admin.", 1 )
	end
end )

hook.Add("PlayerSay", "zpizmak_HandleConCanCommands" , function(ply, text)
	if string.sub(string.lower(text), 1, 15) == "!savepizzamaker" then
		if table.HasValue(zpizmak.config.allowedRanks, ply:GetUserGroup()) then
			zpizmak.f.PublicEnts_Save(ply)
		else
		    zpizmak.f.Notify( ply,"You do not have permission to perform this action, please contact an admin.", 1 )
		end
	end
end)
