from datetime import datetime, timezone

from acspype import ACSDev
from acspype.packet import parse_packet, calibrate_packet, ts_correct_packet
from acspype import ACSPacket
from acspype.qaqc import FLAG, syntax_test

TEST_TIME = datetime.now(timezone.utc)

# TEST_PACKET is from ACS-00011 sampling in air.
TEST_PACKET = (b'\xff\x00\xff\x00\x02\xc0\x05\x01S\x00\x00\x0b\x01\xd0\x00\x00"\xadrc\xadA\x01\xe0\x02\xcc\x00H\x80'
               b'\xc3\x01T\x02\r\x01\x93\x01\xf4\x01\xc3\x02U\x01\xd1\x02<\x02\x07\x02\xa9\x02\x19\x02\x87\x02[\x03'
               b'\x0b\x02q\x02\xeb\x02\xbf\x03y\x02\xd6\x03[\x037\x03\xf7\x03C\x03\xdc\x03\xb9\x04s\x03\xb9\x04Y\x04G'
               b'\x05\n\x04F\x04\xf2\x04\xf3\x05\xb1\x04\xe1\x05\x9e\x05\xb4\x06n\x05\x98\x06`\x06\x99\x07G\x06Y\x07E'
               b'\x07\x8e\x08\x11\x07\x10\x08\x1f\x08z\x08\xe2\x07\xd7\x08\xf5\t}\t\xc4\x08\xb1\t\xe7\n\xa0\n\xc4\t\xa9'
               b'\n\xf1\x0b\xea\x0b\xce\n\xa3\x0c\x15\r:\x0c\xea\x0b\xb9\r;\x0e\xb7\x0e\x1e\x0c\xd4\x0e\x8f\x10=\x0f['
               b'\x0e\r\x0f\xd8\x11\xf2\x10\xd0\x0fu\x11j\x13\xeb\x12`\x10\xec\x13\x1b\x15\xfc\x13\xe8\x12\\\x14\xcc'
               b'\x18\x0e\x15\x8b\x14\x00\x16\x84\x1ao\x17E\x15\x92\x18y\x1c\xbb\x18\xee\x17+\x1aH\x1f\x19\x1a\xa9'
               b'\x18\xcd\x1c-!\x86\x1c?\x1aI\x1d\xf9#\xc8\x1d\xe7\x1b\xfe\x1f\xb8&b\x1f\xd1\x1d\xe0!\xd9)A!\xab\x1f'
               b'\xa2#\xf5+\xf6#f!e%\xd8.\xbb%b#o\'\xfc1\xeb\'~%\x8d*^5;)\xde(\x1c,\xdf98,\x9a*\xb6/\xfe=M/\n-$2'
               b'\xb6A01\x8f/\xa75|E23\xd11\xea8\x0eH\xde604v:\x9fM\x068\xa96\xdf=}P\xf3:\xa28\xc3?\xc3T+<\x99:\xebA'
               b'\xddW\xcf>\xa8<\xf3DF[9@\x98?\x12FY^\xe0C\x16A\xa4I\x1ec6E"CQK\xbef\'J\xb5H\x1dQ\x92m\xbbL~I\xf4S'
               b'\x92q\x10N.K\xb7U\x96tLO\x85M(W\x18w\x0bP\xd5N\x8eX\xa0y\xbcQ\xf8O\xb8Y\xf9|\x18S*Q\x07[>~\xaaTQRF'
               b'\\\xcc\x81%T\xacR\xba]P\x82kT\x9fR\xd7]K\x83\x1fT\x7fR\xca]D\x83\x97T+R\x85\\\xfa\x83\xbeS\xa1R\x04'
               b'\\x\x83|R\xe2QU[\xc2\x82\xe9Q\x81O\xfdZv\x81FO\x96N\x11XZ~\xb1MpK\xfdV!{\xcbJ\xe9I\xa2SCxrI\x8cHjQ'
               b'\x94v\xdfH\xa5GrQ\x07u\x9cFPE\x16N\x8er\x1dC\xceB\x9dK\xa5naA[@-I\x07j\xac>\x90=mE\xeefi;\xef:\xd9B'
               b'\xf3b\\9\\8G@+^I6\x825b=\x17Y\xa83D2\x199\x83T\\0\x02.\xe75\xc6O&-T,;2\xc3J\xd0*\xde)\xba0\x1cF'
               b'\xb7()&\xfc-\x1cB5%r$D*\x03=\xb6"\xe7!\xb3\'19x \\\x1f2$\\5E\x1e\x18\x1c\xf1!\xc81\x82\x1c\x07\x1a'
               b'\xd8\x1f\x8b.\x03\x19\xe8\x18\xb6\x1d\x1b*r\xf8\xad\x00')

TEST_TEMPERATURE = 12.345  # Dummy temperature.
TEST_SALINITY = 34.567  # Dummy salinity.


def test():
    dev = ACSDev('../dev_tools/test_files/ACS-00011_2022-10-20.dev')

    acs_packet = ACSPacket(daq_time=TEST_TIME, full_packet=TEST_PACKET)
    assert syntax_test(acs_packet.full_packet) == FLAG.OK

    parsed_packet = parse_packet(acs_packet)

    calibrated_packet = calibrate_packet(parsed_packet, dev)
    assert dev.serial_number == calibrated_packet.serial_number

    ts_corrected_packet = ts_correct_packet(calibrated_packet, TEST_TEMPERATURE, TEST_SALINITY, dev)

    assert ts_corrected_packet.temperature == TEST_TEMPERATURE
