State prosecutor Robert Owen said an incident between Mr Ihalahewa and another colleague made him feel “embarrassed and uncomfortable”.
“After this event, he started to experience hallucinations,” he said.
He was eventually admitted to Sir Charles Gairdner Hospital where he repeatedly stated “I’m not gay” before eventually being diagnosed with “being in situational crisis in the context of culturally appropriate concerns”.
In the days leading up to his admission, he repeatedly told his brother police were coming after him to arrest him. He was diagnosed with depression with psychotic features.
After moving to Perth with Ms Withana three years later, Mr Ihalahewa, a cleaner, began using the dating app Grindr to meet up with men for casual sex and became worried his wife knew of his infidelity.
“There was an occasion when they were watching a movie that he told her he liked one of the male actors and she said, ‘Do whatever you want’,” Mr Owen said.
On the morning of the murder, Mr Ihalahewa claimed he had not slept for days and left his night shift at the Mercure Hotel early because he “felt like I am not in a safe place in my mind”.
He had stopped taking his medication and was sleeping in a separate room to his wife.
When he arrived home around 5.50am he claimed his wife offered to give him a head massage as he lay watching TV on the couch.
He said he declined and an argument broke out.
He later told police: “I feel, I saw like she was trying to kill me, she’s trying to take me somewhere, back to Sri Lanka … I saw like the devil coming in me … and I got the knife.”
“Noises enough, everything enough … I wanted to kill her.”
Mr Ihalahewa attacked his wife from behind, stabbing her in the neck and chasing her into other areas of the house as she tried to fend him off.
The former teacher, who worked as a part-time UberEats driver in Perth, was stabbed six times and declared dead at 9.20am after Mr Ihalahewa called triple-zero to report the death nearly two hours after the attack.
He told the dispatcher: “My wife and me had an argument and she died.”
A neighbour of the couple, Tristan Young, texted a friend at 7.06am that morning, saying: “OMG the woman next door just sounded like she was getting murdered and now it’s deadly silent.”
In a statement read out during Mr Ihalahewa’s murder trial, Mr Young said Mr Ihalahewa had told him previously he was homosexual, and noticed men “coming and going from the address at all hours” when his wife was away.
“To me [Ms Withana] seemed miserable and sad, I felt sorry for her and felt mad at Pen for putting her through it,” he said.
Mr Ihalahewa’s brother said he was unaware of any issues with his brother and Ms Withana’s relationship, and only became aware he had relapsed the day before the death.
“If I had [noticed any troubles] I would have taken quick action because of his mental illness,” he said.
Mr Ihalahewa has pleaded not guilty to murdering his wife due to unsoundness of mind.
His trial, which began on Monday in the WA Supreme Court, is being heard by a judge alone.
The trial continues.
Heather McNeill is a senior journalist at WAtoday.