The man suspected of killing his former fiancee and two others at hospital had a troubled history that including allegedly threatening to confront his estranged wife at her workplace and engaged in aggressive behavior toward women at the city's fire academy, according to a county court filing and officials' statements to NBC Chicago.

Juan Lopez, 32, killed himself after firing 30 bullets at police and employees of Mercy Hospital and Medical Center on Monday, police said Tuesday. His self-inflicted wound came after he was hit by gunfire from responding officers, officials said.

In the shooting spree, Lopez killed a doctor, police officer and pharmacy resident, police said.

The rampage came just a few years after Lopez's then-wife filed for an order of protection from him, citing threats and his pulling his gun.

The wife claimed on the Cook County protection-order request in 2014 that her husband slept with his gun and had threatened her over texts.

“After beginning separation proceedings, respondent has become threatening,” according to claims by the wife, whose name was redacted on the document, said in the filing.

She further claimed that on Dec. 1, 2014, Lopez allegedly “began sending threats via text message to come to my job and cause a scene.”

And she claimed that in two incidents her husband pulled a firearm.

“One was several weeks ago when he felt threatened by a neighbor and ran outside with his gun to look for him,” the wife said on the protection request.

In another incident cited by the wife, “he forgot about a scheduled apartment appraisal and pulled the gun out (and) pointed it to the Realtor.”

Crosses are placed outside of Mercy Hospital where four people were shot and killed yesterday in Chicago, Illinois on November 20, 2018. Scott Olson / Getty Images

In the same year his estranged wife sought protection from him, Lopez was kicked out of the city’s fire academy after being accused of acting aggressively toward women, officials said..

He entered Chicago's firefighter-training academy on March 17, 2014, and was terminated by May 22, officially for not showing up to respond to claims of aggressive and inappropriate behavior toward women, authorities said.

“In the end, he didn’t show up, was called to respond, and he was fired,” fire department spokesman Larry Merritt told NBC Chicago.

Lopez, 32, is alleged to have gunned down Dr. Tamara O’Neal, 38, as she was leaving her shift at Mercy Hospital and Medical Center on Monday, officials said.

They had been engaged at some point before Monday's deadly confrontation, said Chicago police spokesman Anthony Guglielmi, who has called the shooting “domestic-related."

Also killed in the shooting that began at around 3:30 p.m. was responding police officer Samuel Jimenez, 28, and pharmacy resident Dayna Less, 24.

Officer Samuel Jimenez. Chicago Police Department

O’Neal, who worked in the hospital’s emergency room, was remembered by family members as devoted to her job and church.

“She’s one of the sweetest nieces that I have," her aunt, Delores Davis, told NBC Chicago. "She was also dedicated to her church."

Dr Tamara O'Neal Mercy Hospital

A colleague who said he trained O'Neal said that on Monday he tried to save her life.

"I knew her, trained with her, saved lives with her and tonight, tried to save her life," Dr. John Purakal tweeted Monday night. "I broke down in front of my coworkers when we lost her, and tonight I held hands with her mother in prayer. Tonight, we lost a beautiful, resilient, passionate doc. Keep singing, TO."

Less was engaged to be married in June and had recently overcome a disorder that had brought debilitating headaches since she was a teenager, her loved ones said.

Dayna Less Courtesy Brian Less

“She's an only child. She was the best and brightest star on this planet and she didn't deserve what she got,” her father, Brian Less, told reporters in St. John, Indiana, on Tuesday.

“I was going to be giving a wedding speech instead of a eulogy and there is absolutely no reason for this,” he said.

Chicago Police Department Officer Samuel Jimenez. Chicago Police Department

Jimenez, who has a wife and three young children, joined the Chicago Police Department in February of last year and had just finished his probationary period. The officer wasn't normally assigned to the area near Mercy Hospital, but he rushed to the scene when he heard calls of shots fired.

The tragedy could have been worse, but for a stroke of luck — one of the shooter's bullets hit another responding police officer, but it slammed into his holstered pistol and he was not wounded, a Chicago police spokesman said Tuesday.

Jimenez was the 124th police officer killed in the line of duty this year in the United States, according to the National Law Enforcement Officers Memorial Fund.

There are few situations as potentially deadly for a police officer than encountering a dispute between a current or former couple, said Steve Groeninger, spokesman for the law enforcement advocacy group.

“They’re walking into a lot emotion, people not thinking rationally,” Groeninger told NBC News on Tuesday. “It gets heated and out of control quickly.”