import json


def intrinsics(kinect, path='calibrate/IR/intrinsics_retrieved_from_kinect_mapper.json', write=False):
    """
    Retrieve the depth camera intrinsics from the kinect's mapper
    and write them at: calibrate/IR/intrinsics_retrieved_from_kinect_mapper.json
    :param kinect: kinect instance
    :param path: path to save the intrinsics as a json file
    :param write: save or not save the intrinsics
    :return: returns the intrinsics matrix
    """
    intrinsics_matrix = kinect._mapper.GetDepthCameraIntrinsics()
    if write:
        with open(path, 'w', encoding='utf-8') as json_file:
            configs = {"FocalLengthX": intrinsics_matrix.FocalLengthX, "FocalLengthY": intrinsics_matrix.FocalLengthY,
                       "PrincipalPointX": intrinsics_matrix.PrincipalPointX, "PrincipalPointY": intrinsics_matrix.PrincipalPointY,
                       "RadialDistortionFourthOrder": intrinsics_matrix.RadialDistortionFourthOrder, "RadialDistortionSecondOrder": intrinsics_matrix.RadialDistortionSecondOrder,
                       "RadialDistortionSixthOrder": intrinsics_matrix.RadialDistortionSixthOrder}
            json.dump(configs, json_file, separators=(',', ':'), sort_keys=True, indent=4)
    return intrinsics_matrix