Understanding the Local Structure, Magnetism, and Optical Properties in Layered Compounds with d9 Ions: Insight into Silver Fluorides and K2CuF4