The situation in Hong Kong is a nightmare for Xi Jinping. China’s president has made the restoration of his country’s power and dignity the central theme of his presidency. But part of China’s sovereign territory has descended into violent anarchy.

Universities have turned into battlegrounds. Protesters are hurling Molotov cocktails at the police, but they appear to retain a strong measure of support from the population. Chinese troops have appeared on the streets - but so far only to help clear the roads. Deploying them against the demonstrators could plunge Hong Kong into a long-term insurrection, similar to Belfast in the 1970s or Algiers in the 1950s.

Mr Xi could plausibly argue that the immediate crisis is not his fault. The spark for the first demonstrations in June was the introduction of a bill allowing extradition of criminal suspects from Hong Kong to mainland China. By most accounts that was an idea pushed by Carrie Lam, Hong Kong’s chief executive. When Beijing saw the depth of the opposition, it tried to react sensibly by suspending the bill. But, by then, the protest movement had broadened its aims and gathered an irresistible momentum.

Mr Xi bears a broader responsibility. In the seven years since he came to power, the Chinese state has become significantly more authoritarian, preparing the ground in Hong Kong for a backlash against rule from Beijing.

An anti-corruption drive has seen prominent figures disappear from public life on the mainland and a rash of suicides among Communist party officials. More than a million people have been interned in re-education camps in the province of Xinjiang. The treatment of Xinjiang is often cited by demonstrators in Hong Kong as a sign of how far Beijing will go to crush cultural and regional diversity.

The increasingly Kafkaesque legal system of mainland China stands in stark contrast with Hong Kong’s own tradition of the rule of law. But during the Xi period, the mainland’s intolerance for free speech and thuggish attitude towards the law has seeped into Hong Kong itself. The case of some Hong Kong booksellers who were kidnapped - then detained on the mainland - sent a chilling message, so did the decision to ban elected lawmakers from the Hong Kong assembly, for mangling loyalty oaths to China.

Prominent anti-Beijing political activists such as Joshua Wong and Edward Leung were imprisoned. Mr Wong is now out of jail, while the still-imprisoned Mr Leung, finds his slogan - “Free Hong Kong, revolution now” - chanted on the streets.

There were always tensions inherent in the uneasy formula of “one country, two systems”. In 2003, there were big demonstrations against a proposed national security law for Hong Kong, pushed by Beijing. But in the 15 years between the handover of Hong Kong from Britain to China in 1997 and the accession to power of Mr Xi in 2012 those tensions proved manageable. Hong Kong citizens could reasonably hope that mainland China might evolve into a more liberal and law-governed society in the decades running up to the full integration of Hong Kong with China scheduled for 2047.

But during the Xi years, China has gone backwards politically. Maoist-era slogans have been revived and “Xi Jinping Thought” has been written into the Chinese constitution. Free speech has been further restricted; civil rights lawyers have been locked up and non-governmental organisations have been closed down.

It is hardly surprising if Hong Kong now regards the prospect of full integration with the mainland with horror. And that date no longer seems impossibly far-off. The most radical demonstrators are often in their teens or early twenties. They will be in the prime of their lives when the second handover takes place in 2047. So their assertions that they are fighting for their freedom cannot be dismissed as hyperbole - even if their tactics can be challenged.

The current revolt raises questions not just about Mr Xi’s handling of Hong Kong, but about his entire project. The president’s mantra is “the great rejuvenation of the Chinese people” and central to that is the restoration of national territorial integrity. After Hong Kong, the next step is meant to be Taiwan.

The Chinese government has repeatedly threatened to invade Taiwan if the self-governing island were ever to declare formal independence. However, if Beijing cannot control the streets of Hong Kong, the idea that mainland China Hong Kong is Xi Jinping’s failureJust as disturbingly for Mr Xi’s vision, the rebellion in Hong Kong undermines a central tenet of the patriotic education pushed by the Communist party: namely that there is “one China” and that all Chinese people long for nothing more than to be united. It is now clear that millions of Hong Kongers do not feel that ethnic solidarity overrides their political concerns about mainland China. On the contrary, they are increasingly asserting a separate Hong Kong identity, that is often tinged with prejudice against mainlanders.

Watching events unfold in Hong Kong inspires a fear of impending tragedy. But finding a peaceful way out would require Mr Xi to display a humility, open-mindedness and tolerance for opposing points of view that seem completely alien to him and the system that he has created.

Gideon Rachman is a columnist with the Financial Times