Image copyright Getty Images/Universal Image caption Ron Stallworth (left) is played by John David Washington in BlacKkKlansman (right)

Heard the one about the black cop who infiltrated the KKK? Read the true story behind BlacKkKlansman, the latest film from director Spike Lee.

In October 1978 Ron Stallworth, the first black detective in the history of the Colorado Springs Police Department, saw a classified advert in one of the city's daily newspapers.

The ad invited readers interested in receiving information from the Ku Klux Klan to write to a PO box located in a nearby town.

Stallworth, curiosity piqued, wrote a note to the PO box under his own name. In it he claimed to be a white man who was interested in learning more about the KKK's activities.

Two weeks later he received a phone call from the local organiser of the Klan's Colorado Springs chapter, asking him why he wished to join.

Media playback is unsupported on your device Media caption How Ron Stallworth became a card-carrying member of the KKK

From these speculative beginnings sprang an audacious undercover investigation, conducted first over the phone but eventually involving face-to-face contact.

It led to one of Stallworth's white colleagues impersonating him with such success he was invited to become leader of the Klan's local chapter.

It also led to the exposure of white supremacists in the military, the thwarting of numerous planned cross burnings and Stallworth becoming a card-carrying member of one of America's most detested organisations.

Most detested, yes. Brightest, no. At no point during Stallworth's investigation did his fellow Knights of the Ku Klux Klan suspect the man they were talking to over the phone and the man who was attending their meetings were not the same person.

Their favoured method of setting their kerosene-soaked wooden crucifixes alight, meanwhile, was literally inspired by something they'd seen in a movie.

Image copyright Getty Images/Universal Image caption David Duke, former Grand Wizard of the Ku Klux Klan, is played by Topher Grace

Outside his investigation, Stallworth found himself in the unusual position of providing personal protection to David Duke, former Grand Wizard of the KKK.

Throughout their somewhat chilly encounter, Duke had no notion the police detective on his security detail was the Ron Stallworth he'd been conversing with amicably over the telephone.

Duke, in fact, once bragged he could instantly identify the race of a person from how they spoke - a remark Stallworth said had him "cracking up laughing".

It sounds like a wacky prank, pulled off with elan and daring. Yet the sympathies and sentiments Stallworth was involved in exposing are no laughing matter.

A year ago this month, 32-year-old Heather Heyer was killed in Charlottesville, Virginia when a white nationalist driver ploughed his car into a crowd of people protesting against a far-right rally.

Footage of the incident is shown at the end of Spike Lee's film, proving the racism and bigotry expressed by Stallworth's unwitting dupes have not gone away.

Image copyright Universal Image caption Spike Lee with Adam Driver on the BlacKkKlansman set

"We had to connect the two eras," Lee tells the BBC. "To tell this story in the best way possible, it could not just be a period piece set in the 1970s.

"From the very beginning, we wanted to drop enough stuff into it that people would think 'hey, this is happening today'."

"It's so important Spike made this film now," says actress Laura Harrier, who plays a student activist, who embarks on a romance with the on-screen Stallworth.

"We are still battling racism and hatred all over the world and the footage at the end of the film clearly shows how timely it is."

Harrier's Patrice character is an Afro-sporting fiction who does not appear in the 2014 memoir on which Lee's film is based.

Neither does Flip Zimmerman, the film's version of the real-life detective who became Stallworth's stand-in on occasions when a white version of him was needed.

Adam Driver, who plays Flip in BlacKkKlansman, concedes there are parallels to be drawn between the business of acting and undercover detecting.

Image copyright Universal Image caption Laura Harrier plays Ron's girlfriend Patrice, a fictional character

"The difference is with one you're pretending the stakes are life and death, and with the other they actually are," he clarifies. "The stakes could not be higher."

The real Stallworth eventually left Colorado to become an investigator in Utah, where he became a recognised expert in the correlation between gangster rap music and street gang culture.

He retired in 2005, after which he wrote the memoir that saw him receive death threats from outraged white supremacists.

"I know that in spite of my varied career accomplishments, the one that will always excite and intrigue is the KKK investigation," he writes at the end of his book.

"It has defined me in ways unimaginable and has always fascinated those who hear its tale."

BlacKkKlansman is out in the UK on 24 August. BlacKkKlansman: Race, Hate, and the Undercover Investigation of a Lifetime by Ron Stallworth is published by Arrow Books.

Follow us on Facebook, on Twitter @BBCNewsEnts, or on Instagram at bbcnewsents. If you have a story suggestion email entertainment.news@bbc.co.uk.