The simple answer is that via the chaos in syria , the PKK ypg amongst the kurds (another fact turkey also has large Kurdish population) , who has long running confrontation with turkey as their enemy , was armed by USA among others to create a corridor in the north to become puppets for foreign agenda.
As those PKK and ypg members became renamed and reassigned territorially also,
They then became protected by USA and their agenda.
To turkey it is black and white an issue, not grey.
40,000 killed in decades of confrontations does not escape memory.
When turkey shot down a Russian fighter earlier in the Syrian conflict, turkey and Russia quickly again made amends.
I believe the reason to be that Russia shared intelligence with turkey which revealed USA's hand in the internal and closely external problems turkey was facing , as the coup attempt also ( or it could be said the attempt to divide the country , and the coup attempt was to consolidate power entirely, to erdogan).
The developments in Syria , were to create opportunities for division of Syria, and oil .
The corridor they tried to create, they had obviously planned, and turkey being the casualty of further chaos in the region.
Now erdogan wants to move the Kurds , 30 miles last I heard , to create a buffer zone, from the heavily armed enemies of theirs , the USA and allies have created.
Then he wishes to send the 3 million refugees turkey is hosting back into their own country.
USA and Europe(EU) has been dissapointing for turkey to say the least, and now turkey (erdogan) turns towards who has helped it (Russia) and is infuriated by what they tried to do .