import logging


def _check_phases_by_position(row, end_exon, allow_incomplete_cds):
    """
    Raise an error if the end phase don't make sense for the exon position.

    In particular, an exon end phase -1 is only allowed for the last exon.
    If end_exon is True, the exon has the last position in the transcript.
    If allow_incomplete_cds is False, an error is raised when the last exon
    finishes with an incomplete codon (end_phase 1 or 2).
    The pandas' DataFrame row is printed in the error message.
    """
    end_phase = row['EndPhase']
    transcript_id = row['TranscriptID']
    exon_id = row['ExonID']

    if end_exon and (not allow_incomplete_cds) and end_phase in {1, 2}:
        logging.warning('Exon end phase is 1 or 2 in last exon (%s, %s).',
                        transcript_id, exon_id)
        return False

    if (not end_exon) and end_phase == -1:
        logging.warning('Exon end phase -1 is not in the last exon (%s, %s).',
                        transcript_id, exon_id)
        return False

    return True