The year 2016 was particularly bad for Erdogan. To start with there was a coup attempt against him. Further, the Turkish army couldn't make much headway in Syria. This was followed by the assassination of the Russian ambassador to Syria in Ankara, and now, a gunman entered an exclusive night club in Istanbul and shot dead 39 revelers, and escaped. These acts show the impotence of the Turkish state in dealing with ISIS and the Kurds, who are demanding a separate state.
Erdogan and dreams
Erdogan had dreams of past grandeur when the Ottoman Empire flourished, but the present state of Turkey is pathetic.
They started by shooting a Russian jet over Syria, but soon buckled as Putin enforced sanctions. They blamed the CIA and exiled preacher Gulen for the coup attempt, and asked for his extradition from the USA. But in the absence of reliable evidence, their demand didn't carry any weight in Washington.
Erdogan finds himself at a crossroads. The battle against ISIS and Kurds is going nowhere. Terror attacks are now a daily occurrence. In such a situation Erdogan had no choice but to agree to a cease-fire with Russia in Syria. With this cease-fire, he can have Russia as an ally against ISIS (which has been excluded from the ceasefire).
Battling ISIS
It appears that Turkey has also scaled down its demand for the removal of Assad.
The only positive for Russia and Turkey is the exclusion of the Obama administration from the Syrian deal. They have, however, left the door open for Trump's involvement at a later date. Erdogan and Putin were very unhappy with Obama.
The Turks are hoping that along with the Russians, they will be able to control ISIS. The Islamic State is a devious enemy and not easily defeated, as evidenced by the trouble the Iraqi army is experiencing in capturing Mosul, despite air support from the United States.
Future
At a later stage, Turkey will have to negotiate an agreement with the Kurds as desired by the US. In the meantime, Erdogan has no choice but to face and battle ISIS. It will also have to face terror attacks like the latest in Istanbul.