import pandas as pd
import numpy as np
from aeri_qc.all_checks import checklist
from aeri_qc.main import read_mirror

@checklist.add_check(depends=['hatchOpen','sceneMirrorPosition'], affects_calibration=False, description='hatch is closed')
def hatch_check(frame, parameters):
    """
    Check that the hatch is open on sky views
    """
    return  ((frame.hatchOpen != 1) & (~frame.sceneMirrorPosition.isin([ord('H'), ord('A')])))


@checklist.add_check(depends=['hatchOpen','sceneMirrorPosition'], affects_calibration=True, description='hatch moving during calibration view, mirror might have safed')
def safing_check(frame, parameters):
    """
    Check that the mirror doesn't safe during a calibration view and contaminate other records
    """
    hatch_closing = (frame.hatchOpen == -3)
    return hatch_closing & frame.sceneMirrorPosition.isin([ord('H'), ord('A')])



@checklist.add_check(depends=['sceneMirrorPosition'], affects_calibration=True, description='mirror position not in mirror.beg')
def encoder_check(frame, parameters):
    if 'mirror.beg' not in parameters:
        return frame.sceneMirrorPosition * 0
    mirror_beg = read_mirror(parameters['mirror.beg'])
    angles = { s[2] for s in mirror_beg}
    return frame.sceneMirrorPosition.round(2).isin(angles)