#!/usr/bin/python

import json
import argparse
from goprohero import GoProHero
from wireless import Wireless

# get command to change
parser = argparse.ArgumentParser(description=(
    'A command line interface for passing commands to one or more GoPros.'))
parser.add_argument(
    'ssid', nargs='+', help='ssid of the gopro to control')
parser.add_argument(
    'password', help='the password for the gopro(s)')
parser.add_argument(
    'param', help='the parameter to be changed or "status" for status')
parser.add_argument(
    'value', nargs='?', default=None, help='the value to set the parameter to')
args = parser.parse_args()

# set up camera and network adapters
camera = GoProHero(password=args.password)
network = Wireless()

# command the cameras
for ssid in args.ssid:
    if args.param == 'status':
        print('For {}, getting status'.format(
            ssid, args.param, args.value))
    else:
        print('For {}, setting {} = {}'.format(
            ssid, args.param, args.value))

    # jump to a new network if needed
    if network.current() != ssid:
        network.connect(ssid=ssid, password=args.password)

    # get status or send command
    if args.param == 'status':
        print(json.dumps(camera.status(), indent=2, sort_keys=False))
    else:
        camera.command(args.param, args.value)
