Theory and simulation of electronic and optical properties of charged defects in two-dimensional semiconductors