#!/usr/bin/env python

"""REST-Pi.

Usage:
  sample.py (serve|secureserve) --config=FILE [--port=PORT]
  sample.py (-h | --help)
  sample.py --version

Options:
  -h --help     Show this screen.
  --version     Show version.

"""

from docopt import docopt
from restpi import RestServer
import subprocess
import platform
import signal
import ngrok
import yaml
import sys
import os


def parse_configurations(config_file):
    """
    Parse configuration details from yml file and also ensure that the file is
    well formatted
    """
    config = None
    with open(config_file) as stream:
        config = yaml.load(stream)

    if 'mapping' not in config or 'mode' not in config:
        raise ValueError

    if 'input' not in config['mapping'] or 'output' not in config['mapping']:
        raise ValueError

    return config

if __name__ == '__main__':
    # check to make sure a program exists
    print "Checking for ngrok on system"

    cmd = "where" if platform.system() == "Windows" else "which"
    try:
        subprocess.call([cmd, 'ngrok'])
    except:
        print "No executable"
        sys.exit(0)

    print "ngrok exists."

    def start_ngrok(on_port):
        return subprocess.call([
            'bin/ngrok_control',
            'start',
            '{}'.format(str(on_port))
        ])

    def stop_ngrok():
        return subprocess.call([
            'bin/ngrok_control',
            'stop'
        ])

    def signal_handler(signal, frame):
        try:
            p = stop_ngrok()
        except subprocess.CalledProcessError as e:
            print e.output
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)

    args = docopt(__doc__, version='REST-Pi 1.0')

    if args['--config']:
        try:
            config = parse_configurations(args['--config'])

            if args['serve']:
                server = RestServer(config)
                port_number = args['--port'] or 7000

                # Start ngrok
                p = start_ngrok(port_number)
                tunnels = ngrok.client.get_tunnels()
                http_tunnel = tunnels[1]
                print "Your public API is located at {}".format(
                    http_tunnel.public_url
                )
                server.run(port=port_number, debug=True)

            if args['secureserve']:
                pass

        except ValueError:
            print "There was an error while trying to read the \
                configuration file"

        except (subprocess.CalledProcessError, Exception) as e:
            stop_ngrok()
            print "Quiting program"
    else:
        print "You need to specify a valid configuration file"
