Is there a solution to insomnia?

I have terrible nightmares at night and I'm afraid to sleep, so I take melatonin pills but they don't work and I can't sleep. I sleep in the mornings and stay awake until noon.
Is there a solution to insomnia?
Post Opinion