#!/usr/bin/python3

import json
import subprocess
import argparse

TOOL = '/usr/sbin/util-gps-garmin-3dp'


if __name__ == '__main__':
    gps_interface = '/dev/3d-p/gps/0'
    power_active_high = True
    power_pin = 5
    try:
        output = subprocess.run(['iep-identify'], capture_output=True)
        data = json.loads(output.stdout)
        data = data['model']['tracking']['gps']
        for gps in data:
            gps_interface = gps['interface']['device']
            power_active_high = gps['control']['power']['active_high']
            power_pin = gps['control']['power']['pin']
    except:
        pass

    parser = argparse.ArgumentParser(description='GPS Garmin Configuration Tool')
    parser.add_argument(
        '-d', '--device',
        help='Garmin serial device [Defaults to = {}]'.format(gps_interface),
        default=gps_interface
    )
    parser.add_argument(
        '--is-garmin',
        help='Check if serial device is a valid Garmin GPS.',
        action='store_true',
    )
    parser.add_argument(
        '--toggle-off',
        help='Toggle specified NMEA sentences off.',
        action='store_true',
    )
    parser.add_argument(
        '--toggle-on',
        help='Toggle specified NMEA sentences on.',
        action='store_true',
    )
    parser.add_argument(
        '--toggle-all-off',
        help='Toggle all NMEA sentences off.',
        action='store_true',
    )
    parser.add_argument(
        '--toggle-all-on',
        help='Toggle all NMEA sentences on.',
        action='store_true',
    )
    parser.add_argument(
        '--toggle-reset',
        help='Reset sentences to factory defaults.',
        action='store_true',
    )
    parser.add_argument(
        '--get-baudrate',
        help='Get garmin baudrate.',
        action='store_true',
    )
    parser.add_argument(
        '--baudrate',
        help='Set garmin baudrate.',
        type=int,
    )
    parser.add_argument(
        '--softreboot',
        help='Software reboot garmin device.',
        action='store_true',
    )
    parser.add_argument(
        '--on',
        help='Power on garmin device.',
        action='store_true',
    )
    parser.add_argument(
        '--off',
        help='Power off garmin device.',
        action='store_true',
    )
    parser.add_argument(
        '-v', '--verbose',
        help='Print debugging message.',
        action='store_true',
    )
    parser.add_argument(
        '--GPRMC',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--GPGGA',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--GPGSA',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--GPGSV',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--PGRME',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--GPGLL',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--GPVTG',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--PGRMV',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--PGRMF',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--PGRMB',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--PGRMID',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--PGRMM',
        help='Used with toggle commands.',
        action='store_true',
    )
    parser.add_argument(
        '--PGRMT',
        help='Used with toggle commands.',
        action='store_true',
    )

    args = parser.parse_args()

    # COMMANDS
    extra_args = []
    if args.is_garmin:
        extra_args.append('--is-garmin')
    if args.toggle_off:
        extra_args.append('--toggle-off')
    if args.toggle_on:
        extra_args.append('--toggle-on')
    if args.toggle_all_off:
        extra_args.append('--toggle-all-off')
    if args.toggle_all_on:
        extra_args.append('--toggle-all-on')
    if args.toggle_reset:
        extra_args.append('--toggle-reset')
    if args.get_baudrate:
        extra_args.append('--get-baudrate')
    if args.softreboot:
        extra_args.append('--softreboot')
    if args.on:
        extra_args.append('--on')
    if args.off:
        extra_args.append('--off')
    if args.verbose:
        extra_args.append('-v')

    # NMEA
    for nmea in ['GPRMC', 'GPGGA', 'GPGSA', 'GPGSV', 'PGRME', 'GPGLL', 'GPVTG',
        'PGRMV', 'PGRMF', 'PGRMB', 'PGRMID', 'PGRMM', 'PGRMT', ]:
        if nmea in args and getattr(args, nmea):
            extra_args.append('--{}'.format(nmea))

    if args.baudrate:
        extra_args.append('--baudrate')
        extra_args.append('{}'.format(args.baudrate))

    command = [
        TOOL,
        '-d', args.device,
        '--power-pin', '{}'.format(power_pin),
        '--power-ah', '{}'.format(1 if power_active_high else 0),
    ]

    command.extend(extra_args)
    subprocess.run(command)
