I could really use some help

I have a 99' 323i and I'm really concerned about my car. I originally bought it about 9 months ago and I put in some work because it does have 165 K miles. I had the check engine light go on after about a week after I bought it so I did some investigation. About a day after the light came on it went into safe mode so I was thinking the battery might be wacky because the original owner said he just replaced it. Sure enough, I look in the back to find a WALMART battery! It was about the same size as a motorcycle battery! So I replaced it and all the lights went off. I also have the airbag sensor on and the abs light on. The abs light went off after I fixed the brakes. But, the other day I was driving and my check engine light came on. Shortly after the abs light came back on and now it keeps going into safe mode. I drove out last night in orange county and all of a sudden when I would stop at a light it would go down to about 500 rpms and start shaking. I don't know that much about cars and I'm not sure what could be the issue. Any thoughts?

It's possible that the alternator is going bad and not charging the battery. The problems you are having are typical on a BMW if voltage is low. That may be why the Previous Owner put a new battery in it. You did put a battery in it that is specifically for your car, right?
I had a similar problem. The cause of your issue maybe be different than mine. I had to replace several items. It seemed to be a snow ball effect. Have your mechanic check the mass air sensor, and blower resistor. When I replaced the sensor the check engine light turned off. The resistor was the item that drained my battery. Every time I turned everything off in the car the blower would still send air in to the car's cabin and slowly killed my battery. Replaced it a few months ago and no problem as of yet. Hope this helps.