import asterix
import unittest


class AsterixParseTest(unittest.TestCase):
    def test_ParseCAT048(self):
        sample_filename = asterix.get_sample_file('cat048.raw')
        with open(sample_filename, "rb") as f:
            data = f.read()
            packet = asterix.parse(data)
            self.maxDiff = None
            self.assertIsNotNone(packet)
            self.assertIsNotNone(packet[0])
            self.assertIs(len(packet), 1)
            self.assertTrue('I220' in packet[0])
            self.assertEqual(packet[0]['category'], 48)
            self.assertEqual(packet[0]['len'], 45)
            self.assertEqual(packet[0]['crc'], 'C150ED0E')
            self.assertTrue('ts' in packet[0])
            self.assertEqual(packet[0]['I220']['ACAddr']['val'], '3C660C')
            self.assertEqual(packet[0]['I220']['ACAddr']['desc'], 'AircraftAddress')
            self.assertEqual(packet[0]['I010'], {'SAC': {'desc': 'System Area Code', 'val': 25},
                                                 'SIC': {'desc': 'System Identification Code', 'val': 201}})
            self.assertEqual(packet[0]['I170'], {'GHO': {'desc': 'GHO', 'val': 0, 'meaning': 'True target track'},
                                                 'TCC': {'desc': 'TCC', 'val': 0, 'meaning': 'Radar plane'},
                                                 'RAD': {'desc': 'RAD', 'val': 2, 'meaning': 'SSR/ModeS Track'},
                                                 'spare': {'desc': 'spare bits set to 0', 'const': 0, 'val': 0},
                                                 'TRE': {'desc': 'TRE', 'val': 0, 'meaning': 'Track still alive'},
                                                 'CDM': {'desc': 'CDM', 'val': 0, 'meaning': 'Maintaining'},
                                                 'CNF': {'desc': 'CNF', 'val': 0, 'meaning': 'Confirmed Track'},
                                                 'SUP': {'desc': 'SUP', 'val': 0,
                                                         'meaning': 'Track from cluster network - NO'},
                                                 'FX': {'desc': 'FX', 'val': 0, 'meaning': 'End of Data Item'},
                                                 'DOU': {'desc': 'DOU', 'val': 0, 'meaning': 'Normal confidence'},
                                                 'MAH': {'desc': 'MAH', 'val': 0,
                                                         'meaning': 'No horizontal man. sensed'}})
            self.assertEqual(packet[0]['I200'], {'CGS': {'desc': 'Calculated groundspeed', 'val': 434.94},
                                                 'CHdg': {'desc': 'Calculated heading', 'val': 124.002685546875}})
            self.assertEqual(packet[0]['I220'], {'ACAddr': {'desc': 'AircraftAddress', 'val': '3C660C'}})

            self.assertEqual(packet[0]['I250'][0], {'TARGET_ALT_STATUS': {'desc': 'Status of Target ALT source bits',
                                                                          'meaning': 'No source information provided', 'val': 0},
                                                    'res': {'desc': 'Reserved', 'val': 0},
                                                    'FMS_ALT': {'desc': 'FMS Selected Altitude', 'val': 0.0},
                                                    'APP': {'desc': 'APPROACH Mode', 'meaning': 'Not active', 'val': 0},
                                                    'ALT_HOLD': {'desc': 'ALT HOLD Mode', 'meaning': 'Not active', 'val': 0},
                                                    'TARGET_ALT_SOURCE': {'desc': 'Target ALT source', 'meaning': 'Unknown', 'val': 0},
                                                    'BDS': {'desc': 'BDS register', 'val': '40'},
                                                    'FMS_ALT_STATUS': {'desc': 'FMS Altitude Status', 'val': 0},
                                                    'BP_STATUS': {'desc': 'Barometric Pressure Status', 'val': 1},
                                                    'BP': {'desc': 
'Barome
tric Pressure', 'val': 227.0},
                                                    'MODE_STATUS': {'desc': 'Status of MCP/FCU Mode Bits', 'val': 0},
                                                    'VNAV': {'desc': 'VNAV Mode', 'meaning': 'Not active', 'val': 0},
                                                    'MCP_ALT_STATUS': {'desc': 'MCP Altitude Status', 'val': 1},
                                                    'MCP_ALT': {'desc': 'MCP/FCU Selected Altitude', 'val': 33008.0}})

            self.assertEqual(packet[0]['I040'], {'THETA': {'desc': '', 'val': 340.13671875},
                                                 'RHO': {'desc': '', 'max': 256.0, 'val': 197.68359375}})
            self.assertEqual(packet[0]['I240'],
                             {'TId': {'desc': 'Characters 1-8 (coded on 6 bits each) defining target identification',
                                      'val': 'DLH65A  '}})
            self.assertEqual(packet[0]['I140'], {'ToD': {'desc': 'Time Of Day', 'val': 27354.6015625}})
            self.assertEqual(packet[0]['I070'], {'Mode3A': {'desc': 'Mode-3/A reply code', 'val': '1000'},
                                                 'V': {'desc': '', 'val': 0, 'meaning': 'Code validated'},
                                                 'L': {'desc': '', 'val': 0,
                                                       'meaning': 'Mode-3/A code as derived from the reply of the transponder'},
                                                 'spare': {'desc': 'spare bit set to 0', 'const': 0, 'val': 0},
                                                 'G': {'desc': '', 'val': 0, 'meaning': 'Default'}})
            self.assertEqual(packet[0]['I161'], {'Tn': {'desc': 'Track Number', 'val': 3563}})
            self.assertEqual(packet[0]['I020'], {'SIM': {'desc': 'SIM', 'val': 0, 'meaning': 'Actual target report'},
                                                 'TYP': {'desc': 'TYP', 'val': 5, 'meaning': 'Single ModeS Roll-Call'},
                                                 'RAB': {'desc': 'RAB', 'val': 0,
                                                         'meaning': 'Report from aircraft transponder'},
                                                 'RDP': {'desc': 'RDP', 'val': 0,
                                                         'meaning': 'Report from RDP Chain 1'},
                                                 'FX': {'desc': 'FX', 'val': 0, 'meaning': 'End of Data Item'},
                                                 'SPI': {'desc': 'SPI', 'val': 0, 'meaning': 'Absence of SPI'}})
            self.assertEqual(packet[0]['I090'], {'V': {'desc': '', 'val': 0, 'meaning': 'Code validated'},
                                                 'FL': {'desc': 'FlightLevel', 'val': 330.0},
                                                 'G': {'desc': '', 'val': 0, 'meaning': 'Default'}})
            self.assertEqual(packet[0]['I230'], {'COM': {'desc': 'COM', 'val': 1},
                                                 'BDS37': {'desc': 'BDS 1,0 bits 37/40', 'val': 5},
                                                 'ModeSSSC': {'desc': 'ModeS Specific Service Capability', 'val': 1,
                                                              'meaning': 'Yes'}, 'STAT': {'desc': 'STAT', 'val': 0},
                                                 'AIC': {'desc': 'Aircraft identification capability', 'val': 1,
                                                         'meaning': 'Yes'},
                                                 'BDS16': {'desc': 'BDS 1,0 bit 16', 'val': 1},
                                                 'spare': {'desc': 'spare bit set to 0', 'const': 0, 'val': 0},
                                                 'ARC': {'desc': 'Altitude reporting capability', 'val': 1,
                                                         'meaning': '25ft resolution'},
                                                 'SI': {'desc': 'SI/II Transponder Capability', 'val': 0,
                                                        'meaning': 'SI-Code Capable'}})

    def test_ParseCAT062CAT065(self):
        sample_filename = asterix.