Former FBI agent Peter Strzok filed suit against the Department of Justice on Tuesday, arguing he was wrongly fired for sending private text messages that ripped Donald Trump during the 2016 presidential campaign.

"The decision to fire Special Agent Strzok in violation of his Constitutional rights was the result of a long and public campaign by President Trump and his allies to vilify Strzok and pressure the agency to terminate him," says the complaint, which was filed in federal court in Washington, D.C.

Strzok was a senior FBI official who worked on the investigations into Hillary Clinton's use of a private email server as secretary of state and any links between Russian interference in the 2016 election and the Trump campaign. Then-special counsel Robert Mueller had him removed from his investigation into Trump and Russia after he found out about the anti-Trump texts.

In his lawsuit, which also names Attorney General William Barr, the FBI and its director Christopher Wray, Strzok contends the text messages he traded with another FBI employee, Lisa Page, should never have been made public in the first place. It accuses the Justice Department of having violated the federal Privacy Act with the "deliberate and unlawful disclosure” to the media.

Strzok, a veteran of the FBI for more than two decades, exchanged the messages with Page, an FBI lawyer he was having an affair with, during the 2016 election cycle, including referring to the then-candidate as "an idiot" and "a disaster."

In another exchange, when Page asked for reassurance Trump wouldn't be elected, Strzok replied, "No he's not. We'll stop it."

That comment was seized on by some Republicans as proof of a "deep state" plot to stop Trump from being elected. In the suit, Strzok says it "was meant to reassure the FBI attorney in question that the American people were not likely to elect then-candidate Trump as President, who was trailing in the public polling at that time and had just attacked a Gold Star family."

Trump pointed to the text messages as proof that the Russia investigation was a "witch hunt," and repeatedly demanded publicly that Strzok be fired.

"The complete list of venomous tweets is far too long to recount here," Strzok's lawsuit says.

The FBI's Office of Professional Responsibility reviewed Strzok's conduct and recommended he be suspended and demoted for 60 days instead of being fired because of various mitigating factors, "including but not limited to your 21 years of FBI service, outstanding performance record, and numerous awards" and “the fact that you were assigned to two very stressful and high profile investigations during the time of your misconduct," according to the suit.

The FBI's deputy director, David Bowdich, overruled that decision the next day and terminated Strzok, while also denying him any chance to appeal, which Strzok says was in violation of FBI guidelines.

He was officially fired from the FBI last August — news which the president celebrated on Twitter.

Agent Peter Strzok was just fired from the FBI - finally. The list of bad players in the FBI & DOJ gets longer & longer. Based on the fact that Strzok was in charge of the Witch Hunt, will it be dropped? It is a total Hoax. No Collusion, No Obstruction - I just fight back! — Donald J. Trump (@realDonaldTrump) August 13, 2018

The lawsuit suggests that Trump's complaints about political speech are hypocritical.

"The Trump Administration has consistently tolerated and even encouraged partisan political speech by federal employees, as long as this speech praises President Trump and attacks his political adversaries," it says. "For example, President Trump rejected the recommendation of his own Office of Special Counsel that adviser Kellyanne Conway be removed from her job for repeatedly violating the Hatch Act by attacking former Vice President [Joe] Biden and publicly advocating for and against various U.S. Senate candidates."

"The retaliatory response to Special Agent Strzok’s protected political speech is consistent with a policy and practice adopted by this President and his administration to stifle dissenting speech by current and former federal employees, and to chill such speech in the future."

While Trump is not named as a defendant in the lawsuit, it charges that his actions to "publicly vilify" Strzok "contributed to the FBI’s ultimate decision to unlawfully terminate him, as well as to frequent incidents of public and online harassment and threats of violence to Strzok and his family that began when the texts were first disclosed to the media and continue to this day."

The suit demands his reinstatement, back pay and other damages.

The Justice Department declined comment to NBC News about the lawsuit.