How to repair water damage on a wall
The space between the window and wall I'd just stuff with some fiberglass insulation. I wouldn't be using the expanding foam because it might put a strain on the window and impede operation. Then straighten out that drywall edge and add a new piece down to the window and tape with mud. If the space is small enough, cover with some trim or molding. The latter will look weird if the other windows don't have the same molding.