TO Donald Trump, it was not of the slightest importance that John McCain, when a US naval pilot, had been captured during the Vietnam War and been tortured and beaten during nearly six years as a prisoner in Hanoi.

"He’s not a war hero," Trump told an audience in 2015. "He’s a war hero because he was captured. I like people who weren’t captured.”

Much more recently, Kelly Sadler, an aide in the Trump White House, said McCain's opposition to the President's nomination for CIA director was immaterial, because he was “dying anyway”. Even some fellow staffers were shocked by the assertion. Sadler no longer works at the White House.

McCain was revealed last July as having been diagnosed with brain cancer, but the 81-year-old Arizona Republican senator has refused to bow out quietly. As he writes in his just-published memoir, The Restless Wave, he has always loved a fight, and he has long been taking the fight to Trump.

McCain has never been shy of attacking President Trump. The decision to separate migrant families from their children at the US-Mexico border, McCain told his 3.39 million Twitter followers, was "an affront to the decency of the American people, and contrary to principles and values upon which our nation was founded". Trump caved in to widespread pressure last week and rescinded the policy.

There is much more of this in The Restless Wave. Trump has an "appalling" way of speaking about refugees; his dismissal of "fake news" is copied by autocrats keen to clamp down on a free press; he has showered praise on some of the world's worst tyrants; he threatened to deliberately kill terrorists' wives and children - "an atrocity"; he has hardly ever addressed the issue of human rights.

Noting that Trump seemed to mock the idea that America should promote its values abroad, McCain writes: "I don't know if that is sincerely his view or if he believes that the global progress of democracy and the rule of law should be only a distant, notional goal of American statecraft."

His views are reflected in large measure by his daughter, Meghan, co-host of the popular TV chat show The View, and a political analyst for the ABC network. Trump's constant bullying of her father "never stops being gross", she said earlier this week.

Earlier this year she was attacked on the right-wing Breitbart website for declaring that "young Republicans" would not tolerate the anti-LGBTQ "crap" of US evangelical groups. And as a "socially liberal Republican" she has often denounced extremism in her own party.

John McCain, the son and grandson of US Navy admirals, joined the Naval Academy in 1954. He was shot down and badly injured in 1967 over Vietnam but while in the notorious "Hanoi Hilton" he refused to be repatriated ahead of other US prisoners who had been in North Vietnamese captivity longer than he had been. He rejected the offer of unconditional release and paid an awful price for his defiance.

"The abuse, combined with the after-effects of his injuries, left him physically marked," said the New Yorker in 2007. "He could have avoided it all, but out of loyalty and – one has to name it – love for his comrades, he chose not to."

McCain alludes in his new book to his ill-treatment, in a chapter denouncing the CIA's "enhanced interrogation techniques" that were inflicted on detainees in the wake of the 9/11 attacks.

Arizona elected him to the House of Representatives in 1982 and the Senate four years later. In the presidential election of 2008, with Sarah Palin as his running mate, he was, however, soundly defeated by Barack Obama. In his book and elsewhere, he openly admires Obama's qualities but is by no means uncritical of his record in office.

McCain always objected that Obamacare - the Affordable Care Act - had been "rammed through Congress by Democrats on a strict party-line basis without a single Republican vote" and believed that the "mistakes of the past" should not be repeated. Last July, mistrusting the secretive process behind his own party's "skinny repeal" of Obamacare, he gave it a "thumbs down" at 1.29am when it came to the vote, on what the Washington Post described as "the most dramatic night in the ... Senate in recent history". The repeal was defeated but McCain's principled defiance infuriated Trump.

His memoir makes a strong case for democratic internationalism and bipartisanship, and with pride he describes himself as a "champion of compromise" in the governance of a nation "of 325 million opinionated, quarrelsome, vociferous souls. There is no other way to govern an open society ... Principled compromises aren't unicorns."

It was, he says, a genuine privilege to have run for President in 2008. "I fought for it as hard as I knew how, and I lost. But who could resent any disappointment who had had such an opportunity?" That same belief shapes his attitude towards his entire life and career.

In his book, which ends with Robert Louis Stevenson's poem, Requiem, McCain says he would rather give thanks for his long and blessed life than curse his illness. "The bell tolls for me. I knew it would," he writes, and he says it is his hope that people, regardless of whether or not they will mourn his passing, "will celebrate as I celebrate a happy life lived in imperfect service to a country made of ideals, whose continued success is the hope of the world.