#!/usr/bin/python3
from sys import exit
from signal import signal, SIGINT
from mapper_checkout import Mapper
from mapper_checkout import MapperState

def handler(signal_received, frame): 
    print("Exiting...")
    exit(0)

def check_batteries(host):
    with Mapper(host) as mapper:
        mapper.check_aft_battery()
        mapper.check_fwd_battery()

def list_tests():
    print('\navailable tests:')
    print('\tall          - run all tests below')
    print('\tdate         - run `date`')
    print('\tntp          - check the tail of the xntpd.log')
    print('\tgps          - run `td gps`')
    print('\tfan          - run `fanTest`')
    print('\tbeacon       - qtalk to the avtrak and `CS`')
    print('\tacomms       - qtalk to the acomms and get s-registers')
    print('\taft_battery  - login to batt and print `STATS`')
    print('\tfwd_battery  - login to batt and print `STATS`')
    print('\tsvp          - run `check-svp`')
    print('\tparosci      - run `td parosci`')
    print('\tfastcat      - run `td fastcat`')
    print('\tkearfott     - run kearfott test with align flag')

    
if __name__ == '__main__':

    from argparse import ArgumentParser
    a = ArgumentParser(prog='check_mapper',
        description="check functions on MBARI Dorado mapping vehicles")    
    a.add_argument('--list',help='list available tests', action='store_true')
    a.add_argument('--host',default='mvc-mb1',help="url for vehicle telnet")
    a.add_argument('-v','--verbose', action='store_true', help='print parsed data')
    a.add_argument('device',nargs='?',default='all',help='device name to check, or `all`')

    args = a.parse_args()

    if args.list:
        list_tests()
        exit(0)
    
    signal(SIGINT, handler)
    
    run_all = args.device == 'all' 

    state = MapperState(args.host)

    test_count = 0

    with Mapper(args.host) as mapper:
        if mapper is None:
            exit(-1)

        if args.device == 'kearfott' or run_all:
            test_count += 1
            resp = mapper.check_kearfott()
            if resp != None: state.parse_kearfott(resp) 
            #if args.verbose: print('[PARSED]', state.ins)
        
        if args.device == 'gps' or run_all:
            test_count += 1
            resp = mapper.check_gps()
            if resp != None: state.parse_gps(resp) 
            if args.verbose: print('[PARSED]', state.gps)

        if args.device == 'date' or run_all:
            test_count += 1
            resp = mapper.check_date()
            if resp != None: state.parse_date(resp) 
            if args.verbose: print('[PARSED]', state.system)

        if args.device == 'ntp' or run_all:
            test_count += 1
            resp = mapper.check_ntp()
            if resp != None: state.parse_ntp(resp) 
            if args.verbose: print('[PARSED]', state.system)
        
        if args.device == 'fan' or run_all:
            test_count += 1
            resp = mapper.check_fan_card()
            if resp != None: state.parse_fan_card(resp) 
            if args.verbose: print('[PARSED]', state.fan_card)
        
        if args.device == 'beacon' or run_all:
            test_count += 1
            resp = mapper.check_beacon()
            if resp != None: state.parse_beacon(resp) 
            if args.verbose: print('[PARSED]', state.beacon)
        
        if args.device == 'acomms' or run_all:
            test_count += 1
            resp = mapper.check_benthos_modem()
            #if resp != None: state.parse_modem(resp) 
            #if args.verbose: print('[PARSED]', state.acomms)
        
        if args.device == 'aft_battery' or run_all:
            test_count += 1
            resp = mapper.check_aft_battery()
            if resp != None: state.parse_battery(resp, 'aft') 
            if args.verbose: print('[PARSED]', state.aft_battery)
    
        
        if args.device == 'fwd_battery' or run_all:
            test_count += 1
            resp = mapper.check_fwd_battery()
            if resp != None: state.parse_battery(resp, 'fwd') 
            if args.verbose: print('[PARSED]', state.fwd_battery)
        
        if args.device == 'svp' or run_all:
            test_count += 1
            resp = mapper.check_svp()
            if resp != None: state.parse_svp(resp) 
            if args.verbose: print('[PARSED]', state.svp)

        
        if args.device == 'parosci' or run_all:
            test_count += 1
            resp = mapper.check_parosci()
            if resp != None: state.parse_parosci(resp) 
            if args.verbose: print('[PARSED]', state.pressure)


        if args.device == 'fastcat' or run_all:
            test_count += 1
            resp = mapper.check_fastcat()
            if resp != None: state.parse_fastcat(resp) 
            if args.verbose: print('[PARSED]', state.ctd)
        
        if test_count == 0:
            print(f'Unknown device: {args.device}, try --list to see options.')

    



