Not long after Senator John McCain learned last summer that he had terminal brain cancer, he began convening meetings every Friday in his Capitol Hill office with a group of trusted aides. The subject was his funeral.

He obsessed over the music, selecting the Irish ballad “Danny Boy” and several patriotic hymns. He choreographed the movement of his coffin from Arizona, his home state, to Washington. And in April, when he knew the end was coming, he began reaching out to Republicans, Democrats and even a Russian dissident with requests that they deliver eulogies and serve as pallbearers.

By the time he died on Saturday, Mr McCain had carefully stage-managed a four-day celebration of his life — but what was also an unmistakable rebuke to President Donald Trump and his agenda. For years, Mr Trump had used Twitter and the presidential bully pulpit to mock and condemn the senator from Arizona. In death, Mr McCain found a way to have the last word, even quietly making it clear through friends that Mr Trump was not welcome at the services.

“I think it’s fair to say that they have a very different view of this country and what this country means, here and abroad,” said Mark Salter, the senator’s long-time friend and co-author who sat with Mr McCain — often with a lump in his throat — during the many discussions about his looming death and how to mark it. “His overall message was: ‘It doesn’t have to be this shitty.’ ”

The series of events honouring Mr McCain are the kind of grandiose spectacle that is normally reserved for someone who became president, not someone who twice failed to do so. Friends said that Mr McCain was surprised by the level of interest in his death even as he planned it.

When advisers suggested that his coffin should lie in state at the Arizona state Capitol building, Mr McCain said he believed the legislature would never approve such a rare honour for him, recalled Rick Davis, who has been at Mr McCain’s side for decades and served as his 2008 campaign chairman.

“Every inch of the way, he underestimated what he thought this would be about,” Mr Davis said.

The week’s memorial events began in Arizona on Wednesday, when his body was taken to the Capitol building, and will continue on Thursday at a service at North Phoenix Baptist Church. The procession will then shift to the nation’s capital, when Mr McCain’s coffin will arrive at an air base outside Washington as the president is holding one of his raucous, campaign-style rallies for supporters in Indiana.

By the weekend, when virtually all of official Washington — Democrats and Republicans alike — gathers at the National Cathedral for a nationally televised farewell, Mr Trump is expected to have retreated to Camp David, where White House aides hope he will contain his anger at the attention being lavished on Mr McCain.

John McCain: US Senator in pictures Show all 40 1 /40 John McCain: US Senator in pictures John McCain: US Senator in pictures John McCain - 2000 John McCain waves to crowds at The University of Portland in Oregon. McCain was campaigning for Republican nominee George W. Bush AFP/Getty John McCain: US Senator in pictures John McCain - 1967 Senator John Mccain In A Hanoi Hospital During The Vietnam War November, 1967 Getty John McCain: US Senator in pictures John McCain - 1993 John McCain, a former POW, looks at a display of personal belongings of American POWs at the joint POW/MIA archives center in the Hanoi Army Museum AFP/Getty John McCain: US Senator in pictures John McCain - 1999 Republican Senator John McCain announces his official bid for the 2000 White House in Nashua, New Hampshire. Evoking five years as a prisoner of war in Vietnam, McCain highlighted his own sacrifice and his 17-year record of public service in the US Congress AFP/Getty John McCain: US Senator in pictures John McCain - 1999 Republican presidential candidate John McCain poses with an A-4 jet -- similar to the one which he was piloting when he was shot down during the Vietnam War -- on the deck of the USS Intrepid aircraft carrier in New York. McCain, who flew off of the Intrepid during the Vietnam War, but was not on it when he was shot down, was at the Intrepid to receive the 1999 Intrepid Freedom Award AFP/Getty John McCain: US Senator in pictures John McCain - 2000 Republican presidential hopeful John McCain answers questions at a news conference in New York AFP/Getty John McCain: US Senator in pictures John McCain - 2000 US Arizona Senator and Republican Presidential hopeful John McCain smiles as he speaks on the phone during a radio show interview from his hotel room in Los Angeles AFP/Getty John McCain: US Senator in pictures John McCain - 2000 US Arizona Senator and Republican presidential hopeful John McCain reacts to cheers from the crowd after speaking in a rally at the Santa Clara University on the eve of the multi-state SuperTuesday primaries AFP/Getty John McCain: US Senator in pictures John McCain - 2000 US Senator John McCain (R-AZ) smiles as he is interviewed on WGIR-AM radio in Manchester as he campaigns to become the Republican candidate for president of the United States AFP/Getty John McCain: US Senator in pictures John McCain - 2002 John McCain on "The Tonight Show with Jay Leno" Getty John McCain: US Senator in pictures John McCain - 2004 Senator John McCain speaks at the Victor Awards in Las Vegas Getty John McCain: US Senator in pictures John McCain - 2008 US Senators Joe Lieberman, Lindsey Graham, John McCain and former senator Phil Gramm watch the Super Bowl at the Hyatt Regency Boston. McCain was leading in most polls over rival contender former Massachusetts Gov. Mitt Romney before the biggest "Super Tuesday" in American history Getty John McCain: US Senator in pictures John McCain - 2009 John McCain shakes hands with Dorothy Cross, great niece of the first African American heavyweight boxing champion John Arthur "Jack" Johnson. McCain and Peter King introduced a resolution calling on President Barack Obama to posthumously pardon Johnson, who was a victim of what is widely regarded as a racially motivated conviction in 1913 for violating a law prohibiting taking women across state lines for "immoral purposes" Getty John McCain: US Senator in pictures John McCain - 2009 John McCain delivers remarks at the John Hopkins University Paul H. Nitze School of Advanced International Studies in Washington, DC. McCain spoke on "Why Freedom Still Matters" and "Human Rights and Foreign Policy." Getty John McCain: US Senator in pictures John McCain - 2010 John McCain with veteran Richard Philippi at VFW Post #1677 during a campaign stop in Casa Grande, Arizona Getty John McCain: US Senator in pictures John McCain - 2010 Cindy and John McCain during the Western Conference finals of the NBA Playoffs between the Los Angeles Lakers and the Phoenix Suns at US Airways Center in Arizona. The Lakers defeated the Suns 111-103 Getty John McCain: US Senator in pictures John McCain - 2010 John McCain speaks to a group of supporters alongside his wife Cindy at his victory party after winning Arizona's primary election in 2010 Getty John McCain: US Senator in pictures John McCain - 2010 Senate Armed Services Committee ranking member US Sen. John McCain stands in an elevator on Capitol Hill. The US Senate is debating a the New START, Strategic Arms Reduction Treaty, and is expected to vote on it before the end of the 111th Congress's lame duck session Getty John McCain: US Senator in pictures John McCain - 2010 U2 frontman Bono and John McCain talk during the 2010 Atlantic Council awards dinner at the Ritz Carlton Hotel in Washington, DC Getty John McCain: US Senator in pictures John McCain - 2011 US Defense Secretary Leon Panetta greets ranking member John McCain before a hearing of the Senate Armed Services Committee Panetta and Chairman of the Joint Chiefs of Staff Adm. Michael Mullen testified before the committee on US strategy in Afghanistan and Iraq Getty John McCain: US Senator in pictures John McCain - 2011 US President Barack Obama speaks prior to signing the John M. Roll United States Courthouse Bill into law in the Oval Office of the White House as Homeland Security Secretary and former Arizona Governor Janet Napolitano, Senator John McCain, Chief of Staff to Rep. Gabrielle Giffords Pia Carusone, Rep. Ed Pastor, and Rep. Trent Franks look on. President Obama signed the bill to name a new federal courthouse in Yuma, Arizona, after federal Judge John M. Roll, who was gunned down during the shooting rampage in Tucson, Arizona Getty John McCain: US Senator in pictures John McCain - 2012 Republican presidential candidate and former Massachusetts Governor Mitt Romney talks with former presidential nominee US Sen. John McCain on Romney's campaign bus with Press Secretary Andrea Saul in between events near Manchester, New Hampshire. McCain announced his endorsement of Romney the day after Romney beat former US Senator Rick Santorum by only eight votes in "first in the nation" Iowa Caucuses Getty John McCain: US Senator in pictures John McCain - 2012 John McCain rides on the Senate subway after participating in a news conference on Capitol Hill. McCain joined Senate Foreign Relations Chairman John Kerry as he urged fellow Senators to approve the "Convention on the Rights of Persons with Disabilities bill, an international agreement for protecting the rights of individuals with disabilities Getty John McCain: US Senator in pictures John McCain - 2012 John McCain speaks to members of the media after a hearing on the Benghazi attack before the Select Committee on Intelligence in Washington, DC. Former Central Intelligence Agency (CIA) Director David Petraeus testified before the committee about the September 11 attacks on the American diplomatic compound in Benghazi, Libya, that killed Ambassador Christopher Stevens and three other Americans Getty John McCain: US Senator in pictures John McCain - 2012 John McCain turns to greet Ileana Ros-Lehtinen and Mario Diaz-Balart during a campaign visit to support Republican presidential candidate, former Massachusetts Gov. Mitt Romney at the Bay of Pigs Museum and Library of the 2506 Assault Brigade in the Little Havana neighborhood in Miami, Florida. McCain took the time to rally people behind Romney and to encourage them to go out and vote Getty John McCain: US Senator in pictures John McCain - 2012 John McCain speaks during an interview in his Russell Senate Office Building office on Capitol Hill. McCain said he wanted a solid NATO-Afghanistan accord sealed at a summit, ensuring critical post-2014 US involvement in the region rather than "looming international abandonment." AFP/Getty John McCain: US Senator in pictures John McCain - 2013 US Secretary of State Hillary Clinton and Sen. John McCain joke and embrace before Sen. John Kerry's confirmation hearing before the Senate Foreign Relations Committee to become the next Secretary of State in the Hart Senate Office Building on Capitol Hill Getty John McCain: US Senator in pictures John McCain - 2013 John McCain testifies before a Senate Commerce, Science and Transportation Committee hearing on the state of video, on Capitol Hill in Washington, DC. The committee discussed unbundling cable services and the impact of changing technology, among other topics Getty John McCain: US Senator in pictures John McCain - 2013 John McCain, Meghan McCain and Jimmy McCain attend "Raising McCain" Series New York Premiere at Tribeca Cinemas in New York Getty John McCain: US Senator in pictures John McCain - 2013 Prince Harry is accompanied by US Senator John McCain as he tours a Senate photo exhibit on landmines and unexploded ordnances in the Rotunda of Russell Senate Office Building in Washington, DC. Prince Harry was in the US on a week long visit AFP/Getty John McCain: US Senator in pictures John McCain - 2013 US Senator John McCain speaks during a mass rally of the opposition in Independence Square in Kiev. McCain told more than 200,000 EU supporters rallying in Kiev that Washington backed their desire for European integration. "To all Ukraine, America stands with you," he called out to the cheering crowd. "The free world is with you, America is with you, I am with you... Ukraine will make Europe better and Europe will make Ukraine better." AFP/Getty John McCain: US Senator in pictures John McCain - 2013 US Senators Lindsey Graham and John McCain meet with Israeli Prime Minister Benjamin Netanyahu in Jerusalem, Israel GPO/Getty John McCain: US Senator in pictures John McCain - 2014 John McCain hugs Debbie Valle after speaking at a forum at the Burton Barr Central Library in Phoenix, Arizona. McCain met with Arizona Veterans to discuss mismanagement of Veterans Affairs Getty John McCain: US Senator in pictures John McCain - 2014 John McCain and wife Cindy attend a MLB game between the Arizona Diamondbacks and the Los Angeles Dodgers at Chase Field in Phoenix, Arizona. Getty John McCain: US Senator in pictures PHOENIX, AZ - NOVEMBER 08: Sen. John McCain (R-AZ), center, and his wife Cindy (R) greet supporters after casting their vote at the Mountain View Christian Church polling place on November 8, 2016 in Phoenix, Arizona. Throughout the country, millions of Americans are casting their votes today for either Hillary Clinton or Donald Trump to become the 45th president of the United States. (Photo by Ralph Freso/Getty Images) Ralph Freso Getty Images John McCain: US Senator in pictures John McCain - 2015 WASHINGTON, DC - MAY 05: Sen. John McCain (L) (R-AZ) and Sir Elton John, Founder, Elton John AIDS Foundation, pose for a photo before an Elton John AIDS Foundation and The ONE Campaign hosted reception on global HIV/AIDS funding at the U.S. Capitol Visitor Center on May 5, 2015 in Washington, DC. (Photo by Paul Morigi/Getty Images for EJAF) Paul Morigi Getty/EJAF John McCain: US Senator in pictures John McCain - 2017 Senator Amy Klobuchar takes a selfie with Senator Chris Van Hollen, Senator John McCain and Senator Bernie Sanders on the West Front of the US Capitol in Washington, DC at Donal Trump's inauguration ceremony. He became the 45th president of the United States Getty John McCain: US Senator in pictures John McCain - 2017 Sen. John McCain gives a thumbs up to well wishers as he gets into his car at the US Capitol. McCain was recently diagnosed with brain cancer but returned on the day the Senate is holding a key procedural vote on US President Donald Trump's effort to repeal and replace the Affordable Care Act Getty John McCain: US Senator in pictures John McCain - 2017 US Army Chief of Staff Gen. Mark A. Milley greets Sen. John McCain during a special Twilight Tattoo performance at Fort Myer in Arlington, Virginia. McCain was honored with the Outstanding Civilian Service Medal for over 63 years of dedicated service to the nation and the US Navy Getty John McCain: US Senator in pictures John McCain - 2017 John McCain arrives at Capitol Hill. Donald Trump's tax reform plan has overcome pockets of resistance within Republican ranks, US senators said, setting up a vote that could provide the president with his first major legislative victory."We have the votes," Senate Majority Leader Mitch McConnell told reporters as he entered the chamber AFP/Getty

Mr McCain’s closest friends insisted this week that the senator did not harbour a personal grudge towards the president, even at the end. They described him as mostly interested in promoting the cause of bipartisanship and compromise that the “maverick” lawmaker had carefully fashioned into one of the most durable political brands in the last half-century.

“He wanted to reinforce his message that there is more that unites us than separates us,” said Steve Duprey, a businessman from New Hampshire.

But they also acknowledged what has been plain to just about everyone since the two men repeatedly clashed, sometimes in the most personal ways, in recent years: Mr McCain had little respect for the president.

As such, it was perhaps inevitable, they conceded, that a celebration of Mr McCain’s worldview would be viewed as a critique of the president’s.

“Trump has been a catalyst for him to speak more strongly and more vigorously about the need for those things that Trump doesn’t do,” said John Lehman Jr, who served as secretary of the Navy under President Ronald Reagan.

In the spring, Mr McCain began the uncomfortable task of asking people to speak for him after he died. In April, he approached former Presidents Barack Obama and George W Bush, a bipartisan request to the men who defeated him in his quests for the White House.

But prominent politicians will not be the only participants. Pallbearers include friends Mr McCain made over the years, including actor Warren Beatty and Frederick Smith, the founder of FedEx. Larry Fitzgerald, the wide receiver who played for Mr McCain’s beloved Arizona Cardinals was asked to speak at Thursday’s memorial service in Phoenix.

Some received messages by phone, and others were asked in person. Former Vice President Joe Biden was among those summoned to Sedona, when Mr McCain began executing his funeral plans with a newfound urgency.

They spoke for hours before Mr McCain asked Mr Biden to deliver a eulogy at his funeral in Arizona. Mr Biden immediately accepted, said someone close to the vice president, and he will also serve as a pallbearer on Saturday in Washington. Jim Mattis, Mr Trump’s secretary of defence, said that he was among those who had been asked months ago to be a pallbearer at the final event, a private service that will be held on Sunday at the US Naval Academy in Annapolis, where Mr McCain will be buried.

Mr McCain’s life in politics was built around his reputation for that kind of trademark bluntness. During the 2000 and 2008 presidential campaigns, he spent hours talking with reporters — on the record — sitting on couches in the back of a bus that he strategically named the “Straight Talk Express.”

In death, some of his messages were equally direct. On Monday, Mr Davis read Mr McCain’s final, pull-no-punches remarks to reporters. It escaped no one that Mr McCain was talking about the current president.

“We weaken our greatness when we confuse our patriotism with tribal rivalries that have sown resentment and hatred and violence in all the corners of the globe,” Mr McCain wrote. “We weaken it when we hide behind walls, rather than tear them down, when we doubt the power of our ideals, rather than trust them to be the great force for change they have always been.”

Other messages during the week have been less explicit. But Mr Trump clearly took offence anyway. After Mr McCain died, the president refused for two days to issue a formal statement praising his service to the nation. And he only ordered the White House flag to fly at half-staff for the week after pressure from his staff, Republican lawmakers and veterans groups.