import json


def annotation_to_GeckoJSON(annotation, distances={}, colors={}):
    """
    Parameters:
    -----------
    annotation: `pyannote.core.Annotation`
        proper pyannote annotation for speaker identification/diarization
    distances: `dict`, optional
        in-cluster distances between speech features
        see `get_distances_per_speaker`
    colors: `dict`, optional
        speaker id : consistent color

    Returns:
    --------
    gecko_json : a JSON `dict` based on the demo file of https://github.com/gong-io/gecko/blob/master/samples/demo.json
        should be written to a file using json.dump
    """

    gecko_json = json.loads("""{
      "schemaVersion" : "3.1",
      "monologues" : [  ]
    }""")
    for segment, track, label in annotation.itertracks(yield_label=True):
        distance = distances.get(label, {}).get(segment)
        color = colors.get(label)
        gecko_json["monologues"].append(
            {
                "speaker": {
                    "id": label,
                    "color": color,
                    "distance": distance,
                    "non_id": [],
                    "annotators": 0
                },
                "start": segment.start,
                "end": segment.end
            })
    return gecko_json